使用 select2 为 wordpress 插件选项页面选择多个产品

火雾

我正在尝试使用select2. 该列表使用getproductsearch我之前执行的这个 ajax 操作出现,但我未能保存它我之前为 for 做过这个功能post-metaproduct-category但在 plugin 失败了option page我不确定我做错了什么。

请帮忙。

 class FeatureSale {
    private $feature_sale_options;

    public function __construct() {
        add_action( 'admin_menu', array( $this, 'feature_sale_add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'feature_sale_page_init' ) );
    }

    public function feature_sale_add_plugin_page() {
        add_submenu_page(
        'exclutips-settings',
            'Feature & Sale', // page_title
            'Feature & Sale', // menu_title
            'manage_options', // capability
            'feature-sale', // menu_slug
            array( $this, 'feature_sale_create_admin_page' ) // function
        );
    }

    public function feature_sale_create_admin_page() {
        $this->feature_sale_options = get_option( 'feature_sale_option_name' ); ?>

        <div class="wrap">
        <div class="catbox-area-admin" style="width: 500px;background: #fff;padding: 27px 50px;">
            <h2>Feature & Sale</h2>
            <p></p>
            <?php settings_errors(); ?>

            <form method="post" action="options.php">
                <?php
                    settings_fields( 'feature_sale_option_group' );
                    do_settings_sections( 'feature-sale-admin' );
                    submit_button();
                ?>
            </form>
        </div>
        </div>
    <?php }

    public function feature_sale_page_init() {
        register_setting(
            'feature_sale_option_group', // option_group
            'feature_sale_option_name', // option_name
            array( $this, 'feature_sale_sanitize' ) // sanitize_callback
        );

        add_settings_section(
            'feature_sale_setting_section', // id
            '', // title
            array( $this, 'feature_sale_section_info' ), // callback
            'feature-sale-admin' // page
        );

        add_settings_field(
            'vpm_sale_product', // id
            'VPM Sale Product', // title
            array( $this, 'vpm_sale_product_callback' ), // callback
            'feature-sale-admin', // page
            'feature_sale_setting_section' // section
        );

        add_settings_field(
            'vpm_featured_product', // id
            'VPM Featured Product', // title
            array( $this, 'vpm_featured_product_callback' ), // callback
            'feature-sale-admin', // page
            'feature_sale_setting_section' // section
        );
    }

    public function feature_sale_sanitize($input) {
        $sanitary_values = array();
        if ( isset( $input['vpm_sale_product'] ) ) {
            $sanitary_values['vpm_sale_product'] = $input['vpm_sale_product'];
        }

        if ( isset( $input['vpm_featured_product'] ) ) {
            $sanitary_values['vpm_featured_product'] = $input['vpm_featured_product'];
        }

        return $sanitary_values;
    }

    public function feature_sale_section_info() {

    }

    //Output the HTML for the metabox.

    public function vpm_sale_product_callback() {
        global $post;
        // Nonce field to validate form request came from current site
        wp_nonce_field( basename( __FILE__ ), 'vpm_sale_product_nonce' );

        $html = '';

        // always array because we have added [] to our <select> name attribute
        $feature_sale_options = get_option( 'feature_sale_option_name' ); // Array of All Options
        $vpm_sale_product = $feature_sale_options['vpm_sale_product'];

        $html .= '<p><select id="vpm_sale_product" name="vpm_sale_product[]" multiple="multiple" style="width:99%;max-width:25em;">';

        if( $vpm_sale_product ) {
            foreach( $vpm_sale_product as $post_id ) {
                $title = get_the_title( $post_id );
                // if the post title is too long, truncate it and add "..." at the end
                $title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
                $html .=  '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
            }
        }
        $html .= '</select></p>';

        echo $html;
        //==========================================
    }


     //* Output the HTML for the metabox.

    public function vpm_featured_product_callback() {
        global $post;
        // Nonce field to validate form request came from current site
        wp_nonce_field( basename( __FILE__ ), 'vpm_featured_product_nonce' );

        $html = '';

        // always array because we have added [] to our <select> name attribute
        $feature_sale_options = get_option( 'feature_sale_option_name' ); // Array of All Options
        $vpm_featured_product = $feature_sale_options['vpm_featured_product'];

        $html .= '<p><select id="vpm_featured_product" name="vpm_featured_product[]" multiple="multiple" style="width:99%;max-width:25em;">';

        if( $vpm_featured_product ) {
            foreach( $vpm_featured_product as $post_id ) {
                $title = get_the_title( $post_id );
                // if the post title is too long, truncate it and add "..." at the end
                $title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
                $html .=  '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
            }
        }
        $html .= '</select></p>';

        echo $html;
        echo $vpm_featured_product;
        //==========================================
        ?>
        <script>
        (function ($) {
            'use strict';
        $(function () {
            //--------------------------------------------------------------------------
            // multiple select with AJAX search
            $('#vpm_featured_product,#vpm_sale_product').select2({
                ajax: {
                        url: ajaxurl, // AJAX URL is predefined in WordPress admin
                        dataType: 'json',
                        delay: 250, // delay in ms while typing when to perform a AJAX search
                        data: function (params) {
                            return {
                                q: params.term, // search query
                                action: 'getproductsearch' // AJAX action for admin-ajax.php
                            };
                        },
                        processResults: function( data ) {
                        var options = [];
                        if ( data ) {

                            // data is the array of arrays, and each of them contains ID and the Label of the option
                            $.each( data, function( index, text ) { // do not forget that "index" is just auto incremented value
                                options.push( { id: text[0], text: text[1]  } );
                            });

                        }
                        return {
                            results: options
                        };
                    },
                    cache: true
                },
                minimumInputLength: 3 // the minimum of symbols to input before perform a search
            });

            //----------------------------------------------------------------------------------------
        });
    })(jQuery);
    </script>   
        <?php 
    }

}
if ( is_admin() )
    $feature_sale = new FeatureSale();
莎莉 CJ

问题是这些select标签没有使用正确的name值:

<select id="vpm_sale_product" name="vpm_sale_product[]"...>
<select id="vpm_featured_product" name="vpm_featured_product[]"...>

正确的name值是:(根据您的register_setting()电话)

<select id="vpm_sale_product" name="feature_sale_option_name[vpm_sale_product][]"...>
<select id="vpm_featured_product" name="feature_sale_option_name[vpm_featured_product][]"...>

并且虽然在纠正上述问题后,表单数据将被正确保存,但传递给的第五个参数(即菜单/页面slug)add_submenu_page()应该与传递给add_settings_section()的第四个参数匹配add_settings_field(),并且传递给do_settings_sections()在您的add_submenu_page()调用中,菜单/页面标记为feature-sale,但对于其他三个功能,您将其设置为feature-sale-admin

并使用wp_nonce_field()建议,但在你的情况下,这是没有必要的,因为你提交wp-admin/options.php当然,除非您想在 WordPress 保存选项之前进行检查。但即便如此,我相信一个 nonce 字段是好的。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 jQuery,为 Select2 选择下拉列表赋值不起作用

来自分类Dev

使用Ajax的jQuery Select2插件

来自分类Dev

在 WordPress 中的 select2 上设置多个值

来自分类Dev

有没有办法使用 select2 为下拉列表中的每个选项添加描述?

来自分类Dev

使用jQuery以编程方式选择select2选项

来自分类Dev

如何使用选择的选项克隆select2?

来自分类Dev

wordpress / WooCommerce 为珠宝定制产品插件

来自分类Dev

如何检查选择列表是否为多个?Select2 JQuery、JavaScript

来自分类Dev

使用jQuery Select2搜索时隐藏选项

来自分类Dev

Select2不使用我的templateResults或templateSelection选项

来自分类Dev

使用YII Framework的Select2 Ajax选项

来自分类Dev

如何使用“ Select2”预先选择值

来自分类Dev

如何在 Select2 上使用动态选择?

来自分类Dev

使用jQuery禁用多个select2字段

来自分类Dev

使用 jQuery select2 设置多个值

来自分类Dev

使用select2中的选项文本(不使用值)选择下拉菜单

来自分类Dev

使用select2中的选项文本(不使用值)选择下拉菜单

来自分类Dev

如何从多个m2m字段中为queryset(select2)准备列表

来自分类Dev

jQuery select2插件,默认情况下使用ajax选择加载

来自分类Dev

select2使用ajax加载数据无法选择任何选项

来自分类Dev

Select2 4.0.0 AJAX-使用Tab选择突出显示的选项

来自分类Dev

使用select2的动态选择选项不起作用

来自分类Dev

如何使用从Select2下拉列表中选择的选项来触发jQuery?

来自分类Dev

为选项页面实现WordPress设置类

来自分类Dev

select2 json将所有选项显示为.text()而不是被选中

来自分类Dev

从mongo集合中的数组为Select2选项创建标签对象

来自分类Dev

打开时如何检查变量值是否为select2中的选项之一?

来自分类Dev

替换select2选项?

来自分类Dev

Select2搜索选项

Related 相关文章

  1. 1

    使用 jQuery,为 Select2 选择下拉列表赋值不起作用

  2. 2

    使用Ajax的jQuery Select2插件

  3. 3

    在 WordPress 中的 select2 上设置多个值

  4. 4

    有没有办法使用 select2 为下拉列表中的每个选项添加描述?

  5. 5

    使用jQuery以编程方式选择select2选项

  6. 6

    如何使用选择的选项克隆select2?

  7. 7

    wordpress / WooCommerce 为珠宝定制产品插件

  8. 8

    如何检查选择列表是否为多个?Select2 JQuery、JavaScript

  9. 9

    使用jQuery Select2搜索时隐藏选项

  10. 10

    Select2不使用我的templateResults或templateSelection选项

  11. 11

    使用YII Framework的Select2 Ajax选项

  12. 12

    如何使用“ Select2”预先选择值

  13. 13

    如何在 Select2 上使用动态选择?

  14. 14

    使用jQuery禁用多个select2字段

  15. 15

    使用 jQuery select2 设置多个值

  16. 16

    使用select2中的选项文本(不使用值)选择下拉菜单

  17. 17

    使用select2中的选项文本(不使用值)选择下拉菜单

  18. 18

    如何从多个m2m字段中为queryset(select2)准备列表

  19. 19

    jQuery select2插件,默认情况下使用ajax选择加载

  20. 20

    select2使用ajax加载数据无法选择任何选项

  21. 21

    Select2 4.0.0 AJAX-使用Tab选择突出显示的选项

  22. 22

    使用select2的动态选择选项不起作用

  23. 23

    如何使用从Select2下拉列表中选择的选项来触发jQuery?

  24. 24

    为选项页面实现WordPress设置类

  25. 25

    select2 json将所有选项显示为.text()而不是被选中

  26. 26

    从mongo集合中的数组为Select2选项创建标签对象

  27. 27

    打开时如何检查变量值是否为select2中的选项之一?

  28. 28

    替换select2选项?

  29. 29

    Select2搜索选项

热门标签

归档