-
-
Save RiaanKnoetze/5193516e172554b8871236351e666cf1 to your computer and use it in GitHub Desktop.
<?php | |
add_action( 'admin_bar_menu', function( $wp_admin_bar ) { | |
if ( is_object( $wp_admin_bar ) && $wp_admin_bar->get_node( 'woocommerce-site-visibility-badge' ) ) { | |
$wp_admin_bar->remove_node( 'woocommerce-site-visibility-badge' ); | |
} | |
}, 100 ); |
I think you make a valid point. It would also be more robust to check if the methods get_node
and remove_node
exist before calling them.
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
if ( is_object( $wp_admin_bar ) && method_exists($wp_admin_bar, 'get_node') && method_exists($wp_admin_bar, 'remove_node') && $wp_admin_bar->get_node( 'woocommerce-site-visibility-badge' ) ) {
$wp_admin_bar->remove_node( 'woocommerce-site-visibility-badge' );
}
}, 100 );
I was being factitious because I think the feature this fix is for is regressive.
I'll be genuine now: admin_bar_menu
appears to always return a valid WP_Admin_Bar
object -- so there's no need to test all this actually 😅
As you can see here, all these tests are already performed before the action hook applies: https://github.com/WordPress/wordpress-develop/blob/6.6.2/src/wp-includes/admin-bar.php#L81-L97. (Using array()
instead of []
and the reference passing are actually bugs).
Using get_node()
is redundant because remove_node
simply calls an unset()
. PHP handles the checking whether it's actually set in the first place: https://github.com/WordPress/wordpress-develop/blob/6.6.2/src/wp-includes/class-wp-admin-bar.php#L284-L295.
Here's a modern approach that doesn't slow down the site:
add_action(
'admin_bar_menu',
function ( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'woocommerce-site-visibility-badge' );
},
100,
);
Applying this kind of critical thinking is what Automattic needs to encourage more. With that, we wouldn't even have to remove this stupid node.
Nice, the insights you've shared are quite valuable. Indeed, the modern remove_node
approach you've suggested is more efficient than the one I've shared.
@sybrew - thanks, but you've got a rogue comma after the 100 in that snippet.
@Peter-HVD It's recommended behavior to reduce redundant line changes in Git and SVN. It's allowed since PHP 7.3 (2018) -- see https://wiki.php.net/rfc/trailing-comma-function-calls#:~:text=Status%3A%20Implemented%20(in%20PHP%207.3).
@Peter-HVD It's recommended behavior to reduce redundant line changes in Git and SVN. It's allowed since PHP 7.3 (2018) -- see https://wiki.php.net/rfc/trailing-comma-function-calls#:~:text=Status%3A%20Implemented%20(in%20PHP%207.3).
I did not know that - my IDE flags it as an error, and I've been battling with writing JSON files recently and they definitely doesn't like an extra comma, so I was in that headspace. Cheers.
Yes, we could also check if
$wp_admin_bar
has the methodsget_node
andremove_node
before calling them. However, in this context, it's not necessary because we know that$wp_admin_bar
should be an instance ofWP_Admin_Bar
, which does have these methods.