Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@germanny
Created October 3, 2012 14:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save germanny/3827168 to your computer and use it in GitHub Desktop.
Save germanny/3827168 to your computer and use it in GitHub Desktop.
Infographic standard form
<?php
$ig_post_custom_fields = array (
"infographic" => array(
"name" => "is_infographic",
"std" => "",
"title" => "Is this an infographic post?",
"description" => "If you check this box, we'll add a Pin It share option.",
"type" => "checkbox"),
"image_url" => array(
"name" => "image_url",
"std" => "",
"title" => "Image url",
"description" => "Image for infographic posts.",
"type" => "input"),
"link_text" => array(
"name" => "link_text",
"std" => "",
"title" => "Link text",
"description" => "Optional! Enter the name of the site as you wish it to appear after the From:. If you enter nothing, we'll use the name of the blog from the General Settings tab.",
"type" => "input"),
"content_placement" => array(
"name" => "content_placement",
"std" => "",
"options" => array("Content Above Everything", "Content Below Infographic", "Content Below Everything"),
"title" => "Content Placement",
"description" => "Do you want the content to be above the infographic, below the infographic but above the embed code, or below everything?",
"type" => "radio"),
);
function ig_post_custom_fields() {
echo '<p style="margin-bottom:10px;"><b>Don\'t put your infographic code in the box above. Instead, complete this form and we\'ll put it together for you.</b></p><hr>';
global $post, $ig_post_custom_fields;
foreach($ig_post_custom_fields as $ig) {
$ig_value = stripslashes(get_post_meta($post->ID, $ig['name'], true));
if($ig_value == "")
$ig_value = $ig['std'];
echo '<p style="margin-bottom:10px;">';
echo '<input type="hidden" name="'.$ig['name'].'_box" id="'.$ig['name'].'_box" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
switch ( $ig[ 'type' ] ) {
case "checkbox":
if ( esc_attr($ig_value) === "true" ){
$checked = "checked=\"checked\"";
} else {
$checked = "";
}
echo'<label for="'.$ig['name'].'"><input type="'.$ig['type'].'" name="'.$ig['name'].'" id="'.$ig['name'].'" class="checkbox" value="true" '.$checked.'> <strong>'.$ig['title'].'</strong> <small><em>'.$ig['description'].'</em></small></label><br />';
break;
case "radio":
echo '<p><strong>'.$ig['title'].'</strong> <small><em>'.$ig['description'].'</em></small><br>';
$radio_options = $ig['options'];
foreach ($radio_options as $radio_option) {
$radio_option_combined = strtolower(str_replace(' ', '-', $radio_option));
if ( esc_attr($ig_value) === $radio_option_combined ){
$checked = "checked=\"checked\"";
} else {
$checked = "";
}
echo'<label for="'.$radio_option_combined.'"><input type="'.$ig['type'].'" name="'.$ig['name'].'" id="'.$radio_option_combined.'" class="radio" value="'.$radio_option_combined.'" '.$checked.'> <strong>'.$radio_option.'</strong></label>&nbsp;&nbsp;&nbsp;&nbsp;';
}
echo '</p>';
break;
case "input":
echo'<label for="'.$ig['name'].'"><strong>'.$ig['title'].'</strong> <small><em>'.$ig['description'].'</em></small></label><br><input type="'.$ig['type'].'" name="'.$ig['name'].'" id="'.$ig['name'].'" value="'.esc_attr($ig_value).'" style="width:98%;"><br>';
break;
}
echo '</p>';
}
}
function create_post_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Infographic Image', 'ig_post_custom_fields', 'post', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $ig_post_custom_fields;
foreach($ig_post_custom_fields as $ig) {
// Verify
if(isset($_POST[$ig['name'].'_box'])) {
if ( !wp_verify_nonce( $_POST[$ig['name'].'_box'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
}
//skip auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if(isset($_POST[$ig['name']])) {
$data = $_POST[$ig['name']];
if(get_post_meta($post_id, $ig['name']) == "")
add_post_meta($post_id, $ig['name'], $data, true);
elseif($data != get_post_meta($post_id, $ig['name'], true))
update_post_meta($post_id, $ig['name'], $data);
} else {
delete_post_meta($post_id, $ig['name'], get_post_meta($post_id, $ig['name'], true));
}
}
}
add_action('admin_menu', 'create_post_meta_box');
add_action('save_post', 'save_postdata');
?>
<?php
/* POST CUSTOM FIELDS UI
********************************************************************************************************************************/
include_once('functions/fn-post-custom-fields-infographic.php');
?>
Here's what the backend looks like:
http://cloud.jen.gs/LNqx
This works if there is an Infographics category, or if the user checks the Is this an infographic? box (in case the user doesn't want to have a special category for it).
<?php
/**
* The Template for displaying Infographic posts.
*/
get_header();
?>
... Your stuff here.
<?php
$image_url = get_post_meta($post->ID, 'image_url', TRUE);
$link_text = get_post_meta($post->ID, 'link_text', TRUE);
$content_placement = get_post_meta($post->ID, 'content_placement', TRUE);
if( $content_placement == 'content-above-everything' || $content_placement == '' ) { the_content(); }
?>
<p><a href="<?php the_permalink(); ?>"> <img alt="" border="0" src="<?php echo $image_url; ?>"></a></p>
<?php if( $content_placement == 'content-below-infographic' ) { the_content(); } ?>
<h3>Embed the image above on your site</h3>
<p><textarea>&lt;a href=&quot;<?php the_permalink(); ?>&quot;&gt;&lt;img src=&quot;<?php echo $image_url; ?>&quot; alt=&quot;<?php the_title(); ?>&quot; width=&quot;500&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;Presented By: &lt;a href=&quot;<?php the_permalink(); ?>&quot;&gt;Please Include Attribution to <?php if($link_text) { echo $link_text; } else { echo get_option('blogname'); }; ?> With This Graphic&lt;/a&gt;</textarea></p>
<?php if( $content_placement == 'content-below-everything' ) { the_content(); } ?>
... Finish up your stuff
<?php get_footer(); ?>
<?php
/**
* The Template for displaying all single posts.
*/
if(get_post_meta($post->ID, 'is_infographic', TRUE) || in_category('infographic')) {
include('single-infographic.php');
} else {
get_header();
?>
... Your Stuff here
<?php get_footer(); } ?>
@jimmynotjim
Copy link

Looks good to me. I'm using it on dev.criminaljusticeusa.com and it's working great. This will definitely make posting infographics easier for authors and the content staff.

@germanny
Copy link
Author

Debugged various errors and now this one should be error-free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment