admin - requireinclude php file in add_menu()

admin2025-06-04  0

I'm creating a custom WP Admin Bar menu item with custom HTML inside the dropdowns. Referring to this:

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:

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

Any ideas?

I'm creating a custom WP Admin Bar menu item with custom HTML inside the dropdowns. Referring to this: https://codex.wordpress/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:

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

Any ideas?

Share Improve this question asked Jan 21, 2019 at 20:54 Charlie WedelCharlie Wedel 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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:

ob_start();
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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748988612a315418.html

最新回复(0)