Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo code for hiding custom meta with an underscore prefix
<?php
/*
* Plugin Name: Feelings
* Plugin URI: trepmal.com
* Description: Add your feelings to your post
* Version: 0.1
* Author: Kailey Lampert
* Author URI: kaileylampert.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* TextDomain: feelings
* DomainPath:
* Network:
*/
$feelings = new Feelings();
class Feelings {
function __construct() {
add_action( 'edit_form_after_title', array( &$this, 'edit_form_after_title' ) );
add_action( 'save_post', array( &$this, 'save_box' ), 10, 2 );
add_filter( 'is_protected_meta', array( &$this, 'is_protected_meta' ), 10, 2 );
}
function edit_form_after_title() {
wp_nonce_field( 'save_feelings', 'feelings_nonce' );
$feelings = get_post_meta( get_the_ID(), 'feelings', true );
?><div><label><em>Currently feeling&hellip;</em> <input type="text" name="post_feelings" value="<?php echo $feelings; ?>" class="regular-text" /></label></div><br /><?php
}
function save_box( $post_id, $post ) {
if ( ! isset( $_POST['feelings_nonce'] ) ) //make sure our fields have been submitted
return;
if ( ! wp_verify_nonce( $_POST['feelings_nonce'], 'save_feelings' ) ) //verify intent
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) //no auto saving
return;
if ( ! current_user_can( 'edit_post', $post_id ) ) //verify permissions
return;
$feelings = strip_tags( $_POST['post_feelings'] ); //sanitize
if ( empty( $feelings ) )
delete_post_meta( $post_id, 'feelings' ); //clean up
else
update_post_meta( $post_id, 'feelings', $feelings ); //save
}
function is_protected_meta( $protected, $meta_key ) {
if ( 'feelings' == $meta_key )
return true;
return $protected;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment