Skip to content

Instantly share code, notes, and snippets.

@marklindhout
Created September 25, 2012 09:53
Show Gist options
  • Save marklindhout/3780963 to your computer and use it in GitHub Desktop.
Save marklindhout/3780963 to your computer and use it in GitHub Desktop.
WordPress Theme Template: Create a Call to Action meta-box in the backend, and display it in your template.
/*
This code should be added to your 'functions.php'.
Please replace 'mytheme' with a unique name of your choosing.
*/
/* This function creates the meta box HTML */
function mytheme_create_cta_metabox() {
global $post;
// the invisible authorization-field
if (function_exists('wp_nonce_field')) {
wp_nonce_field('mytheme_nonce_action', 'mytheme_noncename');
}
// form fields of the back-end meta-box
echo '<p><strong>'
. __('Teaser Text', 'mytheme' )
. '</strong></p>'
. '<label class="screen-reader-text" for="mytheme_cta_teasertext">'
. __('Teaser Text', 'mytheme' )
. '</label>'
. '<textarea type="text" id="mytheme_cta_teasertext" name="mytheme_cta_teasertext" cols="25" rows="7" style="width:100%;">'
. get_post_meta( $post->ID, 'teasertext', true)
. '</textarea>';
echo '<p><strong>'
. __('Link URL', 'mytheme' )
. '</strong></p>'
. '<label class="screen-reader-text" for="mytheme_cta_linkurl">'
. __('Link URL', 'mytheme' )
. '</label>'
. '<input type="text" id="mytheme_cta_linkurl" name="mytheme_cta_linkurl" value="' . get_post_meta( $post->ID, 'linkurl', true) . '" size="25" style="width:100%;" />';
echo '<p><strong>'
. __('Link Text', 'mytheme' )
. '</strong></p>'
. '<label class="screen-reader-text" for="mytheme_cta_linktext">'
. __('Link Text', 'mytheme' )
. '</label>'
. '<input type="text" id="mytheme_cta_linktext" name="mytheme_cta_linktext" value="' . get_post_meta( $post->ID, 'linktext', true) . '" size="25" style="width:100%;" />';
}
/*
This function hooks the save action and adds our fields to the post
*/
function mytheme_save_postdata( $post_id ) {
// Does the request come from the right place, and does it have the required auth field?
if ( !wp_verify_nonce( $_POST['mytheme_noncename'], 'mytheme_nonce_action' ) ) {
return $post_id;
}
// is this an autosave?
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
// does user have permission?
if ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
// everything is ok, so we save.
update_post_meta($post_id, 'teasertext', $_POST['mytheme_cta_teasertext']);
update_post_meta($post_id, 'linkurl', $_POST['mytheme_cta_linkurl']);
update_post_meta($post_id, 'linktext', $_POST['mytheme_cta_linktext']);
}
add_action('save_post', 'mytheme_save_postdata');
/* Finally, this action adds the meta box to the defined post types */
function mytheme_add_cta_metabox() {
add_meta_box(
'mytheme_cta_metabox',
__( 'Call To Action', 'mytheme' ),
'mytheme_create_cta_metabox',
'post', // post type
'side',
'high'
);
add_meta_box(
'mytheme_cta_metabox',
__( 'Call To Action', 'mytheme' ),
'mytheme_create_cta_metabox',
'page', // post type
'side',
'high'
);
}
add_action('add_meta_boxes', 'mytheme_add_cta_metabox');
/* This function creates something we can use in our templates */
function get_mytheme_cta() {
// retrieve the field values
$linkurl = get_post_custom_values('linkurl', get_the_ID());
$linktext = get_post_custom_values('linktext', get_the_ID());
$teasertext = get_post_custom_values('teasertext', get_the_ID());
// only show the Call to Action when there's content provided for it.
if ( $linkurl[0] || $linkurl[0] || $teasertext[0] ) {
$output .= '<div class=""cta-container"">';
if ( $linkurl[0] && $linktext[0] ) {
$output .= '<a id=""call-to-action"" href=""' . $linkurl[0] . '"">'
. $linktext[0]
. '</a>';
}
if ( $teasertext[0] ) {
$output .= '<div class=""cta-text"">'
. $teasertext[0]
. '</div>';
}
$output .= '</div>';
}
return $output;
}
<?php
// We can use the Call to Action by calling this function:
echo get_mytheme_cta();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment