How can I override a plugin’s submenu page and keep the same slugs?

I have a plugin and would like to change the callback function of an admin submenu page so that I can change the output.

This is what I have so far (in a child-theme’s functions.php file):

add_action( ‘admin_menu’, ‘replace_submenu_page’, 50 ); // The order of the origional action is 49
function replace_submenu_page() {
remove_submenu_page( ‘edit.php?post_type=wc_booking’, ‘booking_calendar’);
add_submenu_page( ‘edit.php?post_type=wc_booking’, __( ‘Calendar’, ‘woocommerce-bookings’ ), __( ‘Calendar’, ‘woocommerce-bookings’ ), ‘manage_bookings’, ‘booking_calendar’, ‘new_callback’ );
}

function new_callback() {
echo “test”;
}

I expect this to output a page with only test printed on it. However, it outputs the content from the origional function followed by test from my new_callback function.

Furthermore, if I remove the add_submenu_page call above, it only removes the menu link and I can still access the page via the URL.

Can someone please help me find what I am missing?
I would like to completely replace this page using the same menu_slug and parent_slug so that I am changing the plugin’s functionality as little as possible.

The plugin I am testing with is Woocommerce Bookings, but I have experienced this issue while using other plugins as well.

Read more here:: How can I override a plugin’s submenu page and keep the same slugs?

Leave a Reply

Your email address will not be published. Required fields are marked *