admin管理员组文章数量:1122846
I have a function that adds items to the WordPress toolbar. For example:
$args2 = array(
'id' => 'conversations_unread',
'title' => $visitor['conversations_unread'] . '   ',
'href' => XenForo_Application::get('options')->boardUrl . '/conversations'
);
How do I get an icon to the left of this new item?
I've tried to use the 'meta' but the icon doesn't show.
'meta' => array( 'class' => 'ib-icon' ),
I read a reference to adding the image to the 'title' but I'd like this icon to be like the comment bubble.
I have a function that adds items to the WordPress toolbar. For example:
$args2 = array(
'id' => 'conversations_unread',
'title' => $visitor['conversations_unread'] . '   ',
'href' => XenForo_Application::get('options')->boardUrl . '/conversations'
);
How do I get an icon to the left of this new item?
I've tried to use the 'meta' but the icon doesn't show.
'meta' => array( 'class' => 'ib-icon' ),
I read a reference to adding the image to the 'title' but I'd like this icon to be like the comment bubble.
Share Improve this question edited Dec 22, 2014 at 1:24 kaiser 50.8k27 gold badges150 silver badges244 bronze badges asked Dec 22, 2014 at 0:39 LPHLPH 8081 gold badge11 silver badges25 bronze badges4 Answers
Reset to default 16Full example
A quick (mu-)plugin as example:
<?php
/** Plugin Name: Add Admin Bar Icon */
add_action( 'admin_bar_menu', function( \WP_Admin_Bar $bar )
{
$bar->add_menu( array(
'id' => 'wpse',
'parent' => null,
'group' => null,
'title' => __( 'Example', 'some-textdomain' ),
'href' => get_edit_profile_url( get_current_user_id() ),
'meta' => array(
'target' => '_self',
'title' => __( 'Hello', 'some-textdomain' ),
'html' => '<p>Hello</p>',
'class' => 'wpse--item',
'rel' => 'friend',
'onclick' => "alert('Hello');",
'tabindex' => PHP_INT_MAX,
),
) );
} );
Which renders the following HTML as first element (and therefore also renders our admin bar useless, but that's not the point of this example):
<li id="wp-admin-bar-wpse" class="wpse--item">
<a class="ab-item" tabindex="9223372036854775807" href="http://astro.dev/wp-admin/profile.php" onclick="alert(\'Hello\');" target="_self" title="Hello" rel="friend">Example</a>
<p>Hello</p>
</li>
Now that we got a base, we can care about...
Adding Icons
The sad news: There is no easy way to do it. At least not without adding your own stylesheet. As you can read (in the code), there are some things happening: I prepended the HTML needed to wrap a Dashicon before the actual item. Then I added a very high integer as last number to the action - that's what decides the position of the item in the admin bar.
<?php
/** Plugin Name: Add Admin Bar Icon */
add_action( 'admin_bar_menu', function( \WP_Admin_Bar $bar )
{
$bar->add_menu( array(
'id' => 'wpse',
'title' => '<span class="ab-icon"></span>'.__( 'Example', 'some-textdomain' ),
'href' => get_edit_profile_url( get_current_user_id() ),
'meta' => array(
'target' => '_self',
'title' => __( 'Ahoi!', 'some-textdomain' ),
'html' => '<!-- Custom HTML that goes below the item -->',
),
) );
}, 210 ); // <-- THIS INTEGER DECIDES WHERE THE ITEM GETS ADDED (Low = left, High = right)
add_action( 'wp_enqueue_scripts', function()
{
wp_enqueue_style( /* register your stylesheet */ );
}
Finally you will need to add some CSS rules in your own stylesheet. The only moving part is the wpse
in the #/id
. The rest is constant and equal for all admin bar items/nodes. You might also need to adjust the top
position to fit the Dashicon. Just choose a Dashicon from their site and add the fXXX
code in the CSS below.
#wpadminbar #wp-admin-bar-wpse .ab-icon:before {
font: normal 20px/1 dashicons;
content: '\f306';
position: relative;
float: left;
speak: never;
padding: 4px 0;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-image: none !important;
margin-right: 6px;
}
to expand on kaiser's answer, you can also override the icon with a custom image URL and put the styles inline (or again separately if you like) eg. a 22px x 22px icon...
$iconurl = '/images/custom-icon.png';
$iconspan = '<span class="custom-icon" style="
float:left; width:22px !important; height:22px !important;
margin-left: 5px !important; margin-top: 5px !important;
background-image:url(\''.$iconurl.'\');"></span>';
$title = __( 'Example', 'some-textdomain' ),
then use for the title value:
'title' => $iconspan.$title,
In WP 6.6.1, simply add this in CSS :
#wp-admin-bar-YOUR_ID_ITEM>.ab-item:before {
content: "\eXXX";
font-family: eicons;
top: 3px;
font-size: 18px;
}
Find your icon in https://elementor.github.io/elementor-icons/
Anyone knows which php file this code originates from? It creates a WP header tab for support in backend of wp. Want to remove it.
<li id="wp-admin-bar-child_menu_id"><a class="ab-item" href="https://www.rubixhost.com.au" target="_blank">Hosting Support</a></li>
Phone Support
Facebook Support本文标签: How do I add an icon to a new admin bar item
版权声明:本文标题:How do I add an icon to a new admin bar item? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296062a1929705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论