Skip to content

Instantly share code, notes, and snippets.

@jeffsebring
Last active December 16, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsebring/5394261 to your computer and use it in GitHub Desktop.
Save jeffsebring/5394261 to your computer and use it in GitHub Desktop.
Simple Metabox Example w/ 20 Character Validation Error
<?php
/**
* Simple Metabox Example
*
* Validates string is under 20 characters
*
* Copyright (C) 2013 Jeff Sebring <http://jeffsebring.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Safety First
defined( 'ABSPATH' ) || die;
// Add Metabox
add_action( 'add_meta_boxes', 'js_add_metabox_example' );
function js_add_metabox_example()
{
global $post;
$front_page = get_option( 'page_on_front' );
if ( $post->ID === $front_page )
add_meta_box( 'js_metabox_example', 'Metabox Example', 'js_metabox_example_box', 'page', 'normal', 'high' );
}
// Meta Box Input
function js_metabox_example_box( $post )
{
$saved = get_post_meta( $post->ID, 'js_metabox_example', true );
echo '<input type="hidden" name="js_metabox_example_nonce" value="', wp_create_nonce( basename( __FILE__ ) ), '" />';
echo '<label for="js_metabox_example">Meta Data<label>';
echo '<input type="text" id="js_metabox_example" name="js_metabox_example" value="' . $saved . '" size="25" />';
}
// Save Metabox Data
add_action( 'save_post', 'js_metabox_example_save' );
function js_metabox_example_save( $post_id )
{
// Check User Capability
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
// Check Nonce
if ( ! wp_verify_nonce( $_POST[ 'js_metabox_example_nonce' ], basename( __FILE__ ) ) )
return;
// Check Autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST[ 'js_metabox_example' ] ) && strlen( $_POST[ 'js_metabox_example' ] ) > 20 ) :
update_option( 'js_metabox_example_error', true );
return;
else :
update_option( 'js_metabox_example_error', false );
endif;
update_post_meta(
$_POST['post_ID' ],
'js_metabox_example',
sanitize_text_field( $_POST['js_metabox_example'] )
);
}
// Check for Failed Validation
if ( get_option( 'js_metabox_example_error' ) )
add_action( 'admin_notices', 'js_metabox_example_error' );
// Validation Error Message
function js_metabox_example_error()
{
echo '<div id="message" class="error below-h2"><p>Meta Data must be at least 20 characters</p></div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment