Adding a button to the admin bar. Add to Basket.
It is very useful sometimes to clean the site from old ads., for example, from ads from the real estate database. Let's consider an example of how to add your post button to the cart in the admin bar.
All standard buttons can be found in the array, contained in the object $post_type_object->labels. button information, which can be placed, easy to find in the file wp-includes/link-template.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function fb_add_admin_bar_trash_menu() { global $wp_admin_bar; if ( !is_super_admin() || !is_admin_bar_showing() ) return; $current_object = get_queried_object(); if ( empty($current_object) ) return; if ( !empty( $current_object->post_type ) && ( $post_type_object = get_post_type_object( $current_object->post_type ) ) && current_user_can( $post_type_object->cap->edit_post, $current_object->ID ) ) { $wp_admin_bar->add_menu( array( 'id' => 'delete', 'title' => __('Move to Trash'), 'href' => get_delete_post_link($current_object->term_id) ) ); } } add_action( 'admin_bar_menu', 'fb_add_admin_bar_trash_menu', 35 ); |
If you add a little code, you can make a button with an icon.
1 |
'title' => __('<img src="/wp-content/themes/themes-name/images/trash_(delete)_16x16.gif" alt="Move to Trash" title="Move to Trash" style="margin-top:5px" />' ) |
Removing extra items from the WordPress admin bar
In addition to adding new buttons to the admin bar WordPress we can also remove them, knowing the desired index. This is useful if you don't want to show a separate item for registered users..
An example of removing the comment button from the admin bar:
1 2 3 4 5 |
function fb_admin_bar_render() { global $wp_admin_bar; $wp_admin_bar->remove_menu('comments'); } add_action( 'wp_before_admin_bar_render', 'fb_admin_bar_render' ); |
It is also useful to limit some points, for users and check whether the admin bar is disabled or not, this is very easy to do. To delete items, just specify a short condition in the file functions.php your topic.
1 2 3 4 5 6 |
function wp_admin_bar_init() { if (!is_super_admin() || !is_admin_bar_showing() ) return; add_action( 'wp_before_admin_bar_render', 'fb_admin_bar_render' ); } |
You can also check the user for his privilege level.
1 2 3 4 5 |
get_currentuserinfo() ; global $user_level; if ($user_level <> 10) { ... тут код ... } |
Read more about privileges in English here: user level wordpress. If you want to write an article on my site about it, you can register or just log in through social networks. After logging in, you will be able to write an article. If the article turns out to be useful, I will post it., possible with amendments.
The code shown in the article works everything checked ). Another related article for you: adding an entire menu to the WordPress admin bar.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.