Last active
December 16, 2015 06:49
-
-
Save jeffsebring/5394261 to your computer and use it in GitHub Desktop.
Simple Metabox Example w/ 20 Character Validation Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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