How to display admin bar when using WP_USE_THEMES = false?

So I'm using WordPress in a 'themeless' manner, i.e. not using a "blog" template. My site files are in the root directory (not in the theme folder), and WordPressis installed in its own /wordpress/ directory. The only thing my theme exists for is customization of the back-end, including re-branding and custom post types. (I'm basically avoiding plug-ins and widgets and doing a bespoke implementation of WordPress)

With this set-up, is there a way I can get the admin bar to display when my clients view the front-end pages like it normally does?

NOTE: I've tried adding wp_head() and wp_footer() to no avail. I think this might have something to do with my custom file structure.

Topic admin-bar theme-development Wordpress

Category Web


If WordPress is being loaded from an external PHP file by including wp-blog-header.php and the WP_USE_THEMES constant is set to false, the template_redirect hook will not be fired. This is important because template_redirect is how the Toolbar is initialized on the front end. Taking a look at default-filters.php we can see where the Toolbar is initialized:

...
// Admin Bar
// Don't remove. Wrong way to disable.
add_action( 'template_redirect', '_wp_admin_bar_init', 0 ); // <-- initialize Toolbar
add_action( 'admin_init', '_wp_admin_bar_init' ); // <-- initialize Toolbar
add_action( 'before_signup_header', '_wp_admin_bar_init' ); // <-- initialize Toolbar
add_action( 'activate_header', '_wp_admin_bar_init' ); // <-- initialize Toolbar
add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
...

A function can be added via plugin or theme to trigger the initialization of the Toolbar:

function wpse240134_wp_admin_bar_init() {
    _wp_admin_bar_init();
}
add_action( 'init', 'wpse240134_wp_admin_bar_init' );

Note that _wp_admin_bar_init() is considered an internal function for WordPress so use it at your own risk.

See also Admin Bar (Toolbar) not showing on custom PHP file that loads WordPress.

More details on loading WP using an external PHP file: What is the correct way to use wordpress functions outside wordpress files?


require_once 'wordpress/wp-load.php';
wp_footer();

Maybe you have to remove some things from the footer-file (e.g. Powered By WordPress). With var_dump( $wp_actions ); you can see which actions was executed (a list of action hooks). And with var_dump( $wp_filter['wp_footer'] ); you can see which actions are registred for the specific hook (here wp_footer).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.