Ask your WordPress questions! Pay money and get answers fast! (more info)

retrieve the output of a widget to use in a plugin WordPress

  • SOLVED

In functions.php I create a widget like this :
<?php
class slh_widget extends WP_Widget {

function __construct() {
parent::__construct(
'slh_widget',
__('CFlinks, adapted by SLH', 'slh_widget_domain'),
array( 'description' => __( 'Un unique widget pour TOUTES les publications', 'slh_widget_domain' ), )
);
}

public function widget( $args, $instance ) {

foreach ($instance['slh-check'] as $nicename => $id_slh) {
$title = apply_filters( 'widget_title', $nicename );
echo $args['before_widget'];
echo $args['before_title'] . $title . $args['after_title'];
echo cflk_get_links($id_slh);
echo $args['after_widget'];
}

}

// Widget Backend
public function form( $instance ) {


global $wpdb;
$cflk_list = $wpdb->get_results($wpdb->prepare("SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE %s",'cfl-%'));
$form_data = array();
foreach ($cflk_list as $cflk) {
$options = maybe_unserialize(maybe_unserialize($cflk->option_value));
$push = array('option_name' => $cflk->option_name, 'nicename' => $options['nicename']);
array_push($form_data,$push);
}
$list = '';

foreach ($form_data as $data => $info) {
if (is_array($instance['slh-check'])) {
if (in_array(htmlspecialchars($info['option_name']), $instance['slh-check'])) {
$checked = ' checked="checked"';
} else {
$checked = "";
}
}
?>
<label for="<?php echo $this->get_field_id( 'slh-check' ) . '[' . $info['nicename'] . ']'; ?>">
<input class="slh_input" type="checkbox" id="<?php echo $this->get_field_id( 'slh-check' ) . '[' . $info['nicename'] . ']'; ?>" name="<?php echo $this->get_field_name( 'slh-check' ) . '[' . $info['nicename'] . ']'; ?>" value="<?php print (htmlspecialchars($info['option_name'])); ?>"<?php echo $checked ?>> <?php print (htmlspecialchars($info['nicename'])); ?>
</label><br>
<?php

$slh = cflk_get_links_data(htmlspecialchars($info['option_name']));
$title = apply_filters( 'widget_title', $slh['nicename'] );
$list .= '<div class="' . htmlspecialchars($info['option_name']) . '" style="display:none"><h3 class="h3_slh" style="margin-bottom:0">'.$slh['nicename'].'</h3>';
$list .= cflk_get_links($slh['key']);
$list .= '</div>';

}
}

// 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['slh-check'] = ( ! empty( $new_instance['slh-check'] ) ) ? $new_instance['slh-check'] : '';


return $instance;
}
} // Class slh_widget ends here

// Register and load the widget
function slh_load_widget() {
register_widget( 'slh_widget' );
}
add_action( 'widgets_init', 'slh_load_widget' );



Now I need to retrieve the output of the function widget( $args, $instance ) in a plugin.

Not exactly the output of the function widget( $args, $instance ). This widget is composed by several dropdown list, and I need to retrieve each values of this dropdown lists (in an array for example)

Answers (2)

2014-05-27

Dbranes answers:

If you mean that this widget is activated in a sidebar, and you want to get the corresponding setup parameters,
then you can try to peek into the global <em>wp_registered_widgets </em>variable:

print_r( $GLOBALS['wp_registered_widgets'] );


Sébastien | French WordpressDesigner comments:

This code displays the parameters of all widgets. It's not what I need.

I was not clear...

My widget is composed of ten dropdown list.
For each list we could select an option. Each dropdown had a value.
I need to retrieve this list of values to use in a plugin.


Dbranes comments:

What do you get from:

print_r( get_option( 'widget_slh_widget' ) );


Sébastien | French WordpressDesigner comments:

ahah yes that's it!
As you can see, I am very dumb with the widget api ! :-)


Dbranes comments:

ok great,

you probably know it better than I do ;-)



Dbranes comments:

Only <strong>Chuck Norris</strong> understands the WordPress Core ;-)


Sébastien | French WordpressDesigner comments:

lol, absolutely ! :)

2014-05-27

John Cotton answers:

It's not very clear what you want....

The HTML from the widget? Use output buffering and call the widget method of the class.

The values it gets? Do those come from cflk_get_links? ...it's not clear (and we can't see that code).

The settings for the widget? You seem to be holding those as serialised option values so you could just access the db...

You need to explain a little more.


Sébastien | French WordpressDesigner comments:

absolutely, it's not clear...

My widget is composed of ten dropdown list.
For each list we could select an option. Each dropdown had a value.
I need to retrieve this list of values to use in a plugin.


John Cotton comments:

The code outputting the dropdowns doesn't seem to be there. I think you need to show that for anyone to have a chance to help.