• I’m trying to use an instance or option from the widget backend settings outside the class but can’t seem to get it to work.

    I’ve omitted some of the code to simplify what I mean:

    <?php
    
    ##trying to use variable here!
    echo $instance['variableiwant'];
    
       class my_class extends WP_Widget {
    
            ## get variable from the backend form settings of widget
            function form($instance) {
               $title = ! empty( $instance['variableiwant'] ) ? $instance['title'] : __( 'variableiwant' );
            }
    
            ##update save of widget
            function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance['variableiwant'] = strip_tags($new_instance[ 'variableiwant']);
                return $instance;
            }
    
            function widget($args, $instance) {
            }

    I can use the variable perfectly fine inside the class and its functions but is there special API I need to use to pull the variable outside the class as the code:

    echo $instance['variableiwant'];

    Just returns 0 or zero

Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    If this is a widget you’re writing, you could add a function outside the widget, and calls to the function inside the widget, to give you access to the $instance variable. For example:

    function save_instance($instance, $type = 'widget') {
    	(do something with $instance based on $type)
    }
    
    class my_class extends WP_Widget {
    	function form($instance) {
    		$title = ! empty( $instance['variableiwant'] ) ? $instance['title'] : __( 'variableiwant' );
    		save_instance($instance, 'form');
    	}
    
    	function update($new_instance, $old_instance) {
    		$instance = $old_instance;
    		$instance['variableiwant'] = strip_tags($new_instance[ 'variableiwant']);
    		save_instance($instance, 'update');
    		return $instance;
    	}
    
    	function widget($args, $instance) {
    		save_instance($instance);
    	}
    }
    Thread Starter paulsimonrough

    (@paulsimonrough)

    The variable I’m trying to get comes from the backend widget settings.

    In the code see ‘variableiwant’

    Thanks.

    To access a value from outside a class it needs to be made available either by setting it as a public variable, or by setting up a return function. As an example…

    Public variable:

    class MyClass {
    
        public $my_var;
    
    }

    Return function:

    class MyClass {
    
        protected $my_var;
    
        function get_my_var () {
            return $this->my_var;
        }
    
    }
    Dion

    (@diondesigns)

    The variable I’m trying to get comes from the backend widget settings.

    In the code see ‘variableiwant’

    Thanks.

    You’re welcome. ;

    That variable is only available if one of the three main widget functions is called. What I provided was a way for an external function you create to be informed when that variable becomes available. It could have been done with add_action() and do_action(), but those functions carry a lot of overhead compared to what I provided.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Use wordpress widget option (instance) outside the class’ is closed to new replies.