Simple WordPress Custom Field Template

This is a simple way to add custom fields to your posts without much hassle. Download the code and change the example fields to your own specific needs and voila, the custom fields are located on your post. In this example, let's say that you (or your client) review movies. In addition to the text of the review, you want to have three associated custom fields that show up without having to use the dropdown each time to select the custom field. There are custom field plugins that let you dynamically create these, but for this case, there is only one template and this solves the problem quickly. The three custom fields defined below are: Movie Rating (_movie_rating) Movie Running Time (_movie_running_time) Movie Director (_movie_director)
<?php
/*
Plugin Name: Single & Simple Custom Fields Template
Plugin URI: 
Description: This plugin makes a defined set of custom fields for a post
Version: 0.1
Author: Matthew Price
Author URI: http://www.matthewaprice.com
License: GPL2
*/      
        
function sscft_display_movie_custom_fields_on_editor() {        
global $post;
?>
<style type="text/css">
p.details label { float: left; width: 150px; line-height: 26px;}
p.details input[type=text] { width: 400px; }
</style>

// Here is where you display them on the post.  This is where you add your own fields

<p class="details">
        <label for="_movie_rating">Movie Rating</label>
        <input type="text" name="_movie_rating" id="_movie_rating" value="<?php echo get_post_meta($post->ID,'_movie_rating',true); ?>">
</p>

<p class="details">
        <label for="_movie_running_time">Movie Running Time</label>
        <input type="text" name="_movie_running_time" id="_movie_running_time" value="<?php echo get_post_meta($post->ID,'_movie_running_time',true); ?>">
</p>

<p class="details">
        <label for="_movie_director">Movie Director</label>
        <input type="text" name="_movie_director" id="_movie_director" value="<?php echo get_post_meta($post->ID,'_movie_director',true); ?>">
</p>

<?php

}

function sscft_add_movie_details_to_post( $page, $context ) {
// At the end of this i have these fields set to "normal and core"  this will place the fields below the content editor.  There are other options such as "side and high" or "side and core."  these will move the box to the right hand column
        if ( ( 'post' === $page ) && 'advanced' === $context )
                add_meta_box( 'movie-custom-fields', 'Movie Details', 'sscft_display_movie_custom_fields_on_editor', $page, 'normal', 'core' );
}                       

function sscft_save_movie_details($postID) {
global $wpdb;
// Here is where you save the custom fields to the database.  just add your own fields here instead of the movie example

update_post_meta($postID, '_movie_rating', $_POST['movie_rating']); 
update_post_meta($postID, '_movie_running_time', $_POST['movie_running_time']); 
update_post_meta($postID, '_movie_director', $_POST['movie_director']); 
}
 
add_action( 'do_meta_boxes', 'sscft_add_movie_details_to_post', 10, 2 );
add_action( 'save_post', 'sscft_save_movie_details');

?>
Post a comment





Real Time Web Analytics ^