Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created August 1, 2012 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/3229180 to your computer and use it in GitHub Desktop.
Save wpsmith/3229180 to your computer and use it in GitHub Desktop.
Bail out if running an autosave, ajax or a cron for use in save_post
<?php
/**
* Bail out if running an autosave, ajax or a cron
* or user does not have proper capabilities
*
* @return bool|int True if should bail, post ID if lacking cap
*/
function wps_bail( $post_id = '', $cap = 'edit_post' ) {
/** Bail out if running an autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return true;
/** Bail out if running an ajax */
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return true;
/** Bail out if running a cron */
if ( defined( 'DOING_CRON' ) && DOING_CRON )
return true;
/** Bail out if user does not have permissions */
if ( '' != $post_id && ! current_user_can( $cap, $post_id ) )
return $post_id;
return false;
}
<?php
add_action( 'save_post', 'wps_save_worksheet_settings', 10, 2 );
/**
* Save settings post meta fields added to Sermon metaboxes.
*
* @since 1.0.0
*
* @param int $post_id The post ID
* @param object $post Current post object data
*/
function wps_save_worksheet_settings( $post_id, $post ) {
/** Bail out if running an autosave, ajax or a cron */
if ( wps_bail( $post_id ) ) return;
// do stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment