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

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

1. 根据用户名来限制管理菜单项目的访问

如果你希望你的管理菜单的某些项对某些用户可见,那么这个代码就会帮到你。只需替换 functions.php 中的 clients-username 为如下代码即可:

01 function remove_menus()

02 {
03 global $menu;
04 global $current_user;
05 get_currentuserinfo();
06
07 if($current_user->user_login == 'clients-username')
08 {
09 $restricted = array(__('Posts'),
10 __('Media'),
11 __('Links'),
12 __('Pages'),
13 __('Comments'),
14 __('Appearance'),
15 __('Plugins'),
16 __('Users'),
17 __('Tools'),
18 __('Settings')
19 );
20 end ($menu);
21 while (prev($menu)){
22 $value = explode(' ',$menu[key($menu)][0]);
23 if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
24 }// end while
25
26 }// end if
27 }
28 add_action('admin_menu', 'remove_menus');


Source


2. 从面板中删除默认的Widget部件

01 // Create the function to use in the action hook

02 function example_remove_dashboard_widgets() {
03 // Globalize the metaboxes array, this holds all the widgets for wp-admin
04
05 global $wp_meta_boxes;
06
07 // Remove the incomming links widget
08 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
09
10 // Remove right now
11 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
12 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
13 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
14 }
15
16 // Hoook into the 'wp_dashboard_setup' action to register our function
17 add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );


Source


3. 在 WP Admin 中显示紧急信息

该代码将任何登录的用户显示定制的消息

01 /**

02 * Generic function to show a message to the user using WP's
03 * standard CSS classes to make use of the already-defined
04 * message colour scheme.
05 *
06 * @param $message The message you want to tell the user.
07 * @param $errormsg If true, the message is an error, so use
08 * the red message style. If false, the message is a status
09 * message, so use the yellow information message style.
10 */
11 function showMessage($message, $errormsg = false)
12 {
13 if ($errormsg) {
14 echo '<div id="message" class="error">';
15 }
16 else {
17 echo '<div id="message" class="updated fade">';
18 }
19
20 echo "<p><strong>$message</strong></p></div>";
21 }


然后,为管理提醒函数增加钩子用来显示定制消息:

01 /**

02 * Just show our message (with possible checking if we only want
03 * to show message to certain users.
04 */
05 function showAdminMessages()
06 {
07 // Shows as an error message. You could add a link to the right page if you wanted.
08 showMessage("You need to upgrade your database as soon as possible...", true);
09
10 // Only show to admins
11 if (user_can('manage_options') {
12 showMessage("Hello admins!");
13 }
14 }
15
16 /**
17 * Call showAdminMessages() when showing other admin
18 * messages. The message only gets shown in the admin
19 * area, but not on the frontend of your WordPress site.
20 */
21 add_action('admin_notices', 'showAdminMessages');


Source


4. 隐藏 WordPress 更新提醒

1 add_action('admin_menu','wphidenag');

2 function wphidenag() {
3 remove_action( 'admin_notices', 'update_nag', 3 );
4 }


Source


5. 从文章、页面编辑器中移除 Meta-Boxes

01 function remove_extra_meta_boxes() {

02 remove_meta_box( 'postcustom' , 'post' , 'normal' ); // custom fields for posts
03 remove_meta_box( 'postcustom' , 'page' , 'normal' ); // custom fields for pages
04 remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); // post excerpts
05 remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); // page excerpts
06 remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); // recent comments for posts
07 remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); // recent comments for pages
08 remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); // post tags
09 remove_meta_box( 'tagsdiv-post_tag' , 'page' , 'side' ); // page tags
10 remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' ); // post trackbacks
11 remove_meta_box( 'trackbacksdiv' , 'page' , 'normal' ); // page trackbacks
12 remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); // allow comments for posts
13 remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); // allow comments for pages
14 remove_meta_box('slugdiv','post','normal'); // post slug
15 remove_meta_box('slugdiv','page','normal'); // page slug
16 remove_meta_box('pageparentdiv','page','side'); // Page Parent
17 }
18 add_action( 'admin_menu' , 'remove_extra_meta_boxes' );


Source


6. 创建个性化的面板

可轻松编辑自己的应用

01 // Create the function to output the contents of our Dashboard Widget

02 function example_dashboard_widget_function() {
03 // Display whatever it is you want to show
04 echo "Hello World, I'm a great Dashboard Widget";
05 }
06
07 // Create the function use in the action hook
08 function example_add_dashboard_widgets() {
09 wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
10 }
11 // Hoook into the 'wp_dashboard_setup' action to register our other functions
12 add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );


Source


7. 禁用插件停用的功能

该代码将移除插件中的 Deactivate 链接,将下面代码保存到 functions.php 并保存。

01 add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );

02 function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {
03 // Remove edit link for all
04 if ( array_key_exists( 'edit', $actions ) )
05 unset( $actions['edit'] );
06 // Remove deactivate link for crucial plugins
07 if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(
08 'slt-custom-fields/slt-custom-fields.php',
09 'slt-file-select/slt-file-select.php',
10 'slt-simple-events/slt-simple-events.php',
11 'slt-widgets/slt-widgets.php'
12 )))
13 unset( $actions['deactivate'] );
14 return $actions;
15 }


Source


8. 根据角色添加、删除和记录面板部件

01 function tidy_dashboard()

