Home WordpressWordPress cơ bản [WordPress] Tùy biến thanh admin menu

[WordPress] Tùy biến thanh admin menu

by admincp

wordpress-3.3.1 new admin bar

Để thêm vào menu ngang trong phần quản trị wordpress, sử dụng hook “wp_before_admin_bar_render”. Thêm đoạn code sau vào theme functions.php

function remove_admin_bar_links() {
    global $wp_admin_bar;
	//admin mặc định
    $wp_admin_bar->remove_menu('wp-logo');          // Remove the WordPress logo
    $wp_admin_bar->remove_menu('about');            // Remove the about WordPress link
    $wp_admin_bar->remove_menu('wporg');            // Remove the WordPress.org link
    $wp_admin_bar->remove_menu('documentation');    // Remove the WordPress documentation link
    $wp_admin_bar->remove_menu('support-forums');   // Remove the support forums link
    $wp_admin_bar->remove_menu('feedback');         // Remove the feedback link
    $wp_admin_bar->remove_menu('site-name');        // Remove the site name menu
    $wp_admin_bar->remove_menu('view-site');        // Remove the view site link
    $wp_admin_bar->remove_menu('updates');          // Remove the updates link
    $wp_admin_bar->remove_menu('comments');         // Remove the comments link
    $wp_admin_bar->remove_menu('new-content');      // Remove the content link
    $wp_admin_bar->remove_menu('my-account');       // Remove the user details tab
 
	//xoá menu khác không pải mặc định
	//chuột pải vào menu inspect element,Quan sát các dòng có <li id=”wp-admin-bar-XXXXX>…</li> (XXXXX ở đây chính là NAME)
	$wp_admin_bar->remove_menu('NAME MENU');
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

Lưu ý: Để biết tên menu sử dụng tính năng inspect element của trình duyệt, chuột phải vào menu bạn muốn xoá tìm dòng

  • (XXXXX ở đây chính là tên của menu)

    Trong TH menu không tồn tại, thì bạn có thể kiểm tra xem nó tồn tại không? Nếu tồn tại thì xoá đi.

    $logo=$wp_admin_bar->get_node('wp-logo');
    // Check if the 'logo' node exists
    if( $logo ) {
    	$wp_admin_bar->remove_node( 'wp-logo' );
    }

    Create or add new items into the Admin bar. Dùng biến global $wp_admin_bar ở trên. Chi tiết Cú pháp:

    //add node
    $args = array(
            'parent' => false, // false for a root menu, pass the ID value for a submenu of that menu.
    		'id'    => 'my_page', //=false defaults to a sanitized title value.
    		'title' => 'My Page',
    		'href'  => 'http://mysite.com/my-page/',
              // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', 'target' => '', 'title' => '' );
    		'meta'  => array( 'class' => 'my-toolbar-page' )
    	);
    	$wp_admin_bar->add_node( $args );

    Eg:
    Sửa link của logo.

    //add node
    $args = array(
    		'id'    => 'wp-logo',
    		'title' => 'My Page',
    		'href'  => 'http://mysite.com/my-page/',
    		'meta'  => array( 'class' => 'my-toolbar-page' )
    	);
    	$wp_admin_bar->add_node( $args );

    Ngoài ra bạn cũng có thể sửa đổi nav menu admin bar với action admin_bar_menu. Ví dụ sau đây tạo một menu trong menu chứa các items:

    class FacebookMenu {
     
      function FacebookMenu()
      {
          add_action( 'admin_bar_menu', array( $this, "facebook_links" ) );
      }
     
      /**
       * Add's new global menu, if $href is false menu is added but registred as submenuable
       *
       * $name String
       * $id String
       * $href Bool/String
       *
       * @return void
       * @author Janez Troha
       * @author Aaron Ware
       **/
     
      function add_root_menu($name, $id, $href = FALSE)
      {
        global $wp_admin_bar;
        if ( !is_super_admin() || !is_admin_bar_showing() )
            return;
     
        $wp_admin_bar->add_menu( array(
            'id'   => $id,
            'meta' => array(),
            'title' => $name,
            'href' => $href ) );
      }
     
      /**
       * Add's new submenu where additinal $meta specifies class, id, target or onclick parameters
       *
       * $name String
       * $link String
       * $root_menu String
       * $id String
       * $meta Array
       *
       * @return void
       * @author Janez Troha
       **/
      function add_sub_menu($name, $link, $root_menu, $id, $meta = FALSE)
      {
          global $wp_admin_bar;
          if ( ! is_super_admin() || ! is_admin_bar_showing() )
              return;
     
          $wp_admin_bar->add_menu( array(
              'parent' => $root_menu,
              'id' => $id,
              'title' => $name,
              'href' => $link,
              'meta' => $meta
          ) );
      }
     
      function facebook_links() {
          $this->add_root_menu( "Facebook", "fcbl" );
          $this->add_sub_menu( "Facebook pages", "http://www.facebook.com/pages/manage", "fcbl", "fcblp" );
          $this->add_sub_menu( "Facebook apps", "http://www.facebook.com/developers/apps.php", "fcbl", "fcbla" );
          $this->add_sub_menu( "Facebook insights", "http://www.facebook.com/insights", "fcbl", "fcbli" );
      }
     
    }
    add_action( "init", "FacebookMenuInit" );
    function FacebookMenuInit() {
        global $FacebookMenu;
        $FacebookMenu = new FacebookMenu();
    }

    Sửa logo

    Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Vinastar trên TwitterFacebook

    Liên hệ

    Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

    Hotline tư vấn: 0989 48 3456

    Nguồn: Sưu tầm trên internet

    You may also like

    Leave a Comment