我创建了一个包含所有现有类别的下拉列表的元框。
页面管理员应该能够从下拉列表中选择一个类别,该类别将由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>
我完全不知道如何在不重定向到其他页面(如“action=bla.php”)的情况下使用所选列表项?!
在过去,我使用简单的输入字段,这些字段可以通过更新/保存页面功能保存,但是如何使用下拉列表来保存呢?
以下是完整的代码:
<?php
/*
Plugin Name: myname » 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ügbaren Einstellungsmöglichkeiten für die Membership Site.</p>
<div id="inhaltselemente">
<div class="membership-box-content">
<div class="membership-box-title">Kategorie fü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\'] ) );
}
?>