Ask your WordPress questions! Pay money and get answers fast! (more info)

Wordpress automatically adds widgets

  • SOLVED

Hi Experts,

I'm hoping to find a solution to my question...

After a new installation of Wordpress, Wordpress automatically adds 6 widgets to the first Widget Area of the activated theme. (screenshot attached)

I'm a theme developer and I would like to clean things up for my users and not have the first sidebar filled with all these default widgets.

Is there a bit of code I can add to my theme to prevent this "auto adding of widgets" done by Wordpress? Or another way to put it: how can I programmatically remove widgets from a sidebar upon theme activation?

Thanks for your time :)

Answers (3)

2017-01-23

Reigel Gallarde answers:

Yes you can... I want to paste some codes but here's a link from where I got mine...

https://thomasgriffin.io/remove-default-widgets-wordpress/


WP Answers comments:

Hi,

Thanks for your response.

The above code is cool however that code actually removes the widgets completely from the Wordpress back-end.

I do not want to remove these widgets completely. I only want to remove them from the first sidebar and/or prevent Wordpress from "registering" these widgets when a theme gets activated.

Do you see the difference in my request, or shall I provide more details?

Thank you


Reigel Gallarde comments:

I see... yes that's a problem...

hmm... maybe something like this?

add_action( 'plugins_loaded', 'wp011_remove_action' );
add_action( 'init', 'wp011_do_widgets_init', 1 );
function wp011_remove_action() { remove_action( 'init', 'wp_widgets_init', 1 ); }
function wp011_do_widgets_init() { do_action( 'widgets_init' ); }


WP Answers comments:

Hmm....That code did not work :-/


Reigel Gallarde comments:

try it as a plugin..

I've tried it and it did work...


WP Answers comments:

Hi Reigel,

This code will be included in a Wordpress Theme for resale so the code must be added directly to the theme, no plugin.

Do you have a modified version of the code to allow for the code to be in the theme?

Thank you


Reigel Gallarde comments:

ok... how about we clear the widgets on the db?? but only one time and that is when your theme gets activated?

function clear_theme_widgets() {

// clear widgets
update_option('sidebars_widgets', array());
}
add_action('after_switch_theme', 'clear_theme_widgets', 10);


WP Answers comments:

I like this. The code does work, and I think you're on the right track.

There is a problem though. Sometimes users will switch from the main parent theme to their child theme. Also they sometimes will switch themes when debugging an issue, etc.

Is it possible to run this function only 1 time and somehow store that data so it never runs more than once?

If not possible, is it possible to only clear the widgets from the "first sidebar"? I see that array()......can we add something to the array so it only clears the widgets from the first sidebar?

Thanks for your help. We are very close to solution! :)


Reigel Gallarde comments:

you can try using update_option with your variable and check it with get_option... I mean something like this:

function clear_theme_widgets() {

if (get_option('clear_theme_widget') != 1) {
// clear widgets
update_option('sidebars_widgets', array());
update_option('clear_theme_widget', 1);
}

}
add_action('after_switch_theme', 'clear_theme_widgets', 10);

you might also get some hints from here:
http://stackoverflow.com/questions/11757461/how-to-populate-widgets-on-sidebar-on-theme-activation

2017-01-24

Bob answers:

I have not tested it. but it might work.


if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
add_action('admin_footer','removed_widgets');
}


function removed_widgets(){
//get all registered sidebars
global $wp_registered_sidebars;
//get saved widgets
$widgets = get_option('sidebars_widgets');
//loop over the sidebars and remove all widgets
foreach ($wp_registered_sidebars as $sidebar => $value) {
unset($widgets[$sidebar]);
}
//update with widgets removed
update_option('sidebars_widgets',$widgets);
}


WP Answers comments:

Sorry this code did not work :-/

2017-01-24

Francisco Javier Carazo Gil answers:

In order to prevent doing this two times I will add some check like:

if( get_option( 'widgets_cleared' ) != 1 )
// code to delete widgets

And after it:
update_options( 'widgets_cleared', 1 );


WP Answers comments:

The code is not visible.....was your comment meant to include additional code?

thank you


Francisco Javier Carazo Gil comments:

Yes I would add some additional code in order to prevent to delete widgets always.