I want to fetch the value from custom widget input text box to a variable outside the widget class. For example, I have input fields name and age and I am assigning default values of 52 and 9 to them. I want the entered values of name and age in the place of the default values. I tried using global variables as shown in the code below, however, the values were not updating outside the class.

<?php
global $wpdb;
$name = 52;
$age  = 9;

// Creating the widget 
class form_widget extends WP_Widget
{
    function __construct()
    {
        parent::__construct('form_widget', __('FORM Widget', 'form_widget_domain'), array(
            'description' => __('Widget to display a form', 'form_widget_domain')
        ));
    }

    // Creating widget front-end
    public function widget($args, $instance)
    {
        $title = apply_filters('widget_title', $instance['title']);
        $name  = apply_filters('widget_name', $instance['name']);
        $age   = apply_filters('widget_age', $instance['age']);
        echo $args['before_widget'];
        //echo '<div class="widget-text wp_widget_plugin_box" >';
        echo '<div class="form_widget" >';
        if (!empty($title))
            echo $args['before_title'] . $title . $args['after_title'];
        echo __('Forms', 'form_widget_domain');
        echo $args['after_widget'];
        echo '';
    }


    // Widget Backend 
    public function form($instance)
    {
        if (isset($instance['title'])) {
            $title           = $instance['title'];
            $GLOBALS['name'] = $instance['name'];
            $GLOBALS['aget'] = $instance['age'];
        } else {
            $title = __('FORM', 'form_widget_domain');
            $name  = '';
            $age   = '';
        }

        // Widget admin form
?>
<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');
?>" type="text" value="<?php
        echo esc_attr($title);
?>" />
</p>
<p>
<label>NAME</label>
<input class="widefat" id="<?php
        echo $this->get_field_id('name');
?>" name="<?php
        echo $this->get_field_name('name');
?>" type="text" value="<?php
        echo esc_attr($GLOBALS['name']);
?>" />
</p>
<p>
<label>AGE</label>
<input class="widefat" id="<?php
        echo $this->get_field_id('age');
?>" name="<?php
        echo $this->get_field_name('age');
?>" type="text" value="<?php
        echo esc_attr($GLOBALS['aget']);
?>" />
</p>
<div id="mapid"></div>

<?php
        $age = apply_filters('widget_age', $instance['age']);
        echo $age;
        echo $GLOBALS['name'];
        echo $GLOBALS['aget'];
    }

    // Updating widget replacing old instances with new
    public function update($new_instance, $old_instance)
    {
        $instance          = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['name']  = (!empty($new_instance['name'])) ? strip_tags($new_instance['name']) : '';
        $instance['age']   = (!empty($new_instance['age'])) ? strip_tags($new_instance['age']) : '';
        return $instance;
    }
}
function form_load_widget()
{
    register_widget('form_widget');
}
add_action('widgets_init', 'form_load_widget');
?> 

Read more here: How to assiign value of custom widget input text box to a variable?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: