Skip to content

Instantly share code, notes, and snippets.

@ariona
Created March 28, 2017 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariona/cca865eb336686d46c5eae73f96fc60a to your computer and use it in GitHub Desktop.
Save ariona/cca865eb336686d46c5eae73f96fc60a to your computer and use it in GitHub Desktop.
autosaving post meta
jQuery('button').on('click', function(){
...
wp.autosave.server.triggerSave();
...
})
<?php
function print_switch_mode_button() {
...
?>
<span class="hidden">
<input id="lorem" name="_nama_post_meta" type="hidden" value="<?php echo $current_value; ?>">
<?php wp_nonce_field( basename(__FILE__), '_nama_post_meta_nonce' ); ?>
</span>
<?php
...
}
add_action( 'media_buttons', 'print_switch_mode_button', 999);
function save_post_meta( $post_id ) {
if ( ! isset( $_POST['_nama_post_meta_nonce'] ) || ! wp_verify_nonce( $_POST['_nama_post_meta_nonce'], basename(__FILE__) ) ){
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if( empty( $_POST ) ) {
return;
}
if( ! isset( $_POST['_nama_post_meta'] ) )
$_POST['_nama_post_meta'] = '';
update_post_meta( $post_id , $_POST['_nama_post_meta']);
}
add_action( 'save_post', 'save_post_meta' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment