Skip to content

Instantly share code, notes, and snippets.

@hakre
Created May 15, 2011 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/973172 to your computer and use it in GitHub Desktop.
Save hakre/973172 to your computer and use it in GitHub Desktop.
Extend Wordpress Admin Bar with any kind of content you like - not just links.
<?php
$getHtml = function($id) {
?>
<div><a>
Some custom HTML
</a></div>
<?php
};
$myPlugin = function($id, $getHtml) {
add_action('wp_before_admin_bar_render', function() {
ob_start();
add_action('wp_after_admin_bar_render', function() {
echo apply_filters('wp_buffered_admin_bar_render', ob_get_clean());
});
});
add_action('admin_bar_init', function() use($id, $getHtml) {
global $wp_admin_bar; /* @var WP_Admin_Bar */
$wp_admin_bar->add_menu(array('id' => $id, 'title' => $id));
add_action('wp_buffered_admin_bar_render', function($buffer) use ($id, $getHtml) {
if (is_callable($getHtml)) {
ob_start(); $html = $getHtml($id); $html .= ob_get_clean();
} else {
$html = $getHtml;
}
return str_replace("<a href=\"\">{$id}</a>", $html, $buffer);
});
});
};
$myPlugin('__0', $getHtml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment