I am developing a wordpress widget, and need to store some user configuration values, that is why I am using wordpress update_option and get_option. I am new to widget development.
I have some configuration options for the widget that I need to access from a php function that is called via ajax, that is why I use the Options API (is there another way?)
The code I have is the following (first, widget functions, then my ajax handling function):
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['api_key'] = ( ! empty ($new_instance['api_key']) ) ? strip_tags ($new_instance['api_key']) : '';
$instance['forum_key'] = ( ! empty ($new_instance['forum_key']) ) ? strip_tags ($new_instance['forum_key']) : '';
$instance['limit'] = ( ! empty ($new_instance['limit']) ) ? strip_tags ($new_instance['limit']) : '';
return $instance;
} // end widget
public function form( $instance ) {
$instance = wp_parse_args(
(array) $instance
);
$title = ( isset ($instance['title'] ) ) ? esc_attr($instance['title']) : __( 'New title', 'eedee-dq-widget' );
$api_key = ( isset ($instance['api_key'] ) ) ? esc_attr($instance['api_key']) : '';
$forum_key = ( isset ( $instance ['forum_key'] ) ) ? esc_attr($instance['forum_key']) : '' ;
$limit = ( isset ( $instance ['limit'] ) ) ? esc_attr($instance['limit']) : 5 ;
// Display the admin form
include( plugin_dir_path(__FILE__) . '/views/admin.php' );
} // end form
public function handle_ajax_post() {
check_ajax_referer( 'my_nonce', 'security' );
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = false;
if ($error == false) {
$options = get_option('widget_my_comment_widget');
...
everything works (form values saving, ajax calling php function), the only problem is that the line
$options = get_option('widget_my_comment_widget');
returns an array like so
$options = array[3]
$options[3] = array[4]
$options[3]['title'] = (string) discussion
$options[3]['api_key'] = (string) xxx
$options[3]['forum_key'] = (string) zzz
$options[3]['limit'] = (string) 5
$options[4] = array[4]
$options[4]['title'] = (string) New title
$options[4]['api_key'] = (string) xxx
$options[4]['forum_key'] = (string) yyy
$options[4]['limit'] = (string) 5
$options['_multiwidget'] = (int) 1
so it returns multiple widget configurations (why? i only have one instance of the widget in my one sidebar? because i have it in multiple themes?). How can I make sure to either get the correct id of the options I want, or make sure there is only one set of options with no id key?
Edit After disabling the plugin, removing the db entry, enabling again, adding widget again the $options look like:
$options = array[2]
$options[2] = array[4]
$options[2]['title'] = (string) discourse
$options[2]['api_key'] = (string) xxx
$options[2]['forum_key'] = (string) highcut
$options[2]['limit'] = (string) 5
$options['_multiwidget'] = (int) 1
so still there is this index, I googled and found it is related to the _multiwidget setting, but why is it there?
Edit 2 My constructor is
parent::__construct(
'my_comment_widget',
__( 'my Comment Widget', 'eedee-my-widget' ),
array(
'description' => __( 'Displays recent and popular comments ', 'eedee-my-widget' ), )
);
so I am not using namespaces (or am I unwillingly?) like noted in this part of the official documentation, I found googling multi-widget.
$instance
variable.