【问题标题】:Manually highlight Wordpress admin menu item手动突出显示 Wordpress 管理菜单项
【发布时间】:2011-01-19 11:55:10
【问题描述】:

在 Wordpress 管理页面的页面菜单下,我得到了这个布局:

页面

  • 编辑 (网址:edit-pages.php)
  • 新增 (url: page-new.php)
  • 特殊页面(网址:edit-pages.php?special-pages=true)

如您所见,我添加了一个名为“特殊页面”的新子菜单项,它几乎是指向带有自定义过滤器的“编辑”页面的链接。因为 Wordpress 使用文件名来标识和突出显示子菜单项,所以每当我单击特殊页面时,都会选择编辑子菜单项。是否有强制 Wordpress 改为选择特殊页面菜单项?

干杯

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    更好的解决方案:

    add_filter('parent_file', 'my_plugin_select_submenu');
    function my_plugin_select_submenu($file) {
            global $plugin_page;
            if ('__my-current-submenu-slug__' == $plugin_page) {
                $plugin_page = '__my-submenu-slug-to-select__';
            }
            return $file;
    }
    

    【讨论】:

    • 在我的例子中,我链接到管理分类管理页面,分类没有附加到任何帖子类型。您的代码完美无缺。谢谢。
    【解决方案2】:

    要进一步阐明 Ken Vu 的答案,请编辑全局变量 $submenu_file$parent_file。例如,突出显示您的页面:

    global $submenu_file;
    $submenu_file = "edit-pages.php?special-pages=true";
    

    如果您需要更改突出显示的顶级项目,请使用$parent_file。例如,突出显示“Writing”设置页面:

    global $parent_file;
    global $submenu_file;
    $parent_file = 'options-general.php';
    $submenu_file = 'options-writing.php';
    

    【讨论】:

    • 你能告诉我这些代码最好放在哪里吗?我无法让他们工作。
    • 我试图在add_meta_boxes() 的回调函数中实现这一点,但它没有突出显示我的顶级管理菜单。我可以补充一点,我没有任何子菜单,只有顶级管理菜单。
    • 让它正常工作。我会写另一个答案,以便其他人更容易找到它。
    【解决方案3】:

    解决方案:使用 $submenu_file 变量

    $submenu_file = "edit-pages.php?special-pages=true"

    【讨论】:

    • 你能发布一个如何使用它的例子吗?我无法让它工作
    【解决方案4】:

    感谢 Ken Vu 和 Jonathan Brinley。使用您的答案,我终于使我的管理菜单突出显示以正常工作。当我努力让它工作时,我虽然会在这里发布整个结果,所以其他人可以更容易地找到它:

    这个想法是调用parent_file 过滤器(不幸的是,没有记录,因为许多 Wordpress 部分)。在我的例子中,我添加了一个自定义菜单,而不是创建自定义帖子类型时生成的默认菜单。

    在我的自定义邮政编码中,我调用了add_meta_boxes 操作。在这个钩子中,我调用parent_file 过滤器:

    add_filter('parent_file',     array(&$this, 'highlight_admin_menu'));
    

    _

    这就是我的hightlight_admin_menu 函数的样子:

    function highlight_admin_menu($some_slug){
    
      global $parent_file;
    
      $parent_file = 'post.php?post=149&action=edit';
    
      return $parent_file;
    }
    

    _

    这让我的菜单正确突出显示。尝试使用您自己的代码来了解在哪里发出add_filter('parent_file', ...) 代码。查找仅在特定页面加载时执行的一些代码,但很快就可以修改$parent_file 变量。

    我希望这会有所帮助!

    【讨论】:

      【解决方案5】:

      要更改子菜单项的突出显示菜单项,正确的过滤器是submenu_file

      add_filter('submenu_file', 'menuBold');
      
      static function menuBold($submenu_file)
      {
          if ( checkProperPage($_GET) ) {
              // The address of the link to be highlighted
              return 'post-new?post_type=foobar&foo=bar';
          }
      
          // Don't change anything
          return $submenu_file;
      }
      

      检查发生在 WP 的 ~/wp-admin/menu-header.php 文件的第 194 行(Wordpress 4.5.3):

      if ( isset( $submenu_file ) ) {
          if ( $submenu_file == $sub_item[2] )
              $class[] = 'current';
      ...
      }
      

      【讨论】:

        【解决方案6】:

        您可以修改此代码以使其适合您。您可以使用它更改父菜单和子菜单。测试代码。

        function change_active_parent($submenu_file)
        { 
            global $parent_file;
        
            $zone = 'edit-tags.php?taxonomy=zone&post_type=product';
            $storefront = 'edit-tags.php?taxonomy=storefront&post_type=product';
            $container = 'edit-tags.php?taxonomy=container&post_type=product';
            
            if (esc_html($zone) == $submenu_file) {
                $parent_file = 'parent';
                $submenu_file = $zone;
            }
            elseif (esc_html($storefront) == $submenu_file) {
                $parent_file = 'parent';
                $submenu_file = $storefront;
            }
            elseif (esc_html($container) == $submenu_file) {
                $parent_file = 'parent';
                $submenu_file = $container;
            }
            
            return $submenu_file;
        }
        
        add_filter( 'submenu_file', 'change_active_parent' );
        

        【讨论】:

          【解决方案7】:

          使用load-{$page_hook} 动作钩子并修改必要的全局变量:

          /**
           *  For giggles, lets make an admin page that is not "in the menu" to play with.
           */
          add_action('admin_menu', 'mort1305_admin_menu');
          function mort1305_admin_menu() {
              add_submenu_page(
                  NULL,
                  'Page Title',
                  '',
                  'administrator',
                  'my_slug',
                  'mort1305_page_content'
              );
          }
          
          /**
           *  The menu item to highlight and the submenu item to embolden.
           */
          add_action('load-admin_page_my_slug', 'mort1305_on_page_load');
          function mort1305_on_page_load(){
              global $plugin_file, $submenu_file, $title;
              $plugin_page = 'slug-of-menu-item-to-be-highlighted';
              $submenu_file = 'slug-of-submenu-item-to-be-bold';
              foreach($submenu[NULL] as $submenu_arr) {
                  if($submenu_arr[2] === 'test_page_slug') {
                      $title = $submenu_arr[3];
                      break;
                  }
              }
          }
          
          /**
           *  Page content to display.
           */
          function mort_1305_page_content() {
              echo This is the '. get_admin_page_title() .' page.  The slug of my parent is '. get_admin_page_parent() .'.';
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-10-25
            • 1970-01-01
            • 2012-09-15
            • 1970-01-01
            • 2013-08-08
            • 2016-02-25
            • 1970-01-01
            • 2011-10-14
            相关资源
            最近更新 更多