metabox – Have mu-plugin remove meta box ONLY if it isn’t already removed in functions.php

Question

I have a mu-plugin that removes a branded “Welcome to X” dashboard meta box placed there by the hosting company I work for. It works fine mostly.

However, some of our agency clients are already removing it in functions.php, which is now causing a fatal error when the mu-plugin removes it and then functions.php tries again.

The error I’m getting is:

 Cannot redeclare remove_specific_widget() (previously declared in /srv/htdocs/wp-content/mu-plugins/whitelabel-dashboard.php:24) in /srv/htdocs/wp-content/themes/twentytwentytwo/functions.php on line 70

I’ve added a check to see if the function already exists (in functions.php), which should have the mu-plugin only run when it isn’t found in functions.php. But it is only working on sites that do have the code in functions.php, and not on sites that do not.

It’s been about a decade since I looked at any code, so feel free to point and laugh. I suspect the answer is obvious and I’m just missing it.

$test_info returns true (1) in the echo statement on both the test site with removal code in functions.php and on the site without removal code.

Will function_exists() just not work in this case? Is there an alternative, or should I do it myself? (Best I could come up with was to grab functions.php contents to a string, check for the function name with strpos() since some sites don’t run PHP 8 so str_contains() isn’t an option. Then use that check where I’m using function_exists() right now.)

The mu-plugin is:


$test_info = function_exists('remove_x_widget') === false;
echo($test_info);
echo('Is this thing on?');

// remove the "Welcome to X" widget box from dashboard
function remove_x_widget_with_plugin() {  // must be different name than identical function used in functions.php so function_exists() isn't accidentally triggered
        remove_meta_box( 'x_dashboard_widget', 'dashboard', 'normal' );
}

// if x widget is active, remove it
function remove_widget_if_active() {
        if (function_exists('remove_x_widget') === false) {
            add_action('wp_dashboard_setup', 'remove_x_widget_with_plugin' );
    }
}


add_action('wp_dashboard_setup', 'remove_widget_if_active');
?>```

0
Nox 3 months 2022-06-21T14:36:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse