Skip to content

trepmal.com

  • Home
  • Plugins
  • Github
  • Me on WP.org
  • Donate

Categories

  • WordPress Bits (149)
  • Uncategorized (11)
  • Scripts (5)
  • Tidbits (5)
  • Fun (3)

Tags

  • wordpress (30)
  • administration (16)
  • plugin (9)
  • free download (8)
  • functions.php (8)

Year

  • 2018 (8)
  • 2016 (2)
  • 2015 (4)
  • 2014 (19)
  • 2013 (11)

Function: update_post_meta

Hiding Custom Meta

“Hiding custom fields: DO use the [is_]protected_meta filter. DON’T prefix the key with an underscore.”

— me

This post assumes you’re familiar with building plugins that rely on adding custom post meta.

Alright, so you’ve created a new custom field to store special data for a post, perhaps it looks something like this:

custom-field

Simple enough. So you fill it in and save it, perhaps using code like this:

update_post_meta( $post_id, 'feelings', $_POST['post_feelings'] );

NOTE: Code samples are simplified, don’t forget to validate, sanitize, and escape user data, including your own.

But now you have a problem. “Regular” post meta shows up in the Custom Fields meta box:

custom-fields-box

This can cause several levels of confusion for users, I’ll let you imagine the ways.

One quick fix that you may be accustomed to is to change how you save the post meta. Simply prefix the meta key (“feelings”) with an underscore (“_feelings”). WordPress will automatically, with no additional effort on your part, consider that meta to be protected which will prevent it from being displayed in the Custom Fields meta box.

However, that can present its own problems.

With WordPress automatically hiding it, the meta will remain hidden even if the plugin is removed, preventing it from being edited, deleted, or otherwise managed.

Enter the is_protected_meta filter. Keep your meta unprefixed, and use the filter to protect it.

add_filter( 'is_protected_meta', 'hide_feelings', 10, 2 );
function hide_feelings( $protected, $meta_key ) {
	if ( 'feelings' == $meta_key ) return true;
	return $protected;
}

Just a few lines, and you can protect your non-prefixed meta key thus keeping it from appearing in the Custom Fields meta box.

(See a mini demo plugin)

Posted on August 29, 2013March 13, 2014Categories WordPress BitsTags wordpress4 Comments on Hiding Custom Meta

Related Categories for Pages

Just whipped this up as a demo for someone, and thought it would make a great addition here as well.

The idea was to be able to link a category to a page. So if you have a page about Beavers, you can link it to your blog category called Beaver News.

This code is almost a plugin – it will work with some tweaking, but won’t do anything out-of-the-box.

It adds a category selection dropdown to the Edit Page screen, and saves the value as meta. (As-is, you aren’t allowed to not select a category, with a little hacking that should be fixable).

The code at the end includes a demo of how you might use this page meta on the frontend. This code, however, only outputs it in a meta box on the Edit Page screen.

add_action('add_meta_boxes', 'create_box' );
function create_box() {
    add_meta_box( 'related_category', 'Related Category', 'box_html', 'page', 'side');
}

function box_html() {
    global $post;
    $selected = get_post_meta( $post->ID, '_related_category', true );
    wp_nonce_field( plugin_basename(__FILE__), 'related_cat_nonce' );
    wp_dropdown_categories( array('hide_empty' => false, 'show_count' => true, 'name' => 'related_cat', 'selected' => $selected ) );
}

add_action('save_post', 'save_box' );
function save_box( $post_id ) {

    if ( !wp_verify_nonce( $_POST['related_cat_nonce'], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
    
    if (!isset($_POST['related_cat']))
        return $post_id;
    else {
        $related_cat = $_POST['related_cat'];
        update_post_meta( $post_id, '_related_category', $related_cat, get_post_meta( $post_id, '_related_category', true ) );
    }
    
}

// this just puts the feed in a meta box on the appropriate page
// it can be ripped out and added to the page template
add_action('add_meta_boxes', 'create_box_display' );
function create_box_display() {
    add_meta_box( 'related_category_display', 'Related Category Display', 'box_html_display', 'page', 'side');
}

function box_html_display() {
    global $post;
    $selected = get_post_meta( $post->ID, '_related_category', true );
    $related = get_posts('numberposts=5&category='.$selected);
    foreach($related as $p) {
        echo '<h4>'.$p->post_title.'</h4>';
        echo wpautop($p->post_content);
        echo '<hr />';
    }
}
Posted on April 12, 2011March 1, 2012Categories DemosTags meta boxes, related categoriesLeave a comment on Related Categories for Pages

Add Post Options To “Publish” Meta Box

We often add custom meta boxes to a page or post in order to save specific kinds of data. In some cases, the best place to add those fields really isn’t in some box that gets added to the end of the page. The option needs to be in a bit more prominent location.

Fortunately, it’s possible to add text or form elements to the “Publish” meta box.

add_action( 'post_submitbox_misc_actions', 'article_or_box' );
add_action( 'save_post', 'save_article_or_box' );
function article_or_box() {
    global $post;
    if (get_post_type($post) == 'post') {
        echo '<div class="misc-pub-section misc-pub-section-last" style="border-top: 1px solid #eee;">';
        wp_nonce_field( plugin_basename(__FILE__), 'article_or_box_nonce' );
        $val = get_post_meta( $post->ID, '_article_or_box', true ) ? get_post_meta( $post->ID, '_article_or_box', true ) : 'article';
        echo '<input type="radio" name="article_or_box" id="article_or_box-article" value="article" '.checked($val,'article',false).' /> <label for="article_or_box-article" class="select-it">Article</label><br />';
        echo '<input type="radio" name="article_or_box" id="article_or_box-box" value="box" '.checked($val,'box',false).'/> <label for="article_or_box-box" class="select-it">Box</label>';
        echo '</div>';
    }
}
function save_article_or_box($post_id) {

    if (!isset($_POST['post_type']) )
        return $post_id;

    if ( !wp_verify_nonce( $_POST['article_or_box_nonce'], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    if ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
    
    if (!isset($_POST['article_or_box']))
        return $post_id;
    else {
        $mydata = $_POST['article_or_box'];
        update_post_meta( $post_id, '_article_or_box', $_POST['article_or_box'], get_post_meta( $post_id, '_article_or_box', true ) );
    }

}

What I needed at one time was a way to mark a post that would determine whether it would be displayed in a regular article style, or in a special box style. Nowadays, I can use post formats. But the point is, you could put other options there. Perhaps an expiration date field.

Posted on March 28, 2011May 9, 2014Categories WordPress BitsTags meta box, post meta, posts5 Comments on Add Post Options To “Publish” Meta Box

Shortcode with Content

Here’s a bit of code that will allow you to add a footnote to your post – inline. It’s easy enough to add footnotes manually, sure. But where’s the fun in that?

add_shortcode( 'footnote', 'footnote_shortcode' );
function footnote_shortcode( $atts, $content ) {
	extract(shortcode_atts( array() , $atts));
	global $post;
    update_post_meta( $post->ID, '_footnote', $content, get_post_meta( $post->ID, '_footnote', true ) );
	return ' <a href="#fn_'.$post->ID.'">*</a> ';
}
add_filter( 'the_content', 'append_footnote' ); 
function append_footnote($content) {
	global $post;
	if ( get_post_meta( $post->ID, '_footnote', true ) != '' )
	$content .= '<p id="fn_'.$post->ID.'">* '.get_post_meta( $post->ID, '_footnote', true ) .'</p>';
	
	return $content;
}

A footnote could then be added like this:

The moon is 384,403 km [footnote]238,857 miles[/footnote] from the earth.

There might be a better way to append the footnote to the end – so feel free to investigate and improve.

Posted on March 19, 2011March 1, 2012Categories WordPress BitsTags the contentLeave a comment on Shortcode with Content
Proudly powered by WordPress