0

Thanks to another great post here I managed to set up an extra field (called "subtitle") for all WP Widgets. Now I am trying to make all WP widgets display the value of this extra field (naturally, after_title, since it's the subtitle), but I can't quite figure this out.

Here is the code I use to create the extra field for all the widgets:

function gcc_in_widget_form($t,$return,$instance){
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'float' => 'none') );

if ( !isset($instance['subtitle']) )
    $instance['subtitle'] = null;
?>
<!-- SUBTITLE -->
<p>
    <label for="<?php echo $t->get_field_id( 'subtitle' ); ?>"><?php _e( 'Subtitle:' ); ?></label> 
    <input class="widefat" id="<?php echo $t->get_field_id( 'subtitle' ); ?>" name="<?php echo $t->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo $instance['subtitle'];; ?>" />
</p>
$return = null;
return array($t,$return,$instance);
}
function gcc_in_widget_form_update($instance, $new_instance, $old_instance){
  $instance['subtitle'] = strip_tags($new_instance['subtitle']);
  return $instance;
}

add_action('in_widget_form', 'gcc_in_widget_form',5,3);   
add_filter('widget_update_callback', 'gcc_in_widget_form_update',5,3);    
add_filter('dynamic_sidebar_params', 'gcc_dynamic_sidebar_params');

And here is how I am trying to show the field's value in the front-end:

function gcc_dynamic_sidebar_params($params){
  global $wp_registered_widgets;
  $widget_id = $params[0]['widget_id'];
  $widget_obj = $wp_registered_widgets[$widget_id];
  $widget_opt = get_option($widget_obj['callback'][0]->option_name);
  $widget_num = $widget_obj['params'][0]['number'];

  $params[0]['after_title'] .= 'how to display the value of the instance['subtitle']??';

  return $params;
}

Can you please point me to the right direction?

1

Won't a filter on widget_title do this?

function widget_title_wpse_206841($title,$instance) {
  // var_dump($instance);
  if (!empty($instance['subtitle'])) {
    $title .= ' something '.$instance['subtitle'].' something ';
  }
  return $title;
}
add_filter('widget_title','widget_title_wpse_206841',10,2);
  • Aaaah, you are so right!!! Why did I feel the need to make things more complicated? Thanks so much for this! – Gabriela Oct 29 '15 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.