02 {
03 global $wp_meta_boxes, $current_user;
04
05 // remove incoming links info for authors or editors
06 if(in_array('author', $current_user->roles) || in_array('editor', $current_user->roles))
07 {
08 unset($wp_meta_boxes['dashboard']['normal ']['core']['dashboard_incoming_links']);
09 }
10
11 // remove the plugins info and news feeds for everyone
12 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
13 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
14 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
15
16 }
17 //add our function to the dashboard setup hook
18 add_action('wp_dashboard_setup', 'tidy_dashboard');


下面是用来取消默认面板部件的代码列表:

01 //Right Now - Comments, Posts, Pages at a glance

02 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
03 //Recent Comments
04 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
05 //Incoming Links
06 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
07 //Plugins - Popular, New and Recently updated WordPress Plugins
08 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
09
10 //Wordpress Development Blog Feed
11 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
12 //Other WordPress News Feed
13 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
14 //Quick Press Form
15 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
16 //Recent Drafts List
17 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);


Source


9. 更简单的登录 URL

将下面代码粘贴到 .htaccess 文件,放在 WordPress rewrite 规则之前1 RewriteRule ^login$ http://yoursite.com/wp-login.php [NC,L]


Source


10. Remove Pages Columns

下面代码允许你移除 ‘Pages’ 页中的你不再需要的列

1 function remove_pages_columns($defaults) {

2 unset($defaults['comments']);
3 return $defaults;
4 }
5 add_filter('manage_pages_columns', 'remove_pages_columns');


Source


11. 禁止修改主题

该代码将面板中的 ‘Appearance’ 菜单项移除

1 add_action( 'admin_init', 'slt_lock_theme' );

2 function slt_lock_theme() {
3 global $submenu, $userdata;
4 get_currentuserinfo();
5 if ( $userdata->ID != 1 ) {
6 unset( $submenu['themes.php'][5] );
7 unset( $submenu['themes.php'][15] );
8 }
9 }


Source


12. 修改面板底部的文本

1 function remove_footer_admin () {

2 echo "Your own text";
3 }
4
5 add_filter('admin_footer_text', 'remove_footer_admin');


Source


13. Remove Author Metabox/Options & Move to Publish MetaBox

01 // MOVE THE AUTHOR METABOX INTO THE PUBLISH METABOX

02 add_action( 'admin_menu', 'remove_author_metabox' );
03 add_action( 'post_submitbox_misc_actions', 'move_author_to_publish_metabox' );
04 function remove_author_metabox() {
05 remove_meta_box( 'authordiv', 'post', 'normal' );
06 }
07 function move_author_to_publish_metabox() {
08 global $post_ID;
09 $post = get_post( $post_ID );
10 echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
11 post_author_meta_box( $post );
12 echo '</div>';
13 }


Source


14. 修改登录的 Logo

新的 logo 尺寸是 326 x 82 ,把图片放到主题的 images 目录,然后修改下面代码的 ‘companylogo.png’ 并将代码粘贴到 functions.php1 // login page logo
2 function custom_login_logo() {
3 echo '<style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'/companylogo.png) 50% 50% no-repeat !important; }</style>';
4 }
5 add_action('login_head', 'custom_login_logo');


Source


15. 移除文章 Columns

该代码将移除 posts 页面的列

1 function remove_post_columns($defaults) {

2 unset($defaults['comments']);
3 return $defaults;
4 }
5 add_filter('manage_posts_columns', 'remove_post_columns');


Source


16. 禁用管理面板的顶级菜单

01 function remove_menus () {

02 global $menu;
03 $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
04 end ($menu);
05 while (prev($menu)){
06 $value = explode(' ',$menu[key($menu)][0]);
07 if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
08 }
09 }
10 add_action('admin_menu', 'remove_menus');


Source


17. 禁用管理面板的子菜单

1 function remove_submenus() {

2 global $submenu;
3 unset($submenu['index.php'][10]); // Removes 'Updates'.
4 unset($submenu['themes.php'][5]); // Removes 'Themes'.
5 unset($submenu['options-general.php'][15]); // Removes 'Writing'.
6 unset($submenu['options-general.php'][25]); // Removes 'Discussion'.
7 }
8 add_action('admin_menu', 'remove_submenus');


Source


18. 增加自定义的面板 Logo

首先需要创建透明的图片,尺寸为 30x31px (.gif or .png) 然后保存到主题的 images 目录 (/wp-content/themes/theme-name/images) ,名字任意

01 //hook the administrative header output

02 add_action('admin_head', 'my_custom_logo');
03
04 function my_custom_logo() {
05 echo '
06 <style type="text/css">
07 #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
08 </style>
09 ';
10 }


Source


19. 为新的管理条添加和删除链接

01 function my_admin_bar_link() {

02 global $wp_admin_bar;
03 if ( !is_super_admin() || !is_admin_bar_showing() )
04 return;
05 $wp_admin_bar->add_menu( array(
06 'id' => 'diww',
07 'parent' => 'my-blogs',
08 'title' => __( 'Title of the link you want to add'),
09 'href' => admin_url( 'http://mysitesurl.com/wp-admin.php' )
10 ) );
11 }
12 add_action('admin_bar_menu', 'my_admin_bar_link');
1 function remove_admin_bar_links() {
2 global $wp_admin_bar;
3 $wp_admin_bar->remove_menu('my-blogs');
4 $wp_admin_bar->remove_menu('my-account-with-avatar');
5 }
6 add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

版权所有:《太阳花工作室》 => 《19 个代码片段让 WordPress 更易于管理
本文地址:http://bg.artuion.com/win_lin_mac/126.html
除非注明,文章均为 《太阳花工作室》 原创,欢迎转载!转载请注明本文地址,谢谢。