Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a core WordPress function

Tags:

wordpress

I am trying to replace the core WP function wp_delete attachment. In my functions.php file I am adding the following code:

add_action( 'init', 'remove_my_action' );

function remove_my_action(){
    remove_action( 'delete_attachment', 'wp_delete_attachment' );
    add_filter('delete_attachment','wp_delete_attachment',10,2); 
}

And then the copy of the replaced function goes here with edited code.

This does not work. I get the error: Cannot redeclare wp_delete_attachment(). I've tried a number of other methods, but can't get it to work.

The bottom line is that I need to add some code to the middle of wp_delete_attachment function. If I can replace that function with my version somehow or add the code to the existing function without editing the actual code in the wp-includes/post.php file (so that version updates don't override my code), I would be satisfied with either. How can this be done? All the options I found through questions have not solved the issue. Thanks!!

like image 939
Jedediah Zilberberg Avatar asked Jun 20 '26 02:06

Jedediah Zilberberg


1 Answers

You'll need to name your copy of wp_delete_attachment to a unique name. Perhaps namespace it with your site name like function my_site_wp_delete_attachment().

Also, I believe you'll need to use add_action instead of add_filter.

remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_action( 'delete_attachment', 'my_site_wp_delete_attachment'); 
like image 86
Jared Cobb Avatar answered Jun 25 '26 21:06

Jared Cobb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!