admin管理员组

文章数量:1332395

I have widget like this

class Test extends WP_Widget
{
    public function __construct()
    {
        parent::__construct(
            'proreco',
            __('Product recommender', 'password_domain'),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    // The widget form (for the backend )
    public function form($instance)
    {
       
        // Set widget defaults
        $defaults = array(
            'email' => '',
            'password' => '',
            'limit' => '',
            'date' => ''
        );

        // Parse current settings with defaults
        extract(wp_parse_args(( array )$instance, $defaults)); ?>

        <p>
            <label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php _e('Email', 'email_domain'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('email')); ?>"
                   name="<?php echo esc_attr($this->get_field_name('email')); ?>" type="email"
                   value="<?php echo esc_attr($email); ?>"/>
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('password')); ?>"><?php _e('Password:', 'password_domain'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('password')); ?>"
                   name="<?php echo esc_attr($this->get_field_name('password')); ?>" type="password"
                   value="<?php echo esc_attr($password); ?>"/>
        </p>
        <p id="TEST"></p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('limit')); ?>"><?php _e('Limit', 'limit_domain'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('limit')); ?>"
                   name="<?php echo esc_attr($this->get_field_name('limit')); ?>" type="number"
                   value=""/>
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('date')); ?>">Date updated: </label>
            <?php
            echo esc_attr($date);
            ?>
        </p>
        <p>
            <button onclick="ExportData()" type="button">Export database</button>
        </p>

        <script>

            function ExportData() {
                var data = {
                    'action': 'export_database', // the name of your PHP function!
                };

                jQuery.ajax({
                    type: "POST",
                    url: ajaxurl,
                    data: data,
                    dataType: 'json',
                    error: function (data) {
                        alertify.notify(data.errors, 'error', 5);
                    }
                });
            }
        </script>

    <?php }


    public function update($new_instance, $old_instance)
    {

    }

    public function widget($args, $instance)
    {

    }
}

You will se that i have ajax function export_database that use to proceed some data. Is possible to update widget properties from this custom ajax function, not just submitting the form? Here is example of my ajax funcion

function export_database()
{   
    // HERE I HAVE TO UPDATE WIDGET PROPERTIES
    wp_die();
}

add_action('wp_ajax_export_database', 'export_database');  // for admins only
add_action('wp_ajax_nopriv_export_database', 'export_database'); // for ALL users

本文标签: pluginsUpdate widget values from ajax function