Change recent posts widget title

I had a similar situation. Add the following code to your functions.php and see if it works for you. function my_widget_title($title, $instance, $id_base) { if ( ‘recent-posts’ == $id_base) { return __(‘New Title’); } else { return $title; } } add_filter ( ‘widget_title’ , ‘my_widget_title’, 10, 3); Note: this will change the title of all … Read more

Widget – Store and update data

You probably want to look at using WordPress Transients as they can handle both the data and the timestamp/expiration. You can also check out The Deal with WordPress Transients by CSS Tricks which has some good info as well.

Ajax gives 400 error

Explanation: The issue you are likely facing is that you are trying to add your ajax actions from within the widget itself that may be instantiated on the elementor/widgets/widgets_registered hook. If so, this is hook elementor/widgets/widgets_registered is too late and do_action( “wp_ajax_{$action}” ) and do_action( “wp_ajax_nopriv_{$action}” ) have already fired. Working example: NOTE: very rough … Read more

Converting a WordPress widget to a block

The short answer here is no. Widgets are written in PHP and blocks are written in JavaScript. Beyond that there pretty fundamental differences between the two paradigms in the sense of where they are stored, markup output approaches etc. You could take the approach of a dynamic block where the block is rendered on the … Read more

Need some help understanding widgets

Widgets are contained in sidebars (not 100% correct, but good enough). A sidebar is an area in which placement of widgets can be managed in the admin. Widgets are realy an instance of a widget class and usually you can have several widgets of the same class in the same and different sidebars. You can … Read more

Create a widget that allows text input

Complete code: class My_Widget_Avatar extends WP_Widget { function My_Widget_Avatar() { $widget_ops = array( ‘classname’ => ‘widget_avatar’, ‘description’ => __( “My Avatar Widget” ) ); $this->WP_Widget(‘my_avatar’, __(‘My Avatar’), $widget_ops); } function widget( $args, $instance ) { extract($args); $text = apply_filters( ‘widget_text’, $instance[‘text’], $instance ); echo $before_widget; ?> <div class=”textwidget”> <div class=”avatar”> <div class=”avatartext”> <p><?php echo $text; … Read more

How/where is the global variable $wp_registered_widgets filled?

As you can see here, $wp_registered_widgets is defined in wp-includes/widgets.php (as expected). You should be able to debug it by doing something like this: function yoast_print_active_widgets() { global $wp_registered_widgets; echo ‘<pre>’.print_r( $wp_registered_widgets, 1 ).'</pre>’; } add_action(‘init’,’yoast_print_active_widgets’); Then you could loop through the different stages, from init, to send_headers to wp_head and see where stuff goes … Read more

Custom dashboard widget search box

The following will do, although it redirects to the Posts listing page /wp-admin/edit.php… Also included, search boxes for Pages and CPTs. Code /** * PLEASE NOTE THAT THE FOLLOWING CODE DOESN’T HAVE ANY SANITIZATION or NONCE methods */ add_action(‘wp_dashboard_setup’, ‘wpse_58520_dashboard_search_widget’); function wpse_58520_dashboard_search_widget() { wp_add_dashboard_widget( ‘wpse_54742_active_site_plugins’, __( ‘Search Posts/Pages/CPTs’ ), ‘wpse_58520_make_dashboard_search_widget’ ); } function wpse_58520_make_dashboard_search_widget() { … Read more