Determining a Widget Instance and Sidebar Location?

The widget_display_callback hook takes 3 arguments as we can see in /wp-includes/widgets.php: (line 180, WP 3.4.2) $instance = apply_filters(‘widget_display_callback’, $instance, $this, $args); So, to make a complete hook, the code is: // Priority = 10 // Arguments = 3 add_filter(‘widget_display_callback’, ‘wpse_57546_widget_display_callback’, 10, 3); // Using “$this” as an argument produces errors, // as it is … Read more

How to add a filter to all widget output

There is another thread on here that discusses a workaround. Well… the familiar php workaround when a function does not provide a “get to variable” output actually… use ob_start: http://php.net/manual/en/function.ob-start.php to just capture the output and manipulate it before sending it on its way. Leads on stackoverflow: https://stackoverflow.com/search?q=%2Bwordpress+sidebar+%2Bob-start+

What is this instance variable doing in the Widgets class

$instance holds the data stored for this widget instance as an array. You could use the same widget multiple times, and each would get different data. get_field_name() and get_field_id() returns name/id attributes for that widget. They are unique for each widget, but do not depend on the data. That’s why they do not have to … Read more

How to add author details in the post sidebar?

global $authordata; This variable contains the current post’s author data. From an author data widget I have written: global $authordata; if ( ( is_singular() or is_author() ) and is_object( $authordata ) and isset ( $authordata->ID ) ) { return $authordata->ID; } To get the link to the author archive: get_author_posts_url( $authordata->ID ); Update Here is … Read more

Where to find the source code of a widget?

The default widgets are defined in wp-includes/default-widgets.php. To find specific code search the Codex or use queryposts.com. The best tool is your IDE (integrated development environment). All IDEs offer a full text search in a set of files. Here a screen shot from Eclipse PDT As you can see it is really flexible – and … Read more

How to add color picker to widgets?

Here is the code I used for one of my projects: <?php function load_color_picker_style() { wp_enqueue_style( ‘wp-color-picker’ ); } add_action(‘admin_print_scripts-widgets.php’, ‘load_color_picker_script’); add_action(‘admin_print_styles-widgets.php’, ‘load_color_picker_style’); ?> ///Javascript jQuery(document).ready(function($){ function updateColorPickers(){ $(‘#widgets-right .wp-color-picker’).each(function(){ $(this).wpColorPicker({ // you can declare a default color here, // or in the data-default-color attribute on the input defaultColor: false, // a callback to fire … Read more

Programmatically edit the text of a widget

I guess you want to locate the widget by it’s title, so you can try this function: /** * Update a widget text located by it’s title * * @see https://wordpress.stackexchange.com/a/155518/26350 * * @param string $search_title * @param string $new_text * @param boolean */ function wpse_155046_update_widget_text_by_title( $search_title, $new_text ) { // Get all data from … Read more

How do I rebind event after widget save

WordPress provides callbacks for widgets interactions. You’re seems interested in two of them: widget-added, widget-updated Usage example: jQuery(document).on(‘widget-updated’, function(e, widget){ // do your awesome stuff here // “widget” represents jQuery object of the affected widget’s DOM element });

Loop through widgets in sidebar

The registered widgets in your sidebar-1 might be [sidebar-1] => Array ( [0] => categories-2 [1] => archives-4 [2] => recent-comments-4 [3] => calendar-2 [4] => search-2 [5] => archives-2 [6] => text-4 [7] => recent-posts-2 [8] => nav_menu-2 ) If you got for example the calendar-2 widget, then from: print_r( $GLOBALS[‘wp_registered_widgets’][‘calendar-2’] ); you will … Read more