• Resolved Guido

    (@guido07111975)


    Hi again,

    I have a widget with wp_options name: my_widget.

    Within my widget file I can retrieve a value like this:

    if ( !empty( $instance['title'] ) ) {
    	echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
    }

    But how do I retrieve this value outside the widget file?

    I’ve read something about get_option, but don’t understand how to get a single value from my my_widget option, such as: $instance[‘title’]

    Thanks in advance!!

    Guido

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Hi Guido,
    When you do something like:

    $widget_data = get_option('_widget_my_widget');
    var_dump( $widget_data );

    you will see an array of data for all instances of that widget. Each instance corresponds to a numeric key, so to get the ‘title’ value you’d do something like $title = $widget_data[0]['title'];

    The exact code depends on the actual array structure, there could be more levels involved. Which numeric key to use at the first level is the tricky part. If there is only one instance, it’ll be [0]. If more than one, you can hopefully identify the correct instance in the var_dump. As long as one does not rearrange widgets on the admin screen the array key will not change.

    If you need to find the proper array key for whatever the active widget is, even after one has rearranged the admin screen, and without manually examining the data array, I’m afraid I don’t know ??

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    My goal was simple: I have a plugin to add a signup form in sidebar with a field called Phone.

    I’ve added a checkbox in my widget file so user can check this to hide this field:

    <label for="<?php echo $this->get_field_id('phone'); ?>"><?php _e('Hide Phone Field', 'my-text-domain'); ?>:</label>
    <input class="checkbox" type="checkbox" <?php checked( '1', $phone ); ?> id="<?php echo $this->get_field_id( 'phone' ); ?>" name="<?php echo $this->get_field_name( 'phone' ); ?>" value="1">

    Value stored in wp_options table is empty or ‘1’.

    Signup form is located in a separate file, and that’s why I posted this question.

    Will look into your solution ??

    Guido

    Moderator bcworkz

    (@bcworkz)

    It sounds like your signup form can’t know which instance to pay attention to, so you don’t really know which array key to use. I’ve a couple more thoughts.

    It used to be that widgets could not be multi-instance. Then it became an option, then finally the default. I think there may still be a way to create a single instance widget, though I wouldn’t know how to do that. Looking through the WP_Widget source might give a clue.

    If all else fails, store the value separately in the options table from within the update() widget method. Your form and the widget’s form() method can use this common location for the value, independent of the widget’s default saving of options.

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    Found it. First call the whole option:

    $options = get_option( 'widget_my_widget' );

    After this get the single instance from that option and work with that:

    $options['phone']

    Guido

    Moderator bcworkz

    (@bcworkz)

    Nice!

    It’s curious that what initially seems like a complex issue often turns out to have a simple solution ??

    nenontwerp

    (@nenontwerp)

    Hi bcworkz, guido,

    The numeric key you want to get refers to the id of the widget. To get the id of the widget use the class you’r working with for your widget. It extends ‘WP_Widget’ which has some variables. To get a variable from the class start with $this-> and then the name of the variable. To get the id of the widget: $this->id.
    I’m building a widget to display a cf7-form (contact form 7). Because a widget can be placed twice in 1 sidebar I need a dynamic array key (=widget-id) to get the right data for my widget/form. In my case: the widget name is ‘nen_cf7’, and the generated id’s for my widgets are ‘nen_cf7-2’ and ‘nen_cf7-3’. To get the right key:

    
    $widget_id_full     = $this->id;  // widget name extended with the id
    $widget_id_arr      = explode('-', $widget_id_full); // split in array bits
    $widget_id          = $widget_id_arr[ count($widget_id_arr)-1 ]; // get the last one to get the key
    
    $widget_data        = get_option( 'widget_nen_cf7' ); // get all widget data
    $form_id            = $widget_data[ $widget_id ]['form_id'] ; // get my admin content
    

    To assign different titles to each widget I’m using do_action() with a dynamic action hook.

    
    do_action('before_widget_'.$widget_id_full);
    do_action('after_widget_'.$widget_id_full);
    

    In your functions.php

    
    add_action('before_widget_nen_cf7-2','paste_my_header_one');
    function paste_my_header_one(){
        echo '<h2>Header widget one</h2>';
    }
    add_action('before_widget_nen_cf7-3','paste_my_header_two');
    function paste_my_header_two(){
        echo '<h2>Header widget two</h2>';
    }
    
    Moderator bcworkz

    (@bcworkz)

    nenontwerp – Nice! Thanks for sharing.

    Thread Starter Guido

    (@guido07111975)

    Hi ‘nenontwerp’,

    I thought I have found a solution (see my previous reply about get_option), but with today’s knowledge I know the code below isn’t gonna work with (multiple) widgets. The option name stays the same, but the option can contain multiple widgets, so multiple ‘Phone’ instances.

    
    $options = get_option( 'widget_my_widget' );
    $options['phone']
    

    I run into something simular this week, and bcworkz is helping me there as well.

    @nenontwerp: I will look into your solution, thanks.

    Guido

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get widget instance with wp_options’ is closed to new replies.