add_cap('edit_theme_options'); // 编辑主题选项
$role->add_cap('customize'); // 自定义器
$role->add_cap('switch_themes'); // 切换主题
$role->add_cap('edit_themes'); // 编辑主题
$role->add_cap('update_themes'); // 更新主题
$role->add_cap('install_themes'); // 安装主题
$role->add_cap('delete_themes'); // 删除主题
$role->add_cap('upload_themes'); // 上传主题
}
}
// 在主题激活时运行一次
add_action('after_switch_theme', 'enhance_editor_capabilities');
// 确保在插件加载后运行一次
add_action('init', 'enhance_editor_capabilities');
// 在导航菜单管理页面显示菜单ID
function nenghui_show_menu_id_in_nav_menus() {
// 只在nav-menus.php页面加载
global $pagenow;
if ($pagenow !== 'nav-menus.php') {
return;
}
// 获取所有菜单
$menus = wp_get_nav_menus();
if (empty($menus)) {
return;
}
?>
id === 'nav-menus') {
$screen->add_help_tab(array(
'id' => 'nenghui-menu-ids',
'title' => '菜单ID使用',
'content' => '
如何使用菜单ID
' .
'每个菜单都有一个唯一的数字ID,您可以在短代码中使用这些ID来指定特定的菜单。
' .
'About导航短代码示例:
' .
'' .
'[nenghui_about_nav] - 使用自定义器中设置的默认菜单 ' .
'[nenghui_about_nav menu_id="2"] - 使用ID为2的菜单 ' .
'[nenghui_about_nav menu_id="5"] - 使用ID为5的菜单 ' .
'
' .
'注意:菜单ID在页面顶部的信息框中显示,请使用正确的ID。
'
));
}
}
add_action('current_screen', 'nenghui_add_menu_help_info');
// 导入主题配置文件
// require_once 'theme-options.php'; // 暂时注释掉,文件不存在
// 导入小工具配置
require_once 'widgets/widgets-config.php';
// 刷新重写规则以确保FAQ归档页面正常工作
function nenghui_flush_rewrite_rules() {
// 只在主题激活时执行一次
if (get_option('nenghui_rewrite_rules_flushed') !== '1') {
flush_rewrite_rules();
update_option('nenghui_rewrite_rules_flushed', '1');
}
}
add_action('after_switch_theme', 'nenghui_flush_rewrite_rules');
// 当FAQ文章类型注册后刷新重写规则
function nenghui_flush_rewrite_rules_on_init() {
static $flushed = false;
if (!$flushed && get_option('nenghui_faq_rewrite_flushed') !== '1') {
flush_rewrite_rules();
update_option('nenghui_faq_rewrite_flushed', '1');
$flushed = true;
}
}
add_action('init', 'nenghui_flush_rewrite_rules_on_init', 999);
// 所有功能已拆分到独立的配置文件中:
// - 主题设置: inc/theme-setup.php
// - 后台管理: inc/admin-config.php
// - 资源加载: inc/assets-loader.php
// - 媒体配置: inc/media-config.php
// - 初始化配置: inc/init-config.php
// - 清理优化: inc/cleanup.php
// - 性能配置: inc/performance-config.php
// - 自定义器: inc/customizer.php
// - 短代码: inc/shortcode.php
/**
* 自定义导航菜单Walker类,支持多级下拉菜单
*/
class Nenghui_Walker_Nav_Menu extends Walker_Nav_Menu {
// 开始输出子菜单
function start_lvl(&$output, $depth = 0, $args = null) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent\n";
}
// 显示元素
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
$id_field = $this->db_fields['id'];
if (is_object($args[0])) {
$args[0]->has_children = !empty($children_elements[$element->$id_field]);
}
return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
// 开始输出菜单项
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
// 检查是否有子菜单
$has_children = !empty($args->has_children);
if ($has_children) {
$classes[] = 'menu-item-has-children';
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args);
$id = $id ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
$attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
$item_output = isset($args->before) ? $args->before : '';
$item_output .= '';
$item_output .= (isset($args->link_before) ? $args->link_before : '') . apply_filters('the_title', $item->title, $item->ID) . (isset($args->link_after) ? $args->link_after : '');
// 如果有子菜单,添加下拉箭头
if ($has_children) {
$item_output .= ' ▼';
}
$item_output .= '';
$item_output .= isset($args->after) ? $args->after : '';
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
// 结束输出菜单项
function end_el(&$output, $item, $depth = 0, $args = null) {
$output .= "\n";
}
}
function get_post_meta_by_key($key, $cate_type) {
global $wpdb;
$table = $wpdb->postmeta;
$result = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM $table WHERE meta_key = %s AND post_id IN (
SELECT post_id FROM $table WHERE meta_key = 'cate_type' AND meta_value LIKE %s
) LIMIT 1",
$key,
'%' . $wpdb->esc_like($cate_type) . '%'
));
return $result;
}
add_action('wp_ajax_load_more_products', 'load_more_products_callback');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products_callback');
function load_more_products_callback() {
check_ajax_referer('load_more_products_nonce', 'nonce');
$offset = intval($_POST['offset']);
$per_page = intval($_POST['per_page']);
$category = isset($_POST['category']) ? sanitize_text_field($_POST['category']) : '';
$meta_query = array();
if (!empty($category) && $category !== 'all') {
$meta_query[] = array(
'key' => 'cate_type',
'value' => $category,
'compare' => 'LIKE'
);
}
$query_args = array(
'post_type' => 'products',
'posts_per_page' => $per_page,
'offset' => $offset,
'meta_query' => $meta_query,
'post_status' => 'publish'
);
$products_query = new WP_Query($query_args);
$products = array();
if ($products_query->have_posts()) {
while ($products_query->have_posts()) {
$products_query->the_post();
$post_id = get_the_ID();
$cate_type = get_post_meta($post_id, 'cate_type', true);
$cover_image = get_the_post_thumbnail_url($post_id, 'product-cover-medium');
if (empty($cover_image)) {
$cover_image = get_post_meta($post_id, '_product_banner_url', true);
}
if (empty($cover_image)) {
$cover_image = get_template_directory_uri() . '/assets/images/products-1.webp';
}
$usage_scenario_data = get_post_meta($post_id, '_usage_scenario_data', true);
$description = '';
if (!empty($usage_scenario_data) && isset($usage_scenario_data['bottom_text'])) {
$description = $usage_scenario_data['bottom_text'];
}
$attr_keys = get_post_meta($post_id, 'attr_key', true);
$attr_values = get_post_meta($post_id, 'attr_value', true);
$attr_keys_array = !empty($attr_keys) ? explode(',', $attr_keys) : array();
$attr_values_array = !empty($attr_values) ? explode(',', $attr_values) : array();
$attrs = array();
for ($i = 0; $i < min(4, count($attr_keys_array)); $i++) {
if (isset($attr_keys_array[$i]) && isset($attr_values_array[$i])) {
$attrs[] = array(
'key' => trim($attr_keys_array[$i]),
'value' => trim($attr_values_array[$i])
);
}
}
$efficiency = get_post_meta($post_id, 'efficiency', true);
$is_new = get_post_meta($post_id, 'is_new', true);
$is_new_value = filter_var($is_new, FILTER_VALIDATE_BOOLEAN);
ob_start();
?>
ob_get_clean()
);
}
wp_reset_postdata();
}
wp_send_json_success(array('products' => $products));
}
// 拦截导航
// 关键修改:将钩子从 'template_redirect' 提前到 'init',并保持优先级 1
add_action( 'init', 'custom_redirect_chinese_to_cn_domain', 1 );
function custom_redirect_chinese_to_cn_domain() {
// 忽略后台请求和 AJAX 请求,避免误杀管理员操作
if ( is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) ) {
return;
}
$target_domain = 'https://cn.nenghui.com';
$current_uri = $_SERVER['REQUEST_URI'];
$is_chinese_request = false;
$matched_reason = '';
// ==========================================
// 🛠️ 调试模式开关 (测试完毕后请改为 false)
// ==========================================
$debug_mode = false;
// 1. 嗅探 A:严格的正则匹配,匹配 /zh/, /zh_CN, /zh-cn/ 等
// 解释:(?:^|/) 开头或斜杠; (zh|zh_CN|zh-CN|zh-cn|cn) 语言代码; (/|\?|$) 结尾、参数或斜杠
if ( preg_match( '#(?:^|/)(zh|zh_CN|zh-CN|zh-cn|cn)(/|\?|$)#i', $current_uri, $matches ) ) {
$is_chinese_request = true;
$matched_reason = "URL 路径匹配成功 (检测到: " . $matches[1] . ")";
}
// 2. 嗅探 B:检查 GET 参数 (?lang=zh_CN)
if ( isset( $_GET['lang'] ) && in_array( strtolower($_GET['lang']), ['zh_cn', 'zh-cn', 'zh', 'cn'] ) ) {
$is_chinese_request = true;
$matched_reason = "URL 参数匹配成功 (?lang=" . $_GET['lang'] . ")";
}
// ==========================================
// 🖥️ 可视化调试输出
// ==========================================
if ( $debug_mode && $is_chinese_request ) {
// 如果开启了调试且匹配成功,直接输出并终止
echo "";
echo "
🚀 路由跳转调试面板 (Init Hook)
";
echo "
当前请求的 URI: " . esc_html($current_uri) . "
";
echo "
状态: 🟢 匹配成功!准备执行跳转。
";
echo "
触发原因: " . esc_html($matched_reason) . "
";
echo "
目标地址: " . esc_html(trailingslashit($target_domain)) . "
";
echo "
提示:关闭 \$debug_mode 即可真正执行 301 跳转。
";
echo "
";
exit;
} elseif ( $debug_mode && isset($_GET['test_redirect']) ) {
// 增加一个强制调试入口。输入 http://localhost:82/?test_redirect=1 查看未匹配原因
echo "";
echo "
🔴 未匹配到中文特征
";
echo "
当前 URI: " . esc_html($current_uri) . "
";
echo "
";
exit;
}
// ==========================================
// 🚀 正式执行跳转逻辑
// ==========================================
if ( $is_chinese_request && !$debug_mode ) {
// 统一跳转到国内站首页
$final_url = trailingslashit( $target_domain );
// 必须使用 wp_safe_redirect 或 header 并在前置 hook 提前输出
wp_redirect( $final_url, 301 );
exit;
}
}
?>