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.

I created a meta box with a dropdown list which consists of all existing categories.

The administrator of the page should be able to choose one of the categories out of the dropdown list which will be saved by the save/update page function of wordpress.

<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
 <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
 <?php 
  $categories=  get_categories('child_of=10'); 
  foreach ($categories as $category) {
    $option = '<option value="/category/archives/'.$category->category_nicename.'">';
    $option .= $category->cat_name;
    $option .= ' ('.$category->category_count.')';
    $option .= '</option>';
    echo $option;
  }
 ?>
</select>

I have absolutely no idea how to work with the selected list item without redirecting to another page like "action=bla.php"?!

In the past I worked with simple imput fields which can be saved by the update/save page function but how can this be done with a dropdown list?

Here's the complete code:

<?php
/*
Plugin Name: myname &raquo; Membership
Plugin URI: http://www.myname.com
Description: Das membership Plugin.
Author: myname
Version: 2
Author URI: http://www.myname.com
*/

add_action( 'add_meta_boxes', 'membership_meta_box_add' );

function membership_meta_box_add() {
/* 
 * add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
 * id = unique identifier for css or javascript
 * title = displayed at the top of the meta box
 * callback = function which renders the meta box
 * page = will be displayed on "post", "page" or "some_custom_post_type"
 * context = where to place the box: "normal" (below the editor), "side" and "advanced" (column of post editor but further down)
 * priority = from top to bottom: "high", "default", "low"
 * callback_args = will the disregard by us
 */

    add_meta_box( 'myname_membership_box_id', 'myname Membership Einstellungen', 'membership_meta_box_cb', 'page', 'normal', 'high' ); 
}

function membership_meta_box_cb($post) {  

    // $post is already set, and contains an object: the WordPress post
    $values = get_post_custom( $post->ID );

    /***************************************************
     *              Einlesen der bereits gespeicherten Variablen
     ***************************************************/

    $membership_premium_category = isset( $values['membership_premium_category'] ) ? esc_attr($values['membership_premium_category'][0]) : '';  

    // We'll use this nonce field later on when saving.
    wp_nonce_field( 'membership_meta_box_ms_nonce', 'meta_box_ms_nonce' );

    /***************************************************
     *              Konfiguration der Stylesheets
     ***************************************************/   
?>
    <style type="text/css">
        .membership-box-content {padding: 10px 0px 0px 0px; margin: 10px 0px 0px 0px; clear: both;}
        .membership-box-content-switcher {clear: both;}
        .membership-box-title {margin: 0px 0px 5px 0px; font-weight: bold; float: left;}
        .membership-box-dev-note {font-style: italic; float: right; margin-right: 10px;}
        .membership-box-field {margin: 0px 0px 5px 0px; width: 99%;}
        .membership-box-field:hover {background: #F9F9F9;}
        .membership-box-field:focus {background: #F9F9F9;}
        .membership-box-field-small:hover {background: #F9F9F9;}
        .membership-box-field-small:focus {background: #F9F9F9;}
        .membership-box-details {margin: 0px 0px 10px 0px;}
        .extend-button {
            margin: 5px 0px 0px 0px;
            -moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
            -webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
            box-shadow:inset 0px 1px 0px 0px #ffffff;
            background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
            background:-moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
            filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
            background-color:#ededed;
            -moz-border-radius:5px;
            -webkit-border-radius:5px;
            border-radius:5px;
            border:1px solid #dcdcdc;
            display:inline-block;
            color:#777777;
            font-family:Verdana;
            font-size:10px;
            font-weight:normal;
            padding:3px 8px;
            text-decoration:none;
            text-shadow:1px 1px 0px #ffffff;
        }
        .extend-button:hover {
            background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #dfdfdf), color-stop(1, #ededed) );
            background:-moz-linear-gradient( center top, #dfdfdf 5%, #ededed 100% );
            filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#dfdfdf', endColorstr='#ededed');
            background-color:#dfdfdf;
        }
        .extend-button:active {
            position:relative;
            top:1px;
        }
    </style>

    <?
    /***************************************************
     *              Aufbau der Eingabefelder
     ***************************************************/   
    ?>

    <p>Hier finden Sie alle verf&uuml;gbaren Einstellungsm&ouml;glichkeiten f&uuml;r die Membership Site.</p>

    <div id="inhaltselemente">
        <div class="membership-box-content">
            <div class="membership-box-title">Kategorie f&uuml;r Membership Artikel</div><div class="membership-box-dev-note">dev: "membership_premium_category"</div>
            <input type="text" class="membership-box-field" name="membership_premium_category" id="membership_premium_category" value="<?php echo $membership_premium_category; ?>" />
            <div class="membership-box-details">Bitte tragen Sie hier den Namen (nicht Slug/Titelform) der obersten Kategorie aller Premium Artikel ein. <i>Diese Eingabe ist notwendig.</i></div>
        </div>
    </div>

    <!-- WORKING DRAFT -->
    <select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
        <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
        <?php 
        $categories=  get_categories('child_of=10'); 
        foreach ($categories as $category) {
            $option = '<option value="/category/archives/'.$category->category_nicename.'">';
            $option .= $category->cat_name;
            $option .= ' ('.$category->category_count.')';
            $option .= '</option>';
            echo $option;
        }
        ?>
    </select>
    <!-- WORKING DRAFT -->

<?php
} 

    /***************************************************
     *              Speicherfunktion des Plugins
     ***************************************************/

add_action( 'save_post', 'membership_meta_box_save' );

function membership_meta_box_save( $post_id )  
{  
    // Bail if we're doing an auto save  
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn't there, or we can't verify it, bail 
    if( !isset( $_POST['meta_box_ms_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_ms_nonce'], 'membership_meta_box_ms_nonce' ) ) return; 

    // if our current user can't edit this post, bail  
    if( !current_user_can( 'edit_post' ) ) return;  

    // now we can actually save the data  
    $allowed = array('a' => array( 'href' => array()),
                            'object' => '',
                            'iframe' => array( 'width' => array(), 'height' => array(), 'src' => array(), 'frameborder' => array(), 'allowfullscreen' => array())  
    );  

    /***************************************************
     *              Speicherung der einzelnen oben definierten Felder
     ***************************************************/

    // Make sure your data is set before trying to save it
    if( isset( $_POST['membership_premium_category'] ) )
        update_post_meta( $post_id, 'membership_premium_category', wp_kses( $_POST['membership_premium_category'] ) );
} 
?>
share|improve this question
    
paste all of your code –  Bainternet Jan 16 '12 at 15:55
    
I added the complete code and marked the "working draft". The opinion of the file: List all existing categories in a dropdown field and save the selected one like the input field above. –  flowdee Jan 17 '12 at 7:39
    
any suggestions? –  flowdee Jan 24 '12 at 7:19
    
In line 5, I would chance $post_id to $post->ID. Worked for me at least... –  user11771 Apr 20 '12 at 16:05

1 Answer 1

up vote 2 down vote accepted

if you are using it in a metabox then you really don't need this part:

onchange='document.location.href=this.options[this.selectedIndex].value;'

So change the select fields to:

<select name="event-dropdown"> 
    <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
    <?php 
    //get saved data
    $saved_cat = get_post_meta($post_id,'event-dropdown',true);
    $categories=  get_categories('child_of=10'); 
    $select_options = '';
    foreach ($categories as $category) {
        $option = '<option value="'.$category->cat_ID.'">';
        $option .= $category->cat_name;
        $option .= '</option>';
        $select_options .= $option;
    }
    //set saved data as selected
    $select_options = str_replace('value="'.$saved_cat.'"','value="'.$saved_cat.'" selected="selected"',$select_options);
    echo $select_options;
    ?>
</select>

then make sure you save it in your membership_meta_box_save function so add this to the end of it:

if( isset( $_POST['event-dropdown'] ) )
        update_post_meta( $post_id, 'event-dropdown', $_POST['event-dropdown'] );

now the category id will be saved as a meta for each page

share|improve this answer

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.