19 个代码片段让 WordPress 更易于管理

openkk 13年前
   <img alt="19 个代码片段让 WordPress 更易于管理" src="https://simg.open-open.com/show/2b7a1759248e41bc41c172e68bdfd7a3.jpg" width="530" height="350" />    <h2>1. 根据用户名来限制管理菜单项目的访问</h2>    <p>如果你希望你的管理菜单的某些项对某些用户可见,那么这个代码就会帮到你。只需替换 functions.php 中的 clients-username 为如下代码即可:</p>    <pre class="brush: php; title: ; notranslate">function remove_menus()  {      global $menu;      global $current_user;      get_currentuserinfo();        if($current_user->user_login == 'clients-username')      {          $restricted = array(__('Posts'),                              __('Media'),                              __('Links'),                              __('Pages'),                              __('Comments'),                              __('Appearance'),                              __('Plugins'),                              __('Users'),                              __('Tools'),                              __('Settings')          );          end ($menu);          while (prev($menu)){              $value = explode(' ',$menu[key($menu)][0]);              if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}          }// end while        }// end if  }  add_action('admin_menu', 'remove_menus');</pre>    <p><a href="/misc/goto?guid=4958339108541325530" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>2. 从面板中删除默认的Widget部件</h2>    <pre class="brush: php; title: ; notranslate">// Create the function to use in the action hook  function example_remove_dashboard_widgets() {      // Globalize the metaboxes array, this holds all the widgets for wp-admin        global $wp_meta_boxes;        // Remove the incomming links widget      unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);           // Remove right now      unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);      unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);      unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);  }    // Hoook into the 'wp_dashboard_setup' action to register our function  add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );</pre>    <p><a href="/misc/goto?guid=4958339109342546964" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>3. 在 WP Admin 中显示紧急信息</h2>    <p>该代码将任何登录的用户显示定制的消息</p>    <pre class="brush: php; title: ; notranslate">/**   * Generic function to show a message to the user using WP's   * standard CSS classes to make use of the already-defined   * message colour scheme.   *   * @param $message The message you want to tell the user.   * @param $errormsg If true, the message is an error, so use   * the red message style. If false, the message is a status    * message, so use the yellow information message style.   */  function showMessage($message, $errormsg = false)  {      if ($errormsg) {          echo '    <div id="message" class="error">     ';      }      else {          echo '     <div id="message" class="updated fade">      ';      }        echo "      <p><strong>$message</strong></p>       </div>  ";  }    </div>  </pre>    <p>然后,为管理提醒函数增加钩子用来显示定制消息:</p>    <pre class="brush: php; title: ; notranslate">/**   * Just show our message (with possible checking if we only want   * to show message to certain users.   */  function showAdminMessages()  {      // Shows as an error message. You could add a link to the right page if you wanted.      showMessage("You need to upgrade your database as soon as possible...", true);        // Only show to admins      if (user_can('manage_options') {         showMessage("Hello admins!");      }  }    /**    * Call showAdminMessages() when showing other admin    * messages. The message only gets shown in the admin    * area, but not on the frontend of your WordPress site.    */  add_action('admin_notices', 'showAdminMessages');</pre>    <p><a href="/misc/goto?guid=4958339110136889754" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>4. 隐藏 WordPress 更新提醒</h2>    <pre class="brush: php; title: ; notranslate">add_action('admin_menu','wphidenag');  function wphidenag() {  remove_action( 'admin_notices', 'update_nag', 3 );  }</pre>    <p><a href="/misc/goto?guid=4958339110930293038" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>5. 从文章、页面编辑器中移除 Meta-Boxes</h2>    <pre class="brush: php; title: ; notranslate">function remove_extra_meta_boxes() {  remove_meta_box( 'postcustom' , 'post' , 'normal' ); // custom fields for posts  remove_meta_box( 'postcustom' , 'page' , 'normal' ); // custom fields for pages  remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); // post excerpts  remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); // page excerpts  remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); // recent comments for posts  remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); // recent comments for pages  remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); // post tags  remove_meta_box( 'tagsdiv-post_tag' , 'page' , 'side' ); // page tags  remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' ); // post trackbacks  remove_meta_box( 'trackbacksdiv' , 'page' , 'normal' ); // page trackbacks  remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); // allow comments for posts  remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); // allow comments for pages  remove_meta_box('slugdiv','post','normal'); // post slug  remove_meta_box('slugdiv','page','normal'); // page slug  remove_meta_box('pageparentdiv','page','side'); // Page Parent  }  add_action( 'admin_menu' , 'remove_extra_meta_boxes' );</pre>    <p><a href="/misc/goto?guid=4958339111723180669" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>6. 创建个性化的面板</h2>    <p>可轻松编辑自己的应用</p>    <pre class="brush: php; title: ; notranslate">// Create the function to output the contents of our Dashboard Widget  function example_dashboard_widget_function() {      // Display whatever it is you want to show      echo "Hello World, I'm a great Dashboard Widget";  }     // Create the function use in the action hook  function example_add_dashboard_widgets() {      wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');  }  // Hoook into the 'wp_dashboard_setup' action to register our other functions  add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );</pre>    <p><a href="/misc/goto?guid=4958339112504005563" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>7. 禁用插件停用的功能</h2>    <p>该代码将移除插件中的 Deactivate 链接,将下面代码保存到 functions.php 并保存。</p>    <pre class="brush: php; title: ; notranslate">add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );  function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {      // Remove edit link for all      if ( array_key_exists( 'edit', $actions ) )          unset( $actions['edit'] );      // Remove deactivate link for crucial plugins      if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(          'slt-custom-fields/slt-custom-fields.php',          'slt-file-select/slt-file-select.php',          'slt-simple-events/slt-simple-events.php',          'slt-widgets/slt-widgets.php'      )))          unset( $actions['deactivate'] );      return $actions;  }</pre>    <p><a href="/misc/goto?guid=4958339113291284536" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>8. 根据角色添加、删除和记录面板部件</h2>    <pre class="brush: php; title: ; notranslate">function tidy_dashboard()  {    global $wp_meta_boxes, $current_user;      // remove incoming links info for authors or editors    if(in_array('author', $current_user->roles) || in_array('editor', $current_user->roles))    {      unset($wp_meta_boxes['dashboard']['normal ']['core']['dashboard_incoming_links']);    }      // remove the plugins info and news feeds for everyone    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);    }  //add our function to the dashboard setup hook  add_action('wp_dashboard_setup', 'tidy_dashboard');</pre>    <p>下面是用来取消默认面板部件的代码列表:</p>    <pre class="brush: php; title: ; notranslate">//Right Now - Comments, Posts, Pages at a glance  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);  //Recent Comments  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);  //Incoming Links  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);  //Plugins - Popular, New and Recently updated WordPress Plugins  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);    //Wordpress Development Blog Feed  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);  //Other WordPress News Feed  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);  //Quick Press Form  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);  //Recent Drafts List  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);</pre>    <p><a href="/misc/goto?guid=4958339114094013331" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>9. 更简单的登录 URL</h2>    <p>将下面代码粘贴到 .htaccess 文件,放在 WordPress rewrite 规则之前</p>    <pre class="brush: php; title: ; notranslate">RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]</pre>    <p><a href="/misc/goto?guid=4958339114884405184" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>10. Remove Pages Columns</h2>    <p>下面代码允许你移除 ‘Pages’ 页中的你不再需要的列</p>    <pre class="brush: php; title: ; notranslate">function remove_pages_columns($defaults) {    unset($defaults['comments']);    return $defaults;  }  add_filter('manage_pages_columns', 'remove_pages_columns');</pre>    <p><a href="/misc/goto?guid=4958339115678282645" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>11. 禁止修改主题</h2>    <p>该代码将面板中的 ‘Appearance’ 菜单项移除</p>    <pre class="brush: php; title: ; notranslate">add_action( 'admin_init', 'slt_lock_theme' );  function slt_lock_theme() {      global $submenu, $userdata;      get_currentuserinfo();      if ( $userdata->ID != 1 ) {          unset( $submenu['themes.php'][5] );          unset( $submenu['themes.php'][15] );      }  }</pre>    <p><a href="/misc/goto?guid=4958339113291284536" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>12. 修改面板底部的文本</h2>    <pre class="brush: php; title: ; notranslate">function remove_footer_admin () {      echo "Your own text";  }     add_filter('admin_footer_text', 'remove_footer_admin');</pre>    <p><a href="/misc/goto?guid=4958339117185410832" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>13. Remove Author Metabox/Options & Move to Publish MetaBox</h2>    <pre class="brush: php; title: ; notranslate">// MOVE THE AUTHOR METABOX INTO THE PUBLISH METABOX  add_action( 'admin_menu', 'remove_author_metabox' );  add_action( 'post_submitbox_misc_actions', 'move_author_to_publish_metabox' );  function remove_author_metabox() {      remove_meta_box( 'authordiv', 'post', 'normal' );  }  function move_author_to_publish_metabox() {      global $post_ID;      $post = get_post( $post_ID );      echo '    <div style="border-bottom-width:0px;border-top:#eeeeee 1px solid;" id="author" class="misc-pub-section">     Author: ';      post_author_meta_box( $post );      echo '    </div>  ';  }</pre>    <p><a href="http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file?page=2&tab=votes#tab-top" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>14. 修改登录的 Logo</h2>    <p>新的 logo 尺寸是 326 x 82 ,把图片放到主题的 images 目录,然后修改下面代码的 ‘companylogo.png’ 并将代码粘贴到 functions.php</p>    <pre class="brush: php; title: ; notranslate">// login page logo  function custom_login_logo() {      echo '    <style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'/companylogo.png) 50% 50% no-repeat !important; }    </style>  ';  }  add_action('login_head', 'custom_login_logo');</pre>    <p><a href="/misc/goto?guid=4958339118755851654" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>15. 移除文章 Columns</h2>    <p>该代码将移除 posts 页面的列</p>    <pre class="brush: php; title: ; notranslate">function remove_post_columns($defaults) {    unset($defaults['comments']);    return $defaults;  }  add_filter('manage_posts_columns', 'remove_post_columns');</pre>    <p><a href="/misc/goto?guid=4958339109342546964" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>16. 禁用管理面板的顶级菜单</h2>    <pre class="brush: php; title: ; notranslate">function remove_menus () {  global $menu;      $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));      end ($menu);      while (prev($menu)){          $value = explode(' ',$menu[key($menu)][0]);          if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}      }  }  add_action('admin_menu', 'remove_menus');</pre>    <p><a href="/misc/goto?guid=4958339120274088795" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>17. 禁用管理面板的子菜单</h2>    <pre class="brush: php; title: ; notranslate">function remove_submenus() {    global $submenu;      unset($submenu['index.php'][10]); // Removes 'Updates'.      unset($submenu['themes.php'][5]); // Removes 'Themes'.      unset($submenu['options-general.php'][15]); // Removes 'Writing'.      unset($submenu['options-general.php'][25]); // Removes 'Discussion'.  }  add_action('admin_menu', 'remove_submenus');</pre>    <p><a href="/misc/goto?guid=4958339121062144858" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>18. 增加自定义的面板 Logo</h2>    <p>首先需要创建透明的图片,尺寸为 30x31px (.gif or .png) 然后保存到主题的 images 目录 (/wp-content/themes/theme-name/images) ,名字任意</p>    <pre class="brush: php; title: ; notranslate">//hook the administrative header output  add_action('admin_head', 'my_custom_logo');    function my_custom_logo() {  echo '      <style type="text/css">  #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }      </style>  ';  }</pre>    <p><a href="/misc/goto?guid=4958339121858998939" rel="nofollow" target="_blank"><span>Source </span></a></p>    <p> </p>    <h2>19. 为新的管理条添加和删除链接</h2>    <pre class="brush: php; title: ; notranslate">function my_admin_bar_link() {      global $wp_admin_bar;      if ( !is_super_admin() || !is_admin_bar_showing() )          return;      $wp_admin_bar->add_menu( array(      'id' => 'diww',      'parent' => 'my-blogs',      'title' => __( 'Title of the link you want to add'),      'href' => admin_url( 'http://mysitesurl.com/wp-admin.php' )      ) );  }  add_action('admin_bar_menu', 'my_admin_bar_link');</pre>   <pre class="brush: php; title: ; notranslate">function remove_admin_bar_links() {      global $wp_admin_bar;      $wp_admin_bar->remove_menu('my-blogs');      $wp_admin_bar->remove_menu('my-account-with-avatar');  }  add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );</pre>    <p><a href="/misc/goto?guid=4958339122655306773" rel="nofollow" target="_blank"><span>Source </span></a></p> via    <a href="/misc/goto?guid=4958339123446435853" rel="nofollow" target="_blank">skytechgeek</a>