• Resolved LeoAltair

    (@leoaltair)


    Hi !
    I’m trying to get rid of the default WordPress widgets, as I do not use them. Since they are launched by
    add_action('init', 'wp_widgets_init', 1);
    I tried
    remove_action('init', 'wp_widgets_init', 1);

    I also use

    add_action( 'init', 'other_widgets_init') ;
    function other_widgets_init() {
    	if ( !is_blog_installed() )
    		return;
    	do_action('widgets_init');
    }

    to launch all the other widgets, since this call is in wp_widgets_init.

    But remove_action does not do its job ! The default widget are still loaded…

    Everythings works fine if I comment the add_action in the core file, but I don’t want to do it this way, I want to do the thing proper.

    Why doesn’t the remove_action do its job of removing the call to wp_widgets_init like the Codex says it should ?!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Where/when in the WP load process are your trying to remove the action hook? Your plugin files? Plugins are loaded relatively early, perhaps the hook has not yet been added when you try to remove it.

    I am able to remove it with your code from an ‘init’ action hook callback.

    Thread Starter LeoAltair

    (@leoaltair)

    Yes it’s in a plugin file. I don’t see what you mean. The action I’m trying to remove has the priority “1”, which seems pretty early a priority. And it is part of the wordpress core. I don’t see how I can be before.

    You mean I should put my

    remove_action('init', 'wp_widgets_init', 1);

    in another ‘init’ ? Like in this ?

    add_action( 'init', 'hook_the_remove', 1) ;
    function hook_the_remove() {
      remove_action('init', 'wp_widgets_init', 1);
      add_action('widgets_init', 'show_widgets_list', 999999) ;
      other_widgets_init() ;
    }

    That sounds wierd. And I tested it, it doesn’t work : the default widgets are still loaded, even if after the others.

    I am able to remove it with your code from an ‘init’ action hook callback.

    Could you show me your code please ?

    Moderator bcworkz

    (@bcworkz)

    No, it’s not weird, it’s stupid! I didn’t really think that one through, apologies. I removed it after it fired, duh!

    The concept is correct though, we just need a slightly earlier hook, the closest previous hook is ‘after_theme_setup’. I’m able to still remove the hook. This is the code I used to remove and confirm it is removed:

    add_action('after_setup_theme', 'remove_widget_init');
    function remove_widget_init() {
    	global $wp_filter;
    	remove_action('init', 'wp_widgets_init', 1);
    	echo '<pre>';
    	var_dump( $wp_filter['init'] );
    	echo '</pre><br>';
    	return;
    }

    Added to a catch all plugin I use for misc. code and testing. The function would have been listed under the ‘[1]’ key if it were not removed. You wouldn’t even have a ‘[1]’ key if it was the only hook with a 1 priority. You can rearrange the code to see it listed before removal if that would make more sense than my explanation.

    Thread Starter LeoAltair

    (@leoaltair)

    The removal works great with this hook !

    To be sure I list all the widgets only after the widgets_init hook :

    add_action( 'after_setup_theme', 'hook_the_remove', 1) ;
    function hook_the_remove() {
      remove_action('init', 'wp_widgets_init', 1);
      add_action('widgets_init', 'show_widgets_list', 999999) ;
      other_widgets_init() ;
    }
    function other_widgets_init() {
     	if ( !is_blog_installed() )
    		return;
    
    	do_action('widgets_init');
    }
    function show_widgets_list() {
    	global $wp_widget_factory;
    	foreach($wp_widget_factory->widgets as $name => $w)
    		echo $name . ' - ';
    }

    I also replaced all my calls to the_widget with this to avoid errors messages in case a widget cannot be found :

    function secure_widget($name, $ins, $args) {
    	global $wp_widget_factory;
    	if (array_key_exists($name, $wp_widget_factory->widgets ))
    		the_widget($name, $ins, $args);
    	//else echo "<b>$name</b> is not present";
    }
    function test_widget($name) {
    	global $wp_widget_factory;
    	if (array_key_exists($name, $wp_widget_factory->widgets ))
    		return true ;
    	return false;
    }

    Thanks for your help !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘remove_action not removing action (on wp_widgets_init)’ is closed to new replies.