wp_ajax_save_widget

Ajax handler for saving a widget.

Description

wp_ajax_save_widget(); 

Usage

  1. if ( !function_exists( 'wp_ajax_save_widget' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_save_widget(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_save_widget() { 
  2. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; 
  3.  
  4. check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); 
  5.  
  6. if ( !current_user_can('edit_theme_options') || !isset($_POST['id_base']) ) 
  7. wp_die( -1 ); 
  8.  
  9. unset( $_POST['savewidgets'], $_POST['action'] ); 
  10.  
  11. /** 
  12. * Fires early when editing the widgets displayed in sidebars. 
  13. * @since 2.8.0 
  14. */ 
  15.  
  16. /** 
  17. * Fires early when editing the widgets displayed in sidebars. 
  18. * @since 2.8.0 
  19. */ 
  20.  
  21. /** This action is documented in wp-admin/widgets.php */ 
  22.  
  23. $id_base = $_POST['id_base']; 
  24. $widget_id = $_POST['widget-id']; 
  25. $sidebar_id = $_POST['sidebar']; 
  26. $multi_number = !empty($_POST['multi_number']) ? (int) $_POST['multi_number'] : 0; 
  27. $settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false; 
  28. $error = '<p>' . __('Anerrorhas occurred. Please reload the page and try again.') . '</p>'; 
  29.  
  30. $sidebars = wp_get_sidebars_widgets(); 
  31. $sidebar = isset($sidebars[$sidebar_id]) ? $sidebars[$sidebar_id] : array(); 
  32.  
  33. // Delete. 
  34. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { 
  35.  
  36. if ( !isset($wp_registered_widgets[$widget_id]) ) 
  37. wp_die( $error ); 
  38.  
  39. $sidebar = array_diff( $sidebar, array($widget_id) ); 
  40. $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1'); 
  41.  
  42. /** This action is documented in wp-admin/widgets.php */ 
  43. do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base ); 
  44.  
  45. } elseif ( $settings && preg_match( '/__i__|%i%/', key($settings) ) ) { 
  46. if ( !$multi_number ) 
  47. wp_die( $error ); 
  48.  
  49. $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) ); 
  50. $widget_id = $id_base . '-' . $multi_number; 
  51. $sidebar[] = $widget_id; 
  52. $_POST['widget-id'] = $sidebar; 
  53.  
  54. foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 
  55.  
  56. if ( $name == $id_base ) { 
  57. if ( !is_callable( $control['callback'] ) ) 
  58. continue; 
  59.  
  60. ob_start(); 
  61. call_user_func_array( $control['callback'], $control['params'] ); 
  62. ob_end_clean(); 
  63. break; 
  64.  
  65. if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { 
  66. $sidebars[$sidebar_id] = $sidebar; 
  67. echo "deleted:$widget_id"; 
  68. wp_die(); 
  69.  
  70. if ( !empty($_POST['add_new']) ) 
  71. wp_die(); 
  72.  
  73. if ( $form = $wp_registered_widget_controls[$widget_id] ) 
  74. call_user_func_array( $form['callback'], $form['params'] ); 
  75.  
  76. wp_die();