Wednesday, June 22, 2022
HomeWordPress Developmentmetabox - Have mu-plugin take away meta field ONLY if it is...

metabox – Have mu-plugin take away meta field ONLY if it is not already eliminated in features.php


I’ve a mu-plugin that removes a branded “Welcome to X” dashboard meta field positioned there by the internet hosting firm I work for. It really works advantageous principally.

Nevertheless, a few of our company purchasers are already eradicating it in features.php, which is now inflicting a deadly error when the mu-plugin removes it after which features.php tries once more.

The error I am getting is:

 Can't redeclare remove_specific_widget() (beforehand declared in /srv/htdocs/wp-content/mu-plugins/whitelabel-dashboard.php:24) in /srv/htdocs/wp-content/themes/twentytwentytwo/features.php on line 70

I’ve added a test to see if the operate already exists (in features.php), which ought to have the mu-plugin solely run when it is not present in features.php. However it’s only engaged on websites that do have the code in features.php, and never on websites that don’t.

It has been a few decade since I checked out any code, so be at liberty to level and chuckle. I think the reply is apparent and I am simply lacking it.

$test_info returns true (1) within the echo assertion on each the take a look at web site with removing code in features.php and on the positioning with out removing code.

Will function_exists() simply not work on this case? Is there another, or ought to I do it myself? (Finest I may provide you with was to seize features.php contents to a string, test for the operate identify with strpos() since some websites do not run PHP 8 so str_contains() is not an possibility. Then use that test the place I am utilizing function_exists() proper now.)

The mu-plugin is:


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

// take away the "Welcome to X" widget field from dashboard
operate remove_x_widget_with_plugin() {  // have to be completely different identify than equivalent operate utilized in features.php so function_exists() is not by accident triggered
        remove_meta_box( 'x_dashboard_widget', 'dashboard', 'regular' );
}

// if x widget is lively, take away it
operate 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');
?>```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments