Created
May 15, 2011 13:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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