Articles of widgets

Passing $this->get_field_name() to javascript

I’ve tried doing this one of two ways: In the form method of my Widget class (which extends WP_Widget), I have the following snippet, which sets a global javascript variable: if( $instance ) : ?> <script type=”text/javascript”> window.widget_order_name = “<?php echo $this->get_field_name( ‘order’ ) ?>”; </script> <?php endif; In the widget admin markup, I also […]

How can I delete all inactive widgets?

I want to cleanup all inactive widgets. I tried following snippet as suggested by this answer Script to remove all inactive widgets?. $sidebars_widgets = get_option( ‘sidebars_widgets’ ); $sidebars_widgets[‘wp_inactive_widgets’] = array(); update_option( ‘sidebars_widgets’, $sidebars_widgets ); I also tried deleting option sidebars_widgets directly from the options table. But after page is refreshed, old value is restored with […]

How to check if a shortcode is being executed in a widget or post

I am creating a feature for my website using a WordPress shortcode. The shortcode is displays an HTML table that contains certain data. I have enabled the short code for widget/sidebar and it is working fine in the widget. The only problem is that my designer has created two different styles; one for post and […]

Custom Widget form function common elements

I am writing a set of custom widgets, all of which share a common 18 lines of code in the form() function, which looks like this: <div class=”common-section”> <input type=”hidden” class=”inputbox” name=”something” /> <?php $choices = [ ‘first’ => array(‘red’,’orange’,’blue’), ‘second’ => array(‘red’,’white’,’blue’), ‘third’ => array(‘green’,’black’,’white’) ]; ?> <?php foreach($choices as $colorset => $colors) : […]

Updating Widget options in custom install.php

I am using a custom install.php in /wp-content/ to over-ride some defaults when creating sites in a multi-site install. For the most part, this is easy and working really well. I am having difficulty with the widgets though. All I want for widgets is the Search widget and a custom Meta widget we use in-house. […]

How to exclude certain widget from showing up on home/front page?

This question already has an answer here: Call dynamic_sidebar but include/exclude named widgets? 3 answers

'Pages' widget alternative for custom post types?

I want to make a menu from a hierarchical custom post type. There is a default WordPress widget called ‘Pages’ that give you list of all the pages preserving their hierarchy. Is there a way to use this widget for custom post types?

Insert Widget option into mark-up with register_sidebar

I have added a custom field to my all widgets in my theme, called “custom-title”. I have managed to save the field into the database for each widget, like so: s:12:”custom-title”;s:17:”HELLO TEST ANCHOR” I want to use this to add a data-attribute “custom-title” to the mark-up for each widget, on the before_widget argument. I want […]

widget should display post archive by year and on click also by month

I want to display the post archives like seen in this picture. Shown by year and on click it should open up the months. I am not sure how to approach this, would you customize the standard WordPress Archives Widget?

How to make a widget expand wider than the column width when editing its settings in the admin

The WordPress Text widget expands horizontally beyond the margin of the sidebar when added to a sidebar and opened in the admin and I am trying to apply that function to a widget. I see inline css is being dynamically injected when the widget tab is opened and inserts as div style=”z-index: 100; margin-left: -88px;” […]