Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8520453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:32:28+00:00 2026-06-11T06:32:28+00:00

In WordPress, my goal is to be able to give a custom class to

  • 0

In WordPress, my goal is to be able to give a custom class to every widget that I put in the sidebar.
Preferably I would like to add this class in the widget settings of each widget. ALSO 3rd party widgets.

I was thinking of changing the classname, because the classname is already passed to the register_sidebar function (%2$s):

<?php
register_sidebar(array('before_widget' => '<aside id="%1$s" class="widget %2$s blue">'));
?>

Ofcourse I shouldn’t change WP core code or 3rd party plugins. This means that I need to write a plugin that will hook into the widget configuration process.

I already found out that it’s possible to modify all widgets forms, by hooking into action ‘in_widget_form’:

<?php
add_action('in_widget_form', 'teaserWidgetAddToForm');

function teaserWidgetAddToForm($widget_instance) {
  ?>
  <label for="<?php echo $widget_instance->get_field_id('teaser-classname'); ?>">Custom classname(s):</label>
  <input type="text" id="<?php echo $widget_instance->get_field_id('teaser-classname'); ?>" name="<?php echo $widget_instance->get_field_name('teaser-classname'); ?>">
  <?php
}
?>

This data should be saved by the Widget super class, but how do you retrieve this data (so when opening the widget settings later shows what you filled in earlier)

And, this saved data should be put in the widget instance -how is this done? I guess by using something like:

<?php
$wp_registered_widgets[<widget_id>]['callback'][0]['widget_options']['classname'] = <chosen_class>;
?>

So basically I have 2 questions:

  • Am I using the proper way to address this problem (styling of individual widgets)
  • If so, how can I modify a widget instance to save and retrieve additional settings, without having to modify WordPress or 3rd party Plugin source code.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:32:29+00:00Added an answer on June 11, 2026 at 6:32 am
    1. By default each widget instance has a unique ID which can be used to style it.

    2. In order to save data from fields added in an in_widget_form action, you need to also have a widget_update_callback filter.

      function my_widget_update_callback($instance, $new_instance) {
          $instance['my_classnames'] = $new_instance['my_classnames'];
          return $instance;
      }
      

      To display the saved value in the form you’ll need to first retrieve the instance settings.

      function my_in_widget_form($instance) {
          $settings = $instance->get_settings();
          …
      

      Finally, I think the simplest way to add your custom classes is in a widget_display_callback filter, which runs before a widget is displayed. You have to display the widget yourself because you can only return an instance from this filter, and instances do not control the widget CSS classes.

      function my_widget_display_callback($instance, $widget, $args) {
          if (!isset($instance['my_classnames'])) {
              return $instance;
          }
      
          $widget_classname = $widget->widget_options['classname'];
          $my_classnames = $instance['my_classnames'];
      
          $args['before_widget'] = str_replace($widget_classname, "{$widget_classname} {$my_classnames}", $args['before_widget']);
      
          $widget->widget($args, $instance);
      
          return false;
      }
      

    See this article for more info on the available hooks related to widgets: http://shibashake.com/wordpress-theme/wordpress-widget-system

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'd like to add a media library to a custom-made CMS that is build
I'm trying to use custom meta boxes in wordpress. My goal at the moment
First off, let me define the end goal: I'd like to Wordpress (version 2.8)
Wordpress has a filter that automatically add paragraphs to posts. I can remove this
The goal: I'm trying to achieve something like this in WPF: (source: wordpress.org )
I have a wordpress menu like this one: <div id=top_menu> <ul class=menu> <li id=menu-item-1
WordPress RSS feed not work when custom filed have '&'. i think that may
I am trying to build a menu in Wordpress that will look like this
I would like to make a website that is php and only requires one
Wordpress wraps images with captions in a div with a class of .wp-caption. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.