🥳Remove an action from init in wordpress (ok)

https://stackoverflow.com/questions/47094179/remove-an-action-from-init-in-wordpress

PARENT THEME FUNCTION:

add_action('init', 'portfolio_register');

function portfolio_register()
{
//post type definition
}

CHILD THEME FUNCTION: This is what I tried but it is not working.

function child_remove_parent_function() {
    remove_action( 'init', 'portfolio_register' );
}
add_action( 'init', 'child_remove_parent_function', 15 );

Đọc thêm

How to Remove Default WordPress Widgets from Admin Screen

This is tested on WordPress 4.2 and 5.2. So it should work on any version in between. It may also work on lower versions as well but i have not tested.

The default widgets on WordPress admin panel are too many. I am talking about the widgets on appearance > widgets page. I had to remove these on a recent project. This guide shows you how to do that.

There are 2 scenarios here – remove all default widgets or remove only specific widgets.

Remove all default widgets

Let us remove all default widgets. Put the following code on your functions.php file.

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

Here wp_widgets_init is the action hook which registers all default wordpress widgets. By removing this hook with the code above we are preventing WordPress from registering all default widgets.

Hold on! We are not done yet!!

If you try the code above, the widgets will be removed, but you will find that now you can not add any custom widget! Why so? Because, the custom widgets are added through an action hook widgets_init which in turn depends on wp_widgets_init to fire. Removing wp_widgets_init removes widgets_init as well, hence you are unable to add custom widgets.

To overcome this problem, add another line below remove_action as follows

remove_action( 'init', 'wp_widgets_init', 1 ); // original line
add_action( 'init', function() { do_action( 'widgets_init' ); }, 1 ); // added line

What this code does is – it removes the default widgets through first line and then immediately adds the action hook widgets_init back through the second line, allowing you to add any custom widget.

Note that the code above takes care of all future updates as well – which means if new default widgets are added to WordPress core, those will be removed as well without additional code.

Remove specific widget(s)

To remove a specific default widget, you will need to know the name of the widget class, and then unregister that widget using its class name .

For example, to remove the default Gallery widget add the following code to functions.php

// remove default "Gallery" widget 
function wdv_remove_gallery_widget() {
	unregister_widget('WP_Widget_Media_Gallery');	
}

add_action('widgets_init', 'wdv_remove_gallery_widget', 11);

Here 11 denotes the action priority. Which means the order with which this particular action will be executed. Why 11? Because default wordpress widgets are added with a priority of 10, hence in order to remove them you need to set an order greater than 10.

To remove multiple widgets you can use the following code. To make it easy i have listed all default widgets (as available under current WordPress verion 5.2). Again, this goes to functions.php!

// remove all default widgets
function wdv_remove_default_widgets() {
	//unregister_widget('WP_Widget_Media_Gallery');
	//unregister_widget('WP_Widget_Pages');
	//unregister_widget('WP_Widget_Calendar');
	//unregister_widget('WP_Widget_Archives');
	//unregister_widget('WP_Widget_Links');
	//unregister_widget('WP_Widget_Meta');
	//unregister_widget('WP_Widget_Search');
	//unregister_widget('WP_Widget_Text');
	//unregister_widget('WP_Widget_Categories');
	//unregister_widget('WP_Widget_Recent_Posts');
	//unregister_widget('WP_Widget_Recent_Comments');
	//unregister_widget('WP_Widget_RSS');
	//unregister_widget('WP_Widget_Tag_Cloud');
	//unregister_widget('WP_Nav_Menu_Widget');
	//unregister_widget('Twenty_Eleven_Ephemera_Widget');
	//unregister_widget('WP_Widget_Media_Audio');
	//unregister_widget('WP_Widget_Media_Image');
	//unregister_widget('WP_Widget_Media_Video');
	//unregister_widget('WP_Widget_Custom_HTML');
}

add_action('widgets_init', 'wdv_remove_default_widgets', 11);

Just un-comment the specific lines to remove respective widgets.

Did that work for you? Let me know in the comments below.

Last updated