Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Demonstrates how to separate logic for cleaner code and maintainability when working with WordPress meta boxes.
<?php
add_action( 'add_meta_boxes', 'acme_custom_meta_box' );
/**
* Defines a custom post meta box that displays directly below the
* post editor in the WordPress dashboard.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*/
function acme_custom_meta_box() {
add_meta_box(
'acme-custom-meta-box',
'Acme Meta Box',
'acme_render_meta_box',
'post',
'normal',
'core'
);
}
<?php
add_filter( 'the_content', 'acme_display_the_content' );
/**
* Determines whether or not the content for the current post should be displayed.
*
* If the post meta data for 'acme-hide-content' has been set, then the content
* will be replaced by a default message; otherwise, the default content
* will be displayed.
*
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
* @param string The content of the post to be displayed.
* @return string The content, if the associated meta data is not set; a single message, otherwise.
*/
function acme_display_the_content( $content ) {
$should_hide_content = get_post_meta( get_the_ID(), 'acme-hide-content', TRUE );
if ( 'on' === $should_hide_content ) {
$content = __( 'This content is not available.' );
}
return $content;
}
<?php
/**
* Renders the post meta box checkbox option.
*
* The meta box gives the user the ability to toggle the visibility of the content
* on the front-end of the blog. This file provides the markup for the meta box
* as well as the security nonce for ensuring the data can be saved by the
* approprite user.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*
* @package WordPress
*/
?>
<div class="meta-options">
<!-- We include the `input` and the text within the label so the user can click the label to trigger the checkbox. -->
<label for="acme-hide-content">
<input type="checkbox" id="acme-hide-content" name="acme-hide-content" <?php checked( get_post_meta( get_the_ID(), 'acme-hide-content', true ), 'on', true ); ?> />
Hide the content from displaying on this post?
</label>
<?php
// We must define a nonce for security purposes. This will be validated in Acme_Meta_Box_Helper.
wp_nonce_field( 'acme-hide-content', 'acme-hide-content-nonce' );
?>
</div><!-- .meta-options -->
<?php
/**
* Defines functionality for serializing the option saved in the Acme Post Meta Box
*
* The Acme Post Meta Box provides the user the option to toggle the display of the
* post content on the front-end of the blog.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*
* @package WordPress
*/
/**
* Defines functionality for serializing the option saved in the Acme Post Meta Box.
*
* The Acme Post Meta Box provides the user the option to toggle the display of the
* post content on the front-end of the blog. This class helps to register the
* necessary hooks and validation functions to make sure the data can be saved
* (or removed) based on the user input.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*
*/
class Acme_Meta_Box_Helper {
/**
* Initializes the class by defining the callback for the save_post hook.
*/
public function __construct() {
add_action( 'save_post', array( $this, 'save_post' ) );
}
/**
* If the current user can save the meta data associated with the specified post, then
* updates the meta data based on the specified value.
*
* Specifically, if the user has clicked on the option to hide the post content,
* then the post meta data will include that information; otherwise, the meta data
* will be deleted.
*
* @param integer $post_id The current post being saved
*/
public function save_post( $post_id ) {
if( $this->user_can_save( $post_id, 'acme-hide-content-nonce', 'acme-hide-content' ) ) {
if ( isset( $_POST['acme-hide-content'] ) ) {
update_post_meta( $post_id, 'acme-hide-content', $_POST['acme-hide-content'] );
} else {
delete_post_meta( $post_id, 'acme-hide-content' );
}
}
}
/**
* Verifies that the user who is currently logged in has permission to save the data
* from the meta box to the database.
*
* @param integer $post_id The current post being saved.
* @param string $nonce The number used once to identify the serialization value
* @param string $action The source of the action of the nonce being used
* @return boolean True if the user can save the information
*/
private function user_can_save( $post_id, $nonce, $action ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], $action ) );
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
}
<?php
/**
* Renders the content to be displayed in the "Acme Meta Box."
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*/
function acme_render_meta_box() {
echo "TODO";
}
<?php
/**
* Renders the content to be displayed in the "Acme Meta Box" by requiring
* the file that is responsible for rendering the content.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*/
function acme_render_meta_box() {
require_once get_template_directory() . '/acme-meta-box-display.php';
}
<?php
add_action( 'init', 'setup_acme_meta_box_helper' );
/**
* Imports and instantiates the Acme Meta Box Helper which is used to make sure
* that the post meta data can be saved that's represented by the post meta box.
*
* @link http://tommcfarlin.com/wordpress-meta-boxes-aiming-for-simplicity/
*/
function setup_acme_meta_box_helper() {
/**
* Includes the Acme Meta Box Helper class which is used to help serialize information in the
* save_post action of the post serialization life cycle.
*/
require_once get_template_directory() . '/class-acme-meta-box-helper.php';
// Instantiate the helper so that it performs all necessary work when the save action fires.
$helper = new Acme_Meta_Box_Helper();
}
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.