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

Show widget when not using SSL

WordPress has built-in function for this purpose – is_ssl(). http://codex.wordpress.org/Function_Reference/is_ssl So, you should edit this widget and put something like if ( is_ssl() ) { } else { echo ‘you should use ssl’; }

Displaying a widget only on long posts (over X characters)

Use the function t5_word_count() from this answer and extend the method widget() in your widget class: public function widget( $args, $instance ) { if ( ! is_singular() ) return; $content = get_the_content( ”, TRUE ); $words = t5_word_count( $content ); if ( 50 > $words ) return; // print you widget }

Can I use a form in a dashboard widget?

There are two options to use forms in a dashboard widget: Use the parameter control_callback. See the Codex wp_add_dashboard_widget( ‘my_id’, // $widget_id ‘my_name’, // $widget_name ‘my_render_widget’, // $callback ‘my_control’ // $control_callback ); function my_control() { // print some form elements, WordPress will handle the <form> tag. } If you want to use a form in … Read more

Add before_content and after_content to register_sidebar

The limitation is down to the existing arguments and how pretty much all widgets typically output content: echo $args[‘before_widget’]; if ( $title ) { echo $args[‘before_title’] . $title . $args[‘after_title’]; } // Widget content echo $args[‘after_widget’]; Some widgets may offer before/after content hooks, but if you want a “global” fix there’s no surefire way around … Read more