wp_register_sidebar_widget

Register an instance of a widget.

Description

wp_register_sidebar_widget( (int|string) $id, (string) $name, (callable) $output_callback, (array) $options = array() ); 

The default widget option is classname that can be overridden.

The function can also be used to un-register widgets when $output_callback parameter is an empty string.

Parameters (4)

0. $id (int|string)
Widget ID.
1. $name (string)
Widget display title.
2. $output_callback (callable)
Run when widget is called.
3. $options — Optional. (array) => array()
An array of supplementary widget options for the instance.

Options

  • classname (string) => is a shortened version of the output callback name

    Class name for the widget's HTML container.

array(

    /**
     * Class name for the widget's HTML container.
     *
     * @type string
     * @default is a shortened version of the output callback name
     */
    'classname' => is a shortened version of the output callback name
);        


Usage

  1. if ( !function_exists( 'wp_register_sidebar_widget' ) ) { 
  2. require_once ABSPATH . WPINC . '/widgets.php'; 
  3.  
  4. // Widget ID. 
  5. $id = null; 
  6.  
  7. // Widget display title. 
  8. $name = ''; 
  9.  
  10. // Run when widget is called. 
  11. $output_callback = null; 
  12.  
  13. // Optional. An array of supplementary widget options for the instance. 
  14. $options = array( 
  15. 'classname' => is a shortened version of the output callback name 
  16. ); 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = wp_register_sidebar_widget($id, $name, $output_callback, $options); 
  20.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/widgets.php  
  1. function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) { 
  2. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; 
  3.  
  4. $id = strtolower($id); 
  5.  
  6. if ( empty($output_callback) ) { 
  7. unset($wp_registered_widgets[$id]); 
  8. return; 
  9.  
  10. $id_base = _get_widget_id_base($id); 
  11. if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) { 
  12. unset( $wp_registered_widget_controls[ $id ] ); 
  13. unset( $wp_registered_widget_updates[ $id_base ] ); 
  14. return; 
  15.  
  16. $defaults = array('classname' => $output_callback); 
  17. $options = wp_parse_args($options, $defaults); 
  18. $widget = array( 
  19. 'name' => $name,  
  20. 'id' => $id,  
  21. 'callback' => $output_callback,  
  22. 'params' => array_slice(func_get_args(), 4) 
  23. ); 
  24. $widget = array_merge($widget, $options); 
  25.  
  26. if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) { 
  27.  
  28. /** 
  29. * Fires once for each registered widget. 
  30. * @since 3.0.0 
  31. * @param array $widget An array of default widget arguments. 
  32. */ 
  33. $wp_registered_widgets[$id] = $widget;