Skip to content

Instantly share code, notes, and snippets.

@trepmal trepmal/registered-meta-bug.php Secret
Last active Dec 22, 2015

Embed
What would you like to do?
<?php
//Plugin Name: Registered Meta Bug
$my_registered_meta = new My_Registered_Meta();
class My_Registered_Meta {
var $key = 'my_registered_meta';
function __construct() {
add_action( 'edit_form_after_editor', array( &$this, 'edit_form_after_editor' ) );
add_action( 'save_post', array( &$this, 'save_box' ), 10, 2 );
// register_meta( 'post', $this->key, array( &$this, 'sanitize_callback' ), '__return_true' ); //true means we're allowed to edit, carry on
register_meta( 'post', $this->key, array( &$this, 'sanitize_callback' ), '__return_false' ); //false means we're not!
// add_filter( 'is_protected_meta', array( &$this, 'is_protected_meta' ), 10, 2 ); // alternative
}
function sanitize_callback( $meta_value, $meta_key, $meta_type ) {
return strip_tags( $meta_value );
}
function edit_form_after_editor() {
wp_nonce_field( 'save_' . $this->key, $this->key . '_nonce' );
$my_meta = get_post_meta( get_the_ID(), $this->key, true );
?><div><label><em>Some Meta:</em> <input type="text" name="<?php echo $this->key; ?>" value="<?php echo $my_meta; ?>" class="regular-text" /></label></div><br /><?php
}
function save_box( $post_id, $post ) {
if ( ! isset( $_POST[ $this->key . '_nonce'] ) ) //make sure our fields have been submitted
return;
if ( ! wp_verify_nonce( $_POST[ $this->key . '_nonce'], 'save_' . $this->key ) ) //verify intent
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) //no auto saving
return;
if ( ! current_user_can( 'edit_post', $post_id ) ) //verify permissions
return;
$my_meta = $_POST[ $this->key ]; //sanitize
if ( empty( $my_meta ) )
delete_post_meta( $post_id, $this->key ); //clean up
else
update_post_meta( $post_id, $this->key, $my_meta ); //save
}
function is_protected_meta( $protected, $meta_key ) {
if ( $this->key == $meta_key )
return true;
return $protected;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.