Making custom field’s ‘Name’ available in the dropdown list by default in a theme?

My single.php file has the following code:

<?php // Set and display custom field
    $intro_image = get_post_meta($post->ID, 'Intro Image', true); ?>
    <div class="block-1">
        <img src="https://wordpress.stackexchange.com/questions/9637/<?php echo $intro_image; ?>" alt="" />
    </div> <?php
?>

The user will have to type Intro Image (once) in the ‘Name’ field in order to see it next time in the drop-down list. I think it would be better if the user can see ‘Intro Image’ right after installing my WordPress theme (I feel that the user should select options that are available, not write them down themselves).

I’m not sure if what I’m saying make sense. But how to have those ‘Names’ available in the drop-down list before writing them?

1 Answer
1

your best option is to create a custom meta box with-in your theme and then the user will have it no matter if he typed it once before.

// Hook into WordPress
add_action( 'admin_init', 'add_custom_metabox' );
add_action( 'save_post', 'save_custom_intro_image' );

/**
 * Add meta box
 */
function add_custom_metabox() {
    add_meta_box( 'custom-metabox', __( 'Intro Image' ), 'intro_image_custom_metabox', 'post', 'side', 'high' );
}

/**
 * Display the metabox
 */
function intro_image_custom_metabox() {
    global $post;
    $introimage = get_post_meta( $post->ID, 'intro_image', true );
        ?>
    <p><label for="intro_image">Intro Image:<br />
        <input id="siteurl" size="37" name="intro_image" value="<?php if( $introimage ) { echo $introimage; } ?>" /></label></p>
    <?php
}

/**
 * Process the custom metabox fields
 */
function save_custom_intro_image( $post_id ) {
    global $post;   

    if( $_POST ) {
        update_post_meta( $post->ID, 'intro_image', $_POST['intro_image'] );
    }
}

Hope This Helps

Leave a Comment