Can anybody please explain in simple words (layman’s terms), what this function actually does.

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

I am learning to code custom post types and I found this function for saving the content of meta boxes.

Thanks in advance 🙂

1
1

That conditional logic first checkes if the constant DOING_AUTOSAVE is defined by using the PHP function defined('SOME_CONSTANT_NAME').

Then the logic checks to see if the state/value of the constant DOING_AUTOSAVE is true, that’s the second part of the conditional right after the && (and).

So if DOING_AUTOSAVE has been defined in the request lifecycle and the state/value of DOING_AUTOSAVE is true then we return which is a means of aborting the logic that is to follow beneath the conditional.

DOING_AUTOSAVE only gets defined and set as true in the wp-admin/includes/post.php file within the wp_autosave() function.

Of course it may be defined elsewhere customly…

  • Learn more about wp_auto_save() here: https://developer.wordpress.org/reference/functions/wp_autosave/
  • Learn more about defined() here: http://php.net/manual/en/function.defined.php

Leave a Reply

Your email address will not be published. Required fields are marked *