0
votes

WordPress 3.2.1

I'm creating separate page templates, however, there are some page templates that should have default custom fields. That is, if someone created a new page of template type "Contact Us", it should by default have custom fields:

Success Message Email To Phone Number

etc.

Right now, the only way I can think of knocking this out is by having the admin add those custom fields to the page, and then fill them in. However, this isn't the best method for basically giving an admin a "turn-key" type of feature, i.e., they create a page with a specific template and just fill in the fields.

Thanks guys!

1
Do you want these fields to filled at the frontend or does the admin fills them in so the values appear at the frontend? - janw
The admin would fill those values in the WordPress administrative area. I'd just like the template to auto show the fields that are available based on the template that is being created/edited. - StephenPAdams

1 Answers

2
votes

The following will add 1 extra meta box to your (insert-edit) page.
In it you can add custom fields to save.

It does not switch depending on the selected template.
I would recomend using javascript to show/hide fields to do this. Based on ID of the <p>

If you get the meta box working with ALL possible fields you can show I'm willing to help with the javascript to hide and show based on the template.

GL

<?php    
add_action( 'add_meta_boxes', 'carrousel_build' );
function carrousel_build()
{
    add_meta_box('carrousel', 'Carrousel opties', 'carrousel_options', 'page',/*show on 'pages'*/ 'normal', 'high');
}

//this will add a meta box with custom fields
function carrousel_options ($post)
{ ?>
<p>
    Description
</p>
<p id="field1_container">
    <label for="field1">Custom field 1</label>
    <br/>
    <input type="text" id="field1" name="field1" value="<?php echo get_post_meta($post->ID, 'field1', true) ?>" size="25" />
</p>
<p id="field2_container">
    <label for="field2">Custom field 2</label>
    <br/>
    <input type="text" id="field2" name="field2" value="<?php echo get_post_meta($post->ID, 'field2', true) ?>" size="25" />
</p>

<?php }

//save the values of the meta box
add_action( 'save_post', 'post_save' );
function post_save($post_id)
{
    // Check permissions
    if (isset ($_POST['post_type']) && 'carrousel' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ))
    {
        return;
    }

    if (isset($_POST['field1'])) {
        $subtitle = $_POST['field1'];
        update_post_meta($post_id, 'field1', $subtitle);
    }

    if (isset($_POST['field2']))
    {
        $link = $_POST['field2'];
        update_post_meta($post_id, 'field2', $link);
    }
}