including image assets in widget

It sounds like you’ll need the plugins_url() function which generates the URL for the plugins directory and can handle any alternate configuration (such as if you moved it to the /mu-plugins/ directory, a personal favorite of mine). The Codex documentation is good, but here’s a quick example. Let’s say you’re making this in a plugin … Read more

How do I add Widgets to my theme and activate them on my site?

It sounds like you’re trying to add a new widget area (dynamic sidebar) to your Theme. That process has three parts: Register the dynamic sidebar in functions.php, using register_sidebar(): function wpse121723_register_sidebars() { register_sidebar( array( ‘name’ => ‘Home right sidebar’, ‘id’ => ‘home_right_1’, ‘before_widget’ => ‘<div>’, ‘after_widget’ => ‘</div>’, ‘before_title’ => ‘<h2 class=”rounded”>’, ‘after_title’ => ‘</h2>’, … Read more

Widget options get

Widget data is stored in the options table as a two dimensional array. All data for the same type of widget is stored under the same option key. The key of the outer array is a kind of widget “index”– the identifier for the particular instance of the widget. The inner arrays contain the individual … Read more

How do I display the “Archives” widget layout (sidebar) in WordPress by ‘year’ then by ‘months’?

Changing the default widget would be pretty complicated. However, you can write your own shortcode and function to get your desired list. I’m guessing you want an unordered list in your widget? Put this in your theme’s functions.php: add_shortcode(‘archive_by_year_and_month’,’get_archive_by_year_and_month’); function get_archive_by_year_and_month($atts=array()){ global $wpdb; $years = $wpdb->get_col(“SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_type=”post” AND post_status=”publish” ORDER … Read more

WordPress multiple widget in single plugin

There is absolutely no problem with registering several widgets inside one file. Widgets are separate classes, and unlike other programming languages, PHP will have no problem with your declaring two classes inside one file. add_action( ‘widgets_init’, ‘src_load_widgets’ ); function src_load_widgets() { register_widget( ‘Widget_SRC’ ); register_widget( ‘Widget_Rank ‘ ); } class Widget_SRC extends WP_Widget { // … Read more

How can I add a new CSS class to a widget?

Don’t edit your parent theme’s functions.php file. register_sidebar( array( ‘name’ => __( ‘Sidebar name’, ‘theme_text_domain’ ), ‘id’ => ‘unique-sidebar-id’, ‘description’ => ”, ‘class’ => ‘add class here’, ‘before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’, ‘after_widget’ => ‘</li>’, ‘before_title’ => ‘<h2 class=”widgettitle”>’, ‘after_title’ => ‘</h2>’ ) ); Copy it over to your child theme’s functions.php and make … Read more

Unable to save widget contents

Reason is pretty simple and easy to overlook… It has nothing to do with placing both widgets in the same file or anything like this… Let’s look at your code that is responsible for printing the widgets’ form: public function form( $instance ) { echo ‘<p><strong>Sometext here</p>’; } Do you see it now? There are … Read more

How to add multiple custom widget areas

You have to register multiple areas: function new_sidebar_widget_init() { register_sidebar( array( ‘name’ => ‘new-sidebar’, ‘id’ => ‘new-sidebar’, ‘before_widget’ => ‘<div id=”new-sidebar”>’, ‘after_widget’ => ‘</div>’, ‘before_title’ => ”, ‘after_title’ => ”, ) ); register_sidebar( array( ‘name’ => ‘new-sidebar-1’, ‘id’ => ‘new-sidebar-1’, ‘before_widget’ => ‘<div id=”new-sidebar”>’, ‘after_widget’ => ‘</div>’, ‘before_title’ => ”, ‘after_title’ => ”, ) ); … Read more