重命名WordPress管理菜单

时间:2013-05-26 作者:Harish Chouhan

我正在尝试将“WooCommerce”的管理菜单名称更改为“Store Settings”。

我尝试了其他代码,将帖子重命名为文章等,但对WooCommerce不起作用。

WooCommerce的管理菜单功能如下:

function woocommerce_admin_menu() {
    global $menu, $woocommerce;

    if ( current_user_can( \'manage_woocommerce\' ) )
    $menu[] = array( \'\', \'read\', \'separator-woocommerce\', \'\', \'wp-menu-separator woocommerce\' );

    $main_page = add_menu_page( __( \'WooCommerce\', \'woocommerce\' ), __( \'WooCommerce\', \'woocommerce\' ), \'manage_woocommerce\', \'woocommerce\' , \'woocommerce_settings_page\', null, \'55.5\' );

    $reports_page = add_submenu_page( \'woocommerce\', __( \'Reports\', \'woocommerce\' ),  __( \'Reports\', \'woocommerce\' ) , \'view_woocommerce_reports\', \'woocommerce_reports\', \'woocommerce_reports_page\' );

    add_submenu_page( \'edit.php?post_type=product\', __( \'Attributes\', \'woocommerce\' ), __( \'Attributes\', \'woocommerce\' ), \'manage_product_terms\', \'woocommerce_attributes\', \'woocommerce_attributes_page\');

    add_action( \'load-\' . $main_page, \'woocommerce_admin_help_tab\' );
    add_action( \'load-\' . $reports_page, \'woocommerce_admin_help_tab\' );

    $wc_screen_id = strtolower( __( \'WooCommerce\', \'woocommerce\' ) );

    $print_css_on = apply_filters( \'woocommerce_screen_ids\', array( \'toplevel_page_\' . $wc_screen_id, $wc_screen_id . \'_page_woocommerce_settings\', $wc_screen_id . \'_page_woocommerce_reports\', \'toplevel_page_woocommerce\', \'woocommerce_page_woocommerce_settings\', \'woocommerce_page_woocommerce_reports\', \'woocommerce_page_woocommerce_status\', \'product_page_woocommerce_attributes\', \'edit-tags.php\', \'edit.php\', \'index.php\', \'post-new.php\', \'post.php\' ) );

    foreach ( $print_css_on as $page )
        add_action( \'admin_print_styles-\'. $page, \'woocommerce_admin_css\' );
}

add_action(\'admin_menu\', \'woocommerce_admin_menu\', 9);
下面的代码有助于更改菜单名称,但它会导致CSS文件无法正确加载到设置页面。

function change_woocommerce_menu_title( $translated )
{
    $translated = str_replace( \'WooCommerce\', \'Store\', $translated );
    $translated = str_replace( \'WooCommerce\', \'Store\', $translated );
    return $translated;
}
add_filter( \'gettext\', \'change_woocommerce_menu_title\' );
add_filter( \'ngettext\', \'change_woocommerce_menu_title\' );

1 个回复
最合适的回答,由SO网友:brasofilo 整理而成

这是我重命名菜单项的操作:在动作挂钩中admin_menu, 使用递归数组搜索精确定位所需菜单的关键位置,然后修改全局$menu 大堆

add_action( \'admin_menu\', \'rename_woocoomerce_wpse_100758\', 999 );

function rename_woocoomerce_wpse_100758() 
{
    global $menu;

    // Pinpoint menu item
    $woo = recursive_array_search_php_91365( \'WooCommerce\', $menu );

    // Validate
    if( !$woo )
        return;

    $menu[$woo][0] = \'Store Settings\';
}

// http://www.php.net/manual/en/function.array-search.php#91365
function recursive_array_search_php_91365( $needle, $haystack ) 
{
    foreach( $haystack as $key => $value ) 
    {
        $current_key = $key;
        if( 
            $needle === $value 
            OR ( 
                is_array( $value )
                && recursive_array_search_php_91365( $needle, $value ) !== false 
            )
        ) 
        {
            return $current_key;
        }
    }
    return false;
}

结束

相关推荐

unit testing admin password

我正在为我的插件编写单元测试,我需要一个登录的用户来测试,但我在任何地方都找不到管理员密码。有人知道默认的管理员密码是什么吗?我正在扩展WP_UnitTestCase 并从https://unit-tests.svn.wordpress.org/trunk.我目前的代码:<?php require_once(\'myPlugin.php\'); class MyPluginTest extends WP_UnitTestCase{ function