0

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');
?>```
1
  • Firstly, your test info check will run before functions.php is loaded, so that will always return false (hence the 1 you always see, as it is true that false === false). As regards the test inside your callback function - is the remove_x_widget function (the one inside functions.php) namespaced? If so, you'd need to include that namespace in your function_exists.
    – vancoder
    Jun 21 at 21:49

2 Answers 2

1

Hmm, a lazy workaround—as long as you're not running PHP < 5.3.0—might be to use a namespace in your mu-plugins file.

Something like this might do the trick:

<?php
namespace Arglebargle;

\add_action( 'wp_dashboard_setup', 'Arglebarle\remove_x_widget' );

function remove_x_widget() {
    // If the meta box has already been removed, it shouldn't be an
    // issue to 'remove' it again.
    \remove_meta_box( 'x_dashboard_widget', 'dashboard', 'normal' );
}

This code is untested. I hope it provides a good starting point.

1

function_exists won't work in an MU plugin because MU plugins are loaded before regular plugins, so the functions won't exist. The function_exists() check would need to be in the themes.

But you're overthinking the issue. The problem is not that the metabox has already been removed. That wouldn't cause an error. The problem is simply that you're using the same name for the function that does it. Therefore the solution is simply to use a unique name for the function. Better yet, just use an anonymous callback, which makes a conflict is impossible:

add_action(
    'wp_dashboard_setup',
    function() {
        remove_meta_box( 'x_dashboard_widget', 'dashboard', 'normal' );
    },
    999
);

The other thing I've done is use a high priority on the hook, which should ensure that the meta box is removed only after it's been added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.