Is it possible to add a piece of code snippets inside wordpress standard function ? In my case I am developing a plugin that will trace the exact location of the caller of wp_delete_attachment method in wordpress. I can acheive that by debug_backtrace() but this method must be called inside the method you wish to trace. How can I acheive this without touching/ editing the wordpress core functions ?

function wp_delete_attachment( $post_id, $force_delete = false ) { <-- wordpress core function
global $wpdb;
  //my custom code inside wordpress core function
  $bt = debug_backtrace();
  $caller = array_shift($bt);


It depends what you want to do.

If you look into the function definition, you'll notice some do_action():

For example:

do_action( 'delete_attachment', $post_id );
do_action( 'delete_post', $post_id );
do_action( 'deleted_post', $post_id );

These are points, called hooks in WordPress, where you can actually run your stuff. Hooks will help you to modify the core behaviour without modifying the WordPress core itself.

Let's assume you want to hook into the code before the attachment is deleted, you can then use add_action('delete_attachement', 'yourfunction'); in your plugin or theme's functions.php to run any code you want.

For example:

add_action('delete_attachement', 'yourfunction');

function yourfunction( $post_id ){
  echo 'sample';
  • I updated my question. – Danryl Carpio Apr 28 '16 at 6:42
  • I assume you're adding that only to troubleshoot something? If yes, do it and clean up afterwards. – RRikesh Apr 28 '16 at 6:44
  • i am developing a plugin that will log the location which portion of wordpress deleled media files. – Danryl Carpio Apr 28 '16 at 6:47

You need to hook your function to delete_attachement hook, but you also need to remove some extra functions that are called before calling your hooked action to reach the right caller of wp_delete_attachment().

Here's a possible implementation...

function my_backtrace( $post_id ) {
    $bt     = debug_backtrace();
    array_shift( $bt ); // Remove reference to function
    array_shift( $bt ); // Remove reference to call_user_func_array
    array_shift( $bt ); // Remove reference to do_action
    array_shift( $bt ); // Remove reference to wp_delete_attachment
    $caller = array_shift( $bt ); // This is what called wp_delete_attachment
    die( '<pre>' . json_encode( $caller, JSON_PRETTY_PRINT ) . '</pre>' );
add_action('delete_attachement', 'my_backtrace');

Also you can use json_encode( $caller, JSON_PRETTY_PRINT ) to pretty print your encoded JSON ;)

Lemme know how it works for you...


  • Hi Dan, Mark an answer as accepted when it solves your problem so that other can know that this is resolved ;) – shramee May 2 '16 at 2:52

I solve the problem by hooking my function to delete_attachment hook, then putting the debug trace inside my function and filtering the wp_delete_attachment, by this I can retrieve what file and line number where the wp_delete_attachment is called.

add_action( 'delete_attachment', 'your_function' );
function your_function($postid) {
    $trace = debug_backtrace();

    if(is_array($trace) && !empty($trace)) {        
        foreach ($trace as $trace_value) {
            if(array_key_exists("function", $trace_value)) {
                if($trace_value['function'] == "wp_delete_attachment") {

                    echo $trace_value;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.