From c3156e6258eb8b3dbb24d20d3090d6637fc00e55 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 14 Mar 2020 15:59:22 +0000 Subject: [PATCH] Toolbar: Move the logic for rendering the admin bar on `wp_footer` to `wp_admin_bar_render()`. Clarify in the function documentation that it is now called on `wp_body_open` action first, with `wp_footer` as a fallback. Follow-up to [47221]. Fixes #47053. git-svn-id: https://develop.svn.wordpress.org/trunk@47455 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/admin-bar.php | 17 +++++++++++++++-- src/wp-includes/default-filters.php | 1 + src/wp-includes/general-template.php | 8 -------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index 8799583eb7..753e5ab5be 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -55,8 +55,11 @@ function _wp_admin_bar_init() { /** * Renders the admin bar to the page based on the $wp_admin_bar->menu member var. * - * This is called very late on the footer actions so that it will render after - * anything else being added to the footer. + * This is called very early on the {@see 'wp_body_open'} action so that it will render + * before anything else being added to the page body. + * + * For backward compatibility with themes not using the 'wp_body_open' action, + * the function is also called late on {@see 'wp_footer'}. * * It includes the {@see 'admin_bar_menu'} action which should be used to hook in and * add new menus to the admin bar. That way you can be sure that you are adding at most @@ -64,11 +67,19 @@ function _wp_admin_bar_init() { * the `$post` global, among others. * * @since 3.1.0 + * @since 5.4.0 Called on 'wp_body_open' action first, with 'wp_footer' as a fallback. * * @global WP_Admin_Bar $wp_admin_bar + * + * @staticvar bool $rendered */ function wp_admin_bar_render() { global $wp_admin_bar; + static $rendered = false; + + if ( $rendered ) { + return; + } if ( ! is_admin_bar_showing() || ! is_object( $wp_admin_bar ) ) { return; @@ -100,6 +111,8 @@ function wp_admin_bar_render() { * @since 3.1.0 */ do_action( 'wp_after_admin_bar_render' ); + + $rendered = true; } /** diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 9a46d7e2f7..38655c5f7d 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -538,6 +538,7 @@ add_action( 'admin_init', '_wp_admin_bar_init' ); add_action( 'before_signup_header', '_wp_admin_bar_init' ); add_action( 'activate_header', '_wp_admin_bar_init' ); add_action( 'wp_body_open', 'wp_admin_bar_render', 0 ); +add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`. add_action( 'in_admin_header', 'wp_admin_bar_render', 0 ); // Former admin filters that can also be hooked on the front end. diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 7e29e082a6..897e28b3c9 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2892,14 +2892,6 @@ function wp_head() { * @since 1.5.1 */ function wp_footer() { - /** - * Sets up the Admin Bar if the current theme does not use `wp_body_open`. - * - * @since 5.4.0 - */ - if ( ! did_action( 'wp_body_open' ) ) { - add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); - } /** * Prints scripts or data before the closing body tag on the front end. *