Skip to content

Instantly share code, notes, and snippets.

@RiaanKnoetze
Created September 13, 2024 15:24
Show Gist options
  • Save RiaanKnoetze/5193516e172554b8871236351e666cf1 to your computer and use it in GitHub Desktop.
Save RiaanKnoetze/5193516e172554b8871236351e666cf1 to your computer and use it in GitHub Desktop.
Remove the site visibility badges (e.g. "Coming soon", "Store coming soon", and "Live") introduced by WooCommerce in version 9.1
<?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 );
@shameemreza
Copy link

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 );

@sybrew
Copy link

sybrew commented Sep 15, 2024

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.

@shameemreza
Copy link

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.

@Peter-HVD
Copy link

@sybrew - thanks, but you've got a rogue comma after the 100 in that snippet.

@sybrew
Copy link

sybrew commented Sep 16, 2024

@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
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment