$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>admin - requireinclude php file in add_menu()|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

admin - requireinclude php file in add_menu()

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far