Last active
March 13, 2018 04:53
-
-
Save paulund/5903828 to your computer and use it in GitHub Desktop.
Add custom post meta data input boxes to the WordPress post pages.
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 | |
function add_embed_tweet_meta_box() { | |
add_meta_box( | |
'paulund_embed_tweet_meta_box', // $id | |
'Post Embed Tweets Meta Box', // $title | |
'show_embed_tweet_meta_box', // $callback | |
'post', // $page | |
'normal', // $context | |
'high'); // $priority | |
} | |
add_action('add_meta_boxes', 'add_embed_tweet_meta_box'); |
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 | |
add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); |
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 | |
$meta_values = get_post_meta($post_id, $key, $single); | |
?> |
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 | |
$twitter_embed = get_post_meta($post->ID, "twitter_embed", true); | |
if($twitter_embed != ""){ | |
?> | |
What's being said on Twitter | |
<?php | |
echo $twitter_embed; | |
?> | |
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script><?php | |
} | |
?> |
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 | |
/************************************************************************** | |
* Save the meta fields on save of the post | |
**************************************************************************/ | |
function save_embed_tweet_meta($post_id) { | |
// verify nonce | |
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) | |
return $post_id; | |
// check autosave | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) | |
return $post_id; | |
// check permissions | |
if ('page' == $_POST['post_type']) { | |
if (!current_user_can('edit_page', $post_id)) | |
return $post_id; | |
} elseif (!current_user_can('edit_post', $post_id)) { | |
return $post_id; | |
} | |
$old = get_post_meta($post_id, "twitter_embed", true); | |
$new = $_POST["twitter_embed"]; | |
if ($new && $new != $old) { | |
update_post_meta($post_id, "twitter_embed", $new); | |
} elseif ('' == $new && $old) { | |
delete_post_meta($post_id, "twitter_embed", $old); | |
} | |
} | |
add_action('save_post', 'save_embed_tweet_meta'); |
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 | |
function show_embed_tweet_meta_box() { | |
global $post; | |
$meta = get_post_meta($post->ID, 'twitter_embed', true); | |
// Use nonce for verification | |
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; | |
echo '<table class="form-table">'; | |
// begin a table row with | |
echo '<tr> | |
<th><label for="twitter_embed">Twitter Embed</label></th> | |
<td><textarea name="twitter_embed" id="twitter_embed" cols="60" rows="4">'.$meta.'</textarea> | |
<span class="description">Use to embed tweets on your post.</span></td> | |
</tr>'; | |
echo '</table>'; | |
} |
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 | |
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) | |
return false; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment