Replacing a core WordPress function

Viewed 58

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!!

1 Answers
Related