管理员菜单链接显示链接但不转到它

时间:2017-04-10 09:14:43

标签: php jquery html css wordpress

我已经将一些其他菜单从插件分组到单个菜单,我没有那么多用来为仪表板添加更多空间。只是因为某些原因,当我点击新菜单上的链接创建时,它不会转到该链接,但如果我将其悬停,我可以正常看到该链接。这适用于镀铬隐身,但在没有任何插件的情况下打破了firefox和chrome normal。

class re_arange_menus {
    public function __construct() {
        //add_action('admin_menu', [$this, 'admin_menu_refresh'], 999);
        add_action('admin_init', [$this, 'admin_menu_refresh']);
        add_action('admin_head', [$this, 'add_css']);
        add_action( 'wp_before_admin_bar_render', [$this, 'mytheme_admin_bar_render'] );
    }
    public function add_css() {
        echo '<style>
            #wp-admin-bar-_options:hover {
                height: 500px;
                overflow-y: scroll;
            }
            pre {
                margin-left: 10%;
            }
          </style>';
    }
    function mytheme_admin_bar_render() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('_options');
    }
    function admin_menu_refresh() {
        global $submenu, $menu;
        //var_Dump($submenu);
        $menus = [
            //Easy Wp Video Options
            '_options' => 'EasyWPVideo Options',
                    //Course Notes
            'edit.php?post_type=coursenote' => 'Course Notes',
            //Mail Bank - not necesary on removing sub pages - they are integrated in UI => NNUI
            'mb_email_configuration' => 'Mail Bank',
            // SRP - NNUI
            'srp-free-settings' => 'SRP FREE',
            //BWP Panel - NNUI
            'bws_panel' => 'BWS Panel',
            //Her Slider
            'hslide' => 'Hero Slider',
            // Tools
            'tools.php' => 'Tools',
            //YOAST SEO
            'wpseo_dashboard' => 'Yoast Seo',
            // HTTPS
            'wordpress-https' => 'HTTPS',
            // Shortcodes
            'shortcodes-ultimate' => 'ShortCodes Ultimate',
            //Simple Signup Form
            'simple_signup_pro' => 'Simple Signup Pro',
            //Slides
            'edit.php?post_type=slide' => 'Slides',
            //Comments
            'edit-comments.php' => 'Comments',
            //Links
            'link-manager.php' => 'Link Manager',
            'cj-products' => 'Css Jockey'
        ];
        $parent_slug = '_options';
        add_menu_page('Rarely Used Stuff', 'Rarely Used Stuff', 'manage_options', $parent_slug, '', '', 999);
        $i = 0;
        $submenu_keys = array_keys($submenu);
        $menus_keys = array_keys($menus);
        $menus_values = array_values($menus);
        $menus_mod = [];
        $single_menu = [];
        foreach($menus as $menu_item=>$label) {
            $item_nr = array_search($menu_item, array_keys($submenu));
            if ($item_nr) {
                $menus_mod[$menu_item] = $submenu[$submenu_keys[$item_nr]];
                $first_sub_array = current($menus_mod[$menu_item]);
                //$menus_mod[$menu_item][0][] = $label;
                $first_sub_array[] = $label;
                $position = current(array_keys($menus_mod[$menu_item]));
                $menus_mod[$menu_item][$position] = $first_sub_array;
            } else {
                $single_menu[$menu_item] = $label;
            }
        }
        //var_Dump($menus_mod);
        $menus_mod = array_merge($single_menu, $menus_mod);
        //var_Dump($menus_mod);
        //var_dump($menus_mod);
        foreach($menus_mod as $remove_menu=>$submenu) {
            if(is_array($submenu)) {
                if (!empty($submenu)) {
                    foreach($submenu as $link) {
                        //var_dump($link);
                        $test = remove_submenu_page( $remove_menu, $link[2] );
                        //var_Dump($test, $remove_menu, $link[2]);
                    }
                }
            }
            $test2 = remove_menu_page($remove_menu);
        }
        foreach($menus_mod as $add_menu_url=>$add_menu_label) {
            if(is_array($add_menu_label) && !empty($add_menu_label)) {
                $isFirst = true;
                foreach($add_menu_label as $submenu_arr) {
                    $args['parent_slug'] = $parent_slug;
                    $args['page_title'] = $submenu_arr[0];
                    if(!$isFirst) {
                        $title = str_replace('&raquo;', '', '- '.$submenu_arr[0]);
                        $args['menu_title'] = $title;
                    } else {
                        $label = end($submenu_arr);
                        //var_Dump($submenu_arr);
                        $args['menu_title'] = $label;
                    }
                    $args['menu_title'] = preg_replace('#<div(.*?)>(.*?)</div>#', '', $args['menu_title']);
                    $args['capability'] = 'manage_options';
                    if(strpos($submenu_arr[2], '.php') !== false) {
                        $args['menu_slug'] = $submenu_arr[2];
                    } else {
                        $args['menu_slug'] = 'admin.php?page='.$submenu_arr[2];
                    }
                    $args['function'] = null;
                    add_submenu_page(
                        $args['parent_slug'],
                        $args['page_title'],
                        $args['menu_title'],
                        $args['capability'],
                        $args['menu_slug'],
                        $args['function']
                    );
                    $isFirst = false;
                }
            } else {
                    //var_Dump($add_menu_label);
                $args['parent_slug'] = $parent_slug;
                $args['page_title'] = $add_menu_label;
                $args['menu_title'] = $add_menu_label;
                $args['capability'] = 'manage_options';
                if(strpos($add_menu_url, '.php') !== false) {
                    $args['menu_slug'] = $add_menu_url;
                } else {
                    $args['menu_slug'] = 'admin.php?page='.$add_menu_url;
                }
                $args['function'] = null;
                add_submenu_page(
                    $args['parent_slug'],
                    $args['page_title'],
                    $args['menu_title'],
                    $args['capability'],
                    $args['menu_slug'],
                    $args['function']
                );
            }
            $i++;
        }
    }
}
new re_arange_menus;

1 个答案:

答案 0 :(得分:0)

管理修复它。主要问题是:

  1. 在循环中使用变量$submenu作为键,它覆盖了全局$submenu数组中的元素。
  2. 未在add_submenu_page
  3. 下定义回调函数
  4. 在执行remove_submenu_page接缝时删除了所有功能,因此删除了该行代码
  5. 结果,也可以通过OOP

    class re_arange_menus {
        public $menus = [
                //Easy Wp Video Options
                '_options' => 'EasyWPVideo Options',
                        //Course Notes
                'edit.php?post_type=coursenote' => 'Course Notes',
                //Mail Bank - not necesary on removing sub pages - they are integrated in UI => NNUI
                'mb_email_configuration' => 'Mail Bank',
                // SRP - NNUI
                'srp-free-settings' => 'SRP FREE',
                //BWP Panel - NNUI
                'bws_panel' => 'BWS Panel',
                //Her Slider
                'hslide' => 'Hero Slider',
                // Tools
                'tools.php' => 'Tools',
                //YOAST SEO
                'wpseo_dashboard' => 'Yoast Seo',
                // HTTPS
                'wordpress-https' => 'HTTPS',
                // Shortcodes
                'shortcodes-ultimate' => 'ShortCodes Ultimate',
                //Simple Signup Form
                'simple_signup_pro' => 'Simple Signup Pro',
                //Slides
                'edit.php?post_type=slide' => 'Slides',
                //Comments
                'edit-comments.php' => 'Comments',
                //Links
                'link-manager.php' => 'Link Manager',
                'cj-products' => 'Css Jockey'
            ];
        public $parent_slug = 'rustaff';
        public $capabilities = 'manage_options';
        public $callback;
        public function __construct() {
            $this->callback = [$this, 'rustaff'];
            add_action('admin_menu', [$this, 'admin_menu_refresh'], 999);
            add_action('admin_head', [$this, 'add_css']);
            add_action('admin_bar_menu', [$this, 'toolbar_link_to_mypage'], 999);
        }
        function toolbar_link_to_mypage($wp_admin_bar){
            global $wp_admin_bar;
            $args = [
                'id' => 'rustaff',
                'title' => 'Rarely Used Staff',
                'href' => admin_url('admin.php?page=rustaff')
            ];
            $wp_admin_bar->add_node( $args );
        }
        public function add_css() {
            echo '<style>
                #wp-admin-bar-_options:hover {
                    height: 500px;
                    overflow-y: scroll;
                }
                pre {
                    margin-left: 10%;
                }
              </style>';
        }
        function rustaff() {
            $menus = $this->menus;
            echo '<h1>Menu Items Re-Grouped</h1>';
            foreach($menus as $menu_item=>$label) {
                echo ucwords($label).'<br>';
            }
        }
        public function admin_menu_refresh() {
            global $submenu, $menu;
            //var_Dump($submenu);
            $menus = $this->menus;
            $parent_slug = $this->parent_slug;
            $capability = $this->capabilities;
            $callback = $this->callback;
            //$callback = null;
            add_menu_page('Rarely Used Stuff', 'Rarely Used Stuff', $capability, $parent_slug, $callback, '', 999);
            $i = 0;
            $submenu_keys = array_keys($submenu);
            $menus_keys = array_keys($menus);
            $menus_values = array_values($menus);
            $menus_mod = [];
            $single_menu = [];
            foreach($menus as $menu_item=>$label) {
                //var_Dump($menu_item, array_keys($submenu));
                $item_nr = array_search($menu_item, array_keys($submenu));
                if ($item_nr) {
                    $menus_mod[$menu_item] = $submenu[$submenu_keys[$item_nr]];
                    $first_sub_array = current($menus_mod[$menu_item]);
                    //$menus_mod[$menu_item][0][] = $label;
                    $first_sub_array[] = $label;
                    $position = current(array_keys($menus_mod[$menu_item]));
                    $menus_mod[$menu_item][$position] = $first_sub_array;
                } else {
                    $single_menu[$menu_item] = $label;
                }
            }
            //var_Dump($menus_mod);
            $menus_mod = array_merge($single_menu, $menus_mod);
            foreach($menus_mod as $remove_menu=>$mysubmenu) {
                $test2 = remove_menu_page($remove_menu);
            }
            //var_Dump($submenu);
            $args = [];
            foreach($menus_mod as $add_menu_url=>$add_menu_label) {
                if(is_array($add_menu_label) && !empty($add_menu_label)) {
                    $isFirst = true;
                    foreach($add_menu_label as $submenu_arr) {
                        $args['parent_slug'] =  $parent_slug;
                        $args['page_title'] = $submenu_arr[0];
                        if(!$isFirst) {
                            $title = str_replace('&raquo;', '', '- '.$submenu_arr[0]);
                            $args['menu_title'] = $title;
                        } else {
                            $label = end($submenu_arr);
                            //var_Dump($submenu_arr);
                            $args['menu_title'] = $label;
                        }
                        $args['menu_title'] = preg_replace('#<div(.*?)>(.*?)</div>#', '', $args['menu_title']);
                        $args['capability'] = $capability;
                        if(strpos($submenu_arr[2], '.php') !== false) {
                            $args['menu_slug'] = $submenu_arr[2];
                        } else {
                            $args['menu_slug'] = 'admin.php?page='.$submenu_arr[2];
                        }
                        $args['function'] = null;
                        add_submenu_page(
                            $args['parent_slug'],
                            $args['page_title'],
                            $args['menu_title'],
                            $args['capability'],
                            $args['menu_slug'],
                            $args['function']
                        );
                        $isFirst = false;
                    }
                } else {
                        //var_Dump($add_menu_label);
                    $args['parent_slug'] = $parent_slug;
                    $args['page_title'] = $add_menu_label;
                    $args['menu_title'] = $add_menu_label;
                    $args['capability'] = $capability;
                    if(strpos($add_menu_url, '.php') !== false) {
                        $args['menu_slug'] = $add_menu_url;
                    } else {
                        $args['menu_slug'] = 'admin.php?page='.$add_menu_url;
                    }
                    $args['function'] = null;
                    add_submenu_page(
                        $args['parent_slug'],
                        $args['page_title'],
                        $args['menu_title'],
                        $args['capability'],
                        $args['menu_slug'],
                        $args['function']
                    );
                }
                $i++;
            }
        }
    }
    new re_arange_menus;