I'm creating a custom WP Admin Bar menu item with custom HTML inside the dropdowns. Referring to this: https://codex.wordpress.org/Class_Reference/WP_Admin_Bar/add_menu

the HTML i want to use is quite extensive so I created a separate php file for that HTML. Then I'm trying to include that PHP file inside the menu item, but it's not printing/echoing properly. Right now, all the shows inside the menu item is a 1, and the content is getting printed later on in the DOM of the page.

Here's the code I have:

        'id'    => 'dh_row_layouts-content',
        'parent' => 'dh_row_layouts',
        'meta'  => array(        
            'class' => 'dh_hack',
            'html' => include('docs/row_layouts.php'),

Any ideas?

1 Answer 1


You can turn on output buffering, include (and evaluate) the PHP file, and save the output (of the evaluated code) in a variable, like so:

include 'docs/row_layouts.php';
$html = ob_get_clean();

Then just use 'html' => $html in the meta array when you call the $admin_bar->add_menu().

Or if you don't need to evaluate any PHP code in the file, you could use file_get_contents():

'html' => file_get_contents( 'docs/row_layouts.php' )

And you may need to or better use a full absolute path.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.