Created
March 28, 2017 12:06
-
-
Save ariona/cca865eb336686d46c5eae73f96fc60a to your computer and use it in GitHub Desktop.
autosaving post meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery('button').on('click', function(){ | |
... | |
wp.autosave.server.triggerSave(); | |
... | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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