Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"),
);
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 ( attribute_escape($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 "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="'.attribute_escape($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 ( !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;
}
$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);
elseif($data == "")
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/JtCT
This works if there is an Infographics category (as shown), 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
the_content();
$image_url = get_post_meta($post->ID, 'image_url', TRUE);
$link_text = get_post_meta($post->ID, 'link_text', TRUE);
?>
<p><a href="<?php the_permalink(); ?>"> <img alt="" border="0" src="<?php echo $image_url; ?>"></a></p>
<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($name_of_site) { echo $link_text; } else { echo get_option('blogname'); }; ?> With This Graphic&lt;/a&gt;</textarea></p>
... 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(); } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment