問題描述
我正在創建一個小工具,它需要存儲大約 10 個 ID 。現在我使用以下字段方法將每個 ID 存儲在單獨的字段中。它將每個字段的數據存儲在 wordpress 中。使用數組可以將 wordpress 中所有字段的數據存儲在 wordpress 中一行嗎?
<input
class="widefat"
id="<?php echo $this->get_field_id('item1_id'); ?>"
name="<?php echo $this->get_field_name('item1_id'); ?>"
value="<?php echo $instance['item1_id']; ?>"
/>
<input
class="widefat"
id="<?php echo $this->get_field_id('item2_id'); ?>"
name="<?php echo $this->get_field_name('item2_id'); ?>"
value="<?php echo $instance['item2_id']; ?>"
/>
最佳解決方案
你必須收集與這個名稱相同的多個字段…
name="collect[1]"
name="collect[2]"
… 並調整您的窗口小工具的邏輯。
這是一個非常簡單的演示小工具:
<?php # -*- coding: utf-8 -*-
/* Plugin Name: Store Options as array */
add_action( 'widgets_init', array ( 'T5_Array_Options_Widget', 'register' ) );
class T5_Array_Options_Widget extends WP_Widget
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct( strtolower( __CLASS__ ), 'Array Demo' );
}
/**
* Echo the settings update form
*
* @param array $instance Current settings
*/
public function form( $instance )
{
$title = isset ( $instance['title'] ) ? $instance['title'] : '';
$title = esc_attr( $title );
printf(
'<p><label for="%1$s">%2$s</label><br />
<input type="text" name="%3$s" id="%1$s" value="%4$s" class="widefat"></p>',
$this->get_field_id( 'title' ),
'Title',
$this->get_field_name( 'title' ),
$title
);
$fields = isset ( $instance['fields'] ) ? $instance['fields'] : array();
$field_num = count( $fields );
$fields[ $field_num + 1 ] = '';
$fields_html = array();
$fields_counter = 0;
foreach ( $fields as $name => $value )
{
$fields_html[] = sprintf(
'<input type="text" name="%1$s[%2$s]" value="%3$s" class="widefat">',
$this->get_field_name( 'fields' ),
$fields_counter,
esc_attr( $value )
);
$fields_counter += 1;
}
print 'Fields<br />' . join( '<br />', $fields_html );
}
/**
* Renders the output.
*
* @see WP_Widget::widget()
*/
public function widget( $args, $instance )
{
print $args['before_widget']
. $args['before_title']
. apply_filters( 'widget_title', $instance['title'] )
. $args['after_title']
. join( '<br />', $instance['fields'] )
. $args['after_widget'];
}
/**
* Prepares the content. Not.
*
* @param array $new_instance New content
* @param array $old_instance Old content
* @return array New content
*/
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = esc_html( $new_instance['title'] );
$instance['fields'] = array();
if ( isset ( $new_instance['fields'] ) )
{
foreach ( $new_instance['fields'] as $value )
{
if ( '' !== trim( $value ) )
$instance['fields'][] = $value;
}
}
return $instance;
}
/**
* Tell WP we want to use this widget.
*
* @wp-hook widgets_init
* @return void
*/
public static function register()
{
register_widget( __CLASS__ );
}
}
Backend
Frontend
次佳解決方案
如果您需要編號的字段,上述答案是很好的。在我的情況下,我沒有。我有一個包含選項的窗口小工具,允許用户選擇要在窗口小工具中使用的任何數量的類別。
這是我的小工具 form 。 – 這裏有三件重要的事情
-
如果未設置窗口小工具的值,請確保將值默認為空
array() -
以
<label>name的形式,請注意我最後附加了一個[]。這告訴 PHP 我正在提交一個這個鍵值的數組 -
將標籤中的複選框作為
<label><input type="checkbox" ...></label>。 – 我們的每個複選框將不具有唯一的id屬性,因此<label>for屬性將不起作用。我們可以生成唯一的 ID,但這是一個麻煩。如果您只是將標籤包圍在輸入周圍,則標籤可以正確關聯,無需連接for+id的麻煩
現在的代碼
public function form($instance) {
$title = isset($instance['title']) ? $instance['title'] : '';
$categories = isset($instance['categories']) ? $instance['categories'] : array();
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>">
<?php _e( 'Title:' ) ?>
</label>
<input class="widefat"
id="<?php echo $this->get_field_id('title') ?>"
name="<?php echo $this->get_field_name('title') ?>"
value="<?php echo $title ?>" />
</p>
<p>Categories</p>
<ul>
<?php foreach (get_categories() as $category): ?>
<li>
<label>
<input type="checkbox"
class="checkbox"
name="<?php echo $this->get_field_name('categories') ?>[]"
value="<?php echo $category->cat_ID ?>"
<?php checked(in_array($category->cat_ID, $categories)) ?> />
<?php echo $category->name ?>
</label>
</li>
<?php endforeach ?>
</ul>
<?php
}
這是我的更新功能
我有興趣將數組中的類別 ID 保存為數字,所以我使用 array_map 與 intval 來確保所有提交的數據都是有效整數。此外,我使用 array_filter 刪除任何無效的提交。
// @param array $a - the new instance options
// @param arram $b - the old instance options
public function update($a, $b) {
return array(
'title' => isset($a['title']) ? strip_tags($a['title']) : $b['title'],
'categories' => isset($a['categories']) ? array_filter(array_map(function($id) { return intval($id); }, (array) $a['categories'])) : (array) $b['title']
);
}
描述這個 WordPress 的東西是特別有挑戰性的。如果你有任何問題,我會很樂意詳細説明。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。


