Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnbocook/6738600 to your computer and use it in GitHub Desktop.
Save johnbocook/6738600 to your computer and use it in GitHub Desktop.
<?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');
<?php
add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
<?php
$meta_values = get_post_meta($post_id, $key, $single);
?>
<?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
}
?>
<?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');
<?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>';
}
<?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