Skip to main content
2 of 2
added 1317 characters in body
Rob Myrick
  • 305
  • 4
  • 18

Saving fields in a drop-down in Wordpress

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; 
} 
Rob Myrick
  • 305
  • 4
  • 18