Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Below is code that successfully displays a drop-down menu of page templates in my Wordpress theme. I'm using this menu in a custom post type, and don't quite understand how to label it in order to make it save the chosen value after the user "updates".

The value is, in fact, being saved to the database and displaying correctly to the table, it's just not maintaining the chosen value after update.

I've seen some other examples of this, but my drop-down is based on a PHP function all in one line. Any suggestions would be great appreciated.

<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label>

<select name="page_template" id="page_template">

<option value='page_template'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown($template); ?>
</select>

HERE IS THE UPDATED CALLBACK FUNCTION THAT IS SUPPOSED TO DISPLAY THE DROPDOWN:

 // The Callback for Page Template Assignmets Metabox 
function pagetemplate() { 
global $post; 
// Noncename needed to verify where the data originated 
echo '<input type="hidden" name="pagetemplatemeta_noncename" id="pagetemplate_noncename" value="' . 
wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; 
// Get the title data if its already been entered 
?>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label> 
<select name="page_template" id="page_template"> 
<option value='default'><?php _e('Default Template'); ?></option> 
<?php template_dropdown($template); ?> 
</select> 

<?php

} 


//Creates the page template dropdown menu display and functionality
function template_dropdown( $default = '' ) { 
$templates = get_page_templates(); 
ksort( $templates ); 
foreach (array_keys( $templates ) as $template ) 
    : if ( $default == $templates[$template] ) 
        $selected = " selected='selected'"; 
    else 
        $selected = ''; 
echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>"; 
endforeach; 
} 
share|improve this question
    
Where are you defining $template? –  Stephen Harris Jun 10 '12 at 15:10
    
Hi Stephen, I updated the code based on the answer below, but still having problems. Apparently using the core code from WP is not doing the trick. –  Rob Myrick Jun 10 '12 at 16:58
add comment

2 Answers

This is the code from the WordPress core code - line 580:

<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label>
<select name="page_template" id="page_template">
    <option value='default'><?php _e('Default Template'); ?></option>
    <?php page_template_dropdown($template); ?>
</select>

The only thing different from your code is <option value='default'>. So I don't think your code is the problem.

Also the code for page_template_dropdown isn't complicated:

function page_template_dropdown( $default = '' ) {
    $templates = get_page_templates();
    ksort( $templates );
    foreach (array_keys( $templates ) as $template )
        : if ( $default == $templates[$template] )
            $selected = " selected='selected'";
        else
            $selected = '';
    echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
    endforeach;
}

So I'd hazard a guess that there's something wrong with your $template variable.

share|improve this answer
    
Yeah I've been studying both of those pieces of code. I don't really understand how they work together. I've added the function to my functions.php file, and nothing changed. Also, for saving the selection, am I supposed to use page_template or default as my label later on when I use this code to save the data: $events_meta['page_template'] = $_POST['page_template']; –  Rob Myrick Jun 10 '12 at 16:44
    
I added the newest update to my functions.php for you to see. Again, I'm getting the dropdown to display properly and even post to the table, but it's not holding its value when saved by the user. –  Rob Myrick Jun 10 '12 at 16:56
    
If as you say your default template gets updated in the database correctly - then when you refresh the page then you should have the text of the selected template in the $template variable. Have you tried printing out the $template variable to see what's in it? –  icc97 Jun 10 '12 at 21:02
    
Also, in the database, the DB updates based on "page_template" label, not the $template variable. But the drop-down does, in fact, change based on the $template variable. I could post my full functions file for you - since I'm testing it only has this custom post type functionality within it, so not messy –  Rob Myrick Jun 10 '12 at 21:35
    
$events_meta['page_template'] = $_POST['page_template']; –  Rob Myrick Jun 10 '12 at 22:27
show 2 more comments

The code, it would seem works fine. You originally used:

   page_template_dropdown($template);

which is perfectly valid. But $template is supposed to contain the current posts' template. But nowhere do you set it to be so. In the core code, above the drop-down, they have:

   $template = !empty($post->page_template) ? $post->page_template : false;

which does this. How you retrieve the current posts' template will depend on how you've stored it.

Also, I would use page_template_dropdown rather than your own-defined, but identical function template_dropdown (reduces code duplication :) )

Side note:

In your metabox callback, pagetemplate, you do:

function pagetemplate(){
   global $post;
    ....
}

It would be better to use the fact that the $post object is passed as an argument:

function pagetemplate( $post ){
    ....
}
share|improve this answer
    
Hi Stephen, thanks for the help. I definitely need to clean some things up in my code. I still think there's an issue with the option not saving to the selected value upon the update. So I'm guessing somehow the $selected value is not being utilized correctly. Maybe I'm way off, but this would be the only thing stopping the dropdown from reverting to the value after the update, right? –  Rob Myrick Jun 10 '12 at 17:42
    
As @Stephen Harris says - you're not setting the $template value. Try putting this at the top of your function: $template = $post->page_template; –  icc97 Jun 10 '12 at 21:10
    
icc97, still no good, would you like to take a look at this for me (only if you have the time). I've been working on this almost all day. –  Rob Myrick Jun 10 '12 at 21:29
    
@RobMyrick - the template is produced by the page_template_dropdown and it selects which ever template is passed by $template. So you need to retrieve the current post's template. I don't know how you've saved the template, so I can't saw how. Post templates are supported out of the box for non-pages so I'm not surprised $post->page_template doesn't work. How are you saving the user's input? –  Stephen Harris Jun 10 '12 at 22:22
    
@RobMyrick - I meant the dropdown is produced by.... and also I meant to say that post templates aren't supported out of the box for non-pages. It's late :) –  Stephen Harris Jun 10 '12 at 22:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.