Skip to content

Instantly share code, notes, and snippets.

@nguyenvanduocit
Last active August 14, 2018 19:15
Show Gist options
  • Save nguyenvanduocit/e492c8b0918198ffd62c335284339c54 to your computer and use it in GitHub Desktop.
Save nguyenvanduocit/e492c8b0918198ffd62c335284339c54 to your computer and use it in GitHub Desktop.
check_admin_referer('update-post_' . $post_id);
$post_id = edit_post();
// Session cookie flag that the post was saved
if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
}
redirect_post($post_id); // Send user on their way while we keep working
exit();
function hcf_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
$field_list = [
'hcf_author',
'hcf_published_date',
'hcf_price',
];
foreach ( $field_list as $fieldName ) {
if ( array_key_exists( $fieldName, $_POST ) ) {
update_post_meta(
$post_id,
$fieldName,
sanitize_text_field( $_POST[ $fieldName ] )
);
}
}
}
add_action( 'save_post', 'hcf_save' );
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
...
if ( $create_in_db ) {
$post_id = wp_insert_post( array(
'post_title' => __( 'Auto Draft' ),
'post_type' => $post_type,
'post_status' => 'auto-draft' ));
...
function wp_insert_post( $postarr, $wp_error = false ) {
...
do_action( 'save_post', $post_ID, $post, $update );
...
return $post_ID;
}
function hcf_save( $post_id ) {
...
if (get_post_status($post_id) === 'auto-draft') {
return;
}
...
}
function wp_autosave( $post_data ) {
// Back-compat
if ( ! defined( 'DOING_AUTOSAVE' ) )
define( 'DOING_AUTOSAVE', true );
...
if ( ! wp_check_post_lock( $post->ID )
&& get_current_user_id() == $post->post_author
&& ( 'auto-draft' == $post->post_status
|| 'draft' == $post->post_status ) ) {
// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
return edit_post( wp_slash( $post_data ) );
} else {
// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revsion.
return wp_create_post_autosave( wp_slash( $post_data ) );
}
}
function edit_post( $post_data = null ) {
...
update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
$success = wp_update_post( $post_data );
...
return $post_ID;
}
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
// Make sure meta is added to the post, not a revision.
if ( $the_post = wp_is_post_revision($post_id) )
$post_id = $the_post;
$updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
if ( $updated ) {
wp_cache_set( 'last_changed', microtime(), 'posts' );
}
return $updated;
}
function hcf_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if (get_post_status($post_id) === 'auto-draft') {
return;
}
$field_list = [
'hcf_author',
'hcf_published_date',
'hcf_price',
];
foreach ( $field_list as $fieldName ) {
if ( array_key_exists( $fieldName, $_POST ) ) {
update_post_meta(
$post_id,
$fieldName,
sanitize_text_field( $_POST[ $fieldName ] )
);
}
}
}
add_action( 'save_post', 'hcf_save' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment