Created
July 1, 2013 22:12
-
-
Save alexmansfield/5905079 to your computer and use it in GitHub Desktop.
Code from meta box tutorial: http://themefoundation.com/wordpress-meta-boxes-guide/ (does not include javascritp files).
This file contains hidden or 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
/** | |
* Adds a meta box to the post editing screen | |
*/ | |
function example_custom_meta() { | |
add_meta_box( 'example_meta', 'Example Title', 'example_meta_callback', 'post' ); | |
} // end example_custom_meta() | |
add_action( 'add_meta_boxes', 'example_custom_meta' ); | |
/** | |
* Outputs the content of the metabox | |
*/ | |
function example_meta_callback( $post ) { | |
wp_nonce_field( basename( __FILE__ ), 'example_nonce' ); | |
// TODO: This should be set to '' if get_post_meta( $post->ID ); is empty. | |
$example_stored_meta = get_post_meta( $post->ID ); | |
?> | |
<p> | |
<label for="meta-text" class="example-row-title">Example Text Input</label> | |
<input type="text" name="meta-text" id="meta-text" value="<?php echo $example_stored_meta['meta-text'][0]; ?>" /> | |
</p> | |
<p> | |
<span class="example-row-title">Example Checkbox Input</span> | |
<div class="example-row-content"> | |
<label for="meta-checkbox"> | |
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php checked( $example_stored_meta['meta-checkbox'][0], 'yes' ); ?> /> | |
Checkbox label | |
</label> | |
<label for="meta-checkbox-two"> | |
<input type="checkbox" name="meta-checkbox-two" id="meta-checkbox-two" value="yes" <?php checked( $example_stored_meta['meta-checkbox-two'][0], 'yes' ); ?> /> | |
Another checkbox | |
</label> | |
</div> | |
</p> | |
<p> | |
<span class="example-row-title">Example Radio Buttons</span> | |
<div class="example-row-content"> | |
<label for="meta-radio-one"> | |
<input type="radio" name="meta-radio" id="meta-radio-one" value="radio-one" <?php checked( $example_stored_meta['meta-radio'][0], 'radio-one' ); ?>> | |
Radio Option #1 | |
</label> | |
<label for="meta-radio-two"> | |
<input type="radio" name="meta-radio" id="meta-radio-two" value="radio-two" <?php checked( $example_stored_meta['meta-radio'][0], 'radio-two' ); ?>> | |
Radio Option #2 | |
</label> | |
</div> | |
</p> | |
<p> | |
<label for="meta-select" class="example-row-title">Example Select Input</label> | |
<select name="meta-select" id="meta-select"> | |
<option value="select-one" <?php selected( $example_stored_meta['meta-select'][0], 'select-one' ); ?>>One</option>'; | |
<option value="select-two" <?php selected( $example_stored_meta['meta-select'][0], 'select-two' ); ?>>Two</option>'; | |
</select> | |
</p> | |
<p> | |
<label for="meta-textarea" class="example-row-title">Example Textarea Input</label> | |
<textarea name="meta-textarea" id="meta-textarea"><?php echo $example_stored_meta['meta-textarea'][0]; ?></textarea> | |
</p> | |
<p> | |
<label for="meta-color" class="example-row-title">Color Picker</label> | |
<input name="meta-color" type="text" value="<?php echo $example_stored_meta['meta-color'][0]; ?>" class="meta-color" /> | |
</p> | |
<p> | |
<label for="meta-image" class="example-row-title">Example File Upload</label> | |
<input type="text" name="meta-image" id="meta-image" value="<?php echo $example_stored_meta['meta-image'][0]; ?>" /> | |
<input type="button" id="meta-image-button" class="button" value="Choose or Upload an Image" /> | |
</p> | |
<?php | |
} // end example_meta_callback() | |
/** | |
* Saves the custom meta input | |
*/ | |
function example_meta_save( $post_id ) { | |
// Checks save status | |
$is_autosave = wp_is_post_autosave( $post_id ); | |
$is_revision = wp_is_post_revision( $post_id ); | |
$is_valid_nonce = ( isset( $_POST[ 'example_nonce' ] ) && wp_verify_nonce( $_POST[ 'example_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; | |
// Exits script depending on save status | |
if ( $is_autosave || $is_revision || !$is_valid_nonce ) { | |
return; | |
} | |
// Checks for input and sanitizes/saves if needed | |
if( isset( $_POST[ 'meta-text' ] ) ) { | |
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) ); | |
} | |
// Checks for input and saves | |
if( isset( $_POST[ 'meta-checkbox' ] ) ) { | |
update_post_meta( $post_id, 'meta-checkbox', 'yes' ); | |
} else { | |
update_post_meta( $post_id, 'meta-checkbox', '' ); | |
} | |
// Checks for input and saves | |
if( isset( $_POST[ 'meta-checkbox-two' ] ) ) { | |
update_post_meta( $post_id, 'meta-checkbox-two', 'yes' ); | |
} else { | |
update_post_meta( $post_id, 'meta-checkbox-two', '' ); | |
} | |
// Checks for input and saves if needed | |
if( isset( $_POST[ 'meta-radio' ] ) ) { | |
update_post_meta( $post_id, 'meta-radio', $_POST[ 'meta-radio' ] ); | |
} | |
// Checks for input and saves if needed | |
if( isset( $_POST[ 'meta-select' ] ) ) { | |
update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] ); | |
} | |
// Checks for input and saves if needed | |
if( isset( $_POST[ 'meta-textarea' ] ) ) { | |
update_post_meta( $post_id, 'meta-textarea', $_POST[ 'meta-textarea' ] ); | |
} | |
// Checks for input and saves if needed | |
if( isset( $_POST[ 'meta-color' ] ) ) { | |
update_post_meta( $post_id, 'meta-color', $_POST[ 'meta-color' ] ); | |
} | |
// Checks for input and saves if needed | |
if( isset( $_POST[ 'meta-image' ] ) ) { | |
update_post_meta( $post_id, 'meta-image', $_POST[ 'meta-image' ] ); | |
} | |
} // end example_meta_save() | |
add_action( 'save_post', 'example_meta_save' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment