問題描述

我正在建立一個小工具,它需要儲存大約 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 。 – 這裡有三件重要的事情

  1. 如果未設定視窗小工具的值,請確保將值預設為空 array()

  2. <label> name 的形式,請注意我最後附加了一個 []。這告訴 PHP 我正在提交一個這個鍵值的陣列

  3. 將標籤中的核取方塊作為<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_mapintval 來確保所有提交的資料都是有效整數。此外,我使用 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。