I’m sorry for the late reply. I achieved the same using these pieces of code:
functions.js
add_filter('show_admin_bar', 'only_show_admin_bar_in_ajax_request', 99);
function only_show_admin_bar_in_ajax_request() {
return is_user_logged_in() && stripos($_SERVER['REQUEST_URI'], 'ajax_display_admin_bar') !== false;
}
add_action('wp_ajax_ajax_display_admin_bar', 'ajax_display_admin_bar');
add_action('wp_ajax_nopriv_ajax_display_admin_bar', 'ajax_display_admin_bar');
function ajax_display_admin_bar() {
if (only_show_admin_bar_in_ajax_request()) {
/** @var WP_Admin_Bar $wp_admin_bar */
global $wp_admin_bar;
/* Load the admin bar class code ready for instantiation */
require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
/* Instantiate the admin bar */
/**
* Filters the admin bar class to instantiate.
*
* @since 3.1.0
*
* @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
*/
$admin_bar_class = apply_filters( 'wp_admin_bar_class', 'WP_Admin_Bar' );
if ( class_exists( $admin_bar_class ) ) {
$wp_admin_bar = new $admin_bar_class;
} else {
exit;
}
$wp_admin_bar->initialize();
$wp_admin_bar->add_menus();
echo '<link rel="stylesheet" id="admin-bar-css" href="//www.steenks-service.nl/wp-includes/css/admin-bar.min.css" type="text/css" media="all">';
echo '<link rel="stylesheet" id="dashicons-css" href="//www.steenks-service.nl/wp-includes/css/dashicons.min.css" type="text/css" media="all">';
echo '<link rel="stylesheet" id="bottom-admin-bar-css" href="' . plugins_url( 'bottom-admin-bar/css/view.css' ) . '" type="text/css" media="all">';
/**
* Load all necessary admin bar items.
*
* This is the hook used to add, remove, or manipulate admin bar items.
*
* @since 3.1.0
*
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference
*/
do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
/**
* Fires before the admin bar is rendered.
*
* @since 3.1.0
*/
do_action( 'wp_before_admin_bar_render' );
$wp_admin_bar->render();
/**
* Fires after the admin bar is rendered.
*
* @since 3.1.0
*/
do_action( 'wp_after_admin_bar_render' );
}
exit;
}
main.js
$(function () {
$.ajax({
method: 'GET',
url: window.location.href,
async: true,
data: {
action: 'ajax_display_admin_bar'
},
success: function (response) {
$('body').append(response);
}
});
});