3 * Widgets administration panel.
6 * @subpackage Administration
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
12 /** WordPress Administration Widgets API */
13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
15 if ( ! current_user_can('edit_theme_options') )
16 wp_die( __( 'Cheatin’ uh?' ));
18 $widgets_access = get_user_setting( 'widgets_access' );
19 if ( isset($_GET['widgets-access']) ) {
20 $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
21 set_user_setting( 'widgets_access', $widgets_access );
24 function wp_widgets_access_body_class($classes) {
25 return "$classes widgets_access ";
28 if ( 'on' == $widgets_access ) {
29 add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
31 wp_enqueue_script('admin-widgets');
34 wp_enqueue_script( 'jquery-touch-punch' );
38 * Fires early before the Widgets administration screen loads,
39 * after scripts are enqueued.
43 do_action( 'sidebar_admin_setup' );
45 $title = __( 'Widgets' );
46 $parent_file = 'themes.php';
48 get_current_screen()->add_help_tab( array(
50 'title' => __('Overview'),
52 '<p>' . __('Widgets are independent sections of content that can be placed into any widgetized area provided by your theme (commonly called sidebars). To populate your sidebars/widget areas with individual widgets, drag and drop the title bars into the desired area. By default, only the first widget area is expanded. To populate additional widget areas, click on their title bars to expand them.') . '</p>
53 <p>' . __('The Available Widgets section contains all the widgets you can choose from. Once you drag a widget into a sidebar, it will open to allow you to configure its settings. When you are happy with the widget settings, click the Save button and the widget will go live on your site. If you click Delete, it will remove the widget.') . '</p>'
55 get_current_screen()->add_help_tab( array(
56 'id' => 'removing-reusing',
57 'title' => __('Removing and Reusing'),
59 '<p>' . __('If you want to remove the widget but save its setting for possible future use, just drag it into the Inactive Widgets area. You can add them back anytime from there. This is especially helpful when you switch to a theme with fewer or different widget areas.') . '</p>
60 <p>' . __('Widgets may be used multiple times. You can give each widget a title, to display on your site, but it’s not required.') . '</p>
61 <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
63 get_current_screen()->add_help_tab( array(
64 'id' => 'missing-widgets',
65 'title' => __('Missing Widgets'),
67 '<p>' . __('Many themes show some sidebar widgets by default until you edit your sidebars, but they are not automatically displayed in your sidebar management tool. After you make your first widget change, you can re-add the default widgets by adding them from the Available Widgets area.') . '</p>' .
68 '<p>' . __('When changing themes, there is often some variation in the number and setup of widget areas/sidebars and sometimes these conflicts make the transition a bit less smooth. If you changed themes and seem to be missing widgets, scroll down on this screen to the Inactive Widgets area, where all of your widgets and their settings will have been saved.') . '</p>'
71 get_current_screen()->set_help_sidebar(
72 '<p><strong>' . __('For more information:') . '</strong></p>' .
73 '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
74 '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
77 if ( ! current_theme_supports( 'widgets' ) ) {
78 wp_die( __( 'The theme you are currently using isn’t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="http://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
81 // These are the widgets grouped by sidebar
82 $sidebars_widgets = wp_get_sidebars_widgets();
84 if ( empty( $sidebars_widgets ) )
85 $sidebars_widgets = wp_get_widget_defaults();
87 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
88 if ( 'wp_inactive_widgets' == $sidebar_id )
91 if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
92 if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
93 register_sidebar(array(
94 'name' => __( 'Inactive Sidebar (not used)' ),
96 'class' => 'inactive-sidebar orphan-sidebar',
97 'description' => __( 'This sidebar is no longer available and does not show anywhere on your site. Remove each of the widgets below to fully remove this inactive sidebar.' ),
98 'before_widget' => '',
100 'before_title' => '',
104 unset( $sidebars_widgets[ $sidebar_id ] );
109 // register the inactive_widgets area as sidebar
110 register_sidebar(array(
111 'name' => __('Inactive Widgets'),
112 'id' => 'wp_inactive_widgets',
113 'class' => 'inactive-sidebar',
114 'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
115 'before_widget' => '',
116 'after_widget' => '',
117 'before_title' => '',
123 // We're saving a widget without js
124 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
125 $widget_id = $_POST['widget-id'];
126 check_admin_referer("save-delete-widget-$widget_id");
128 $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
130 foreach ( $_POST as $key => $val ) {
131 if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
132 $_POST[$key] = array( $number => array_shift($val) );
138 $sidebar_id = $_POST['sidebar'];
139 $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
141 $id_base = $_POST['id_base'];
142 $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
145 if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
147 if ( !in_array($widget_id, $sidebar, true) ) {
148 wp_redirect( admin_url('widgets.php?error=0') );
152 $sidebar = array_diff( $sidebar, array($widget_id) );
153 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
156 $_POST['widget-id'] = $sidebar;
158 foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
159 if ( $name != $id_base || !is_callable($control['callback']) )
163 call_user_func_array( $control['callback'], $control['params'] );
169 $sidebars_widgets[$sidebar_id] = $sidebar;
171 // Remove old position.
172 if ( !isset($_POST['delete_widget']) ) {
173 foreach ( $sidebars_widgets as $key => $sb ) {
175 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
177 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
180 wp_set_sidebars_widgets($sidebars_widgets);
181 wp_redirect( admin_url('widgets.php?message=0') );
185 // Output the widget form without js
186 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
187 $widget_id = $_GET['editwidget'];
189 if ( isset($_GET['addnew']) ) {
190 // Default to the first sidebar
191 $keys = array_keys( $wp_registered_sidebars );
192 $sidebar = array_shift( $keys );
194 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
195 // Copy minimal info from an existing instance of this widget to a new instance
196 foreach ( $wp_registered_widget_controls as $control ) {
197 if ( $_GET['base'] === $control['id_base'] ) {
198 $control_callback = $control['callback'];
199 $multi_number = (int) $_GET['num'];
200 $control['params'][0]['number'] = -1;
201 $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
202 $wp_registered_widget_controls[$control['id']] = $control;
209 if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
210 $control = $wp_registered_widget_controls[$widget_id];
211 $control_callback = $control['callback'];
212 } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
213 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
217 $name = esc_html( strip_tags($control['name']) );
219 if ( !isset($sidebar) )
220 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
222 if ( !isset($multi_number) )
223 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
225 $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
227 // Show the widget form.
228 $width = ' style="width:' . max($control['width'], 350) . 'px"';
229 $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
231 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
233 <h2><?php echo esc_html( $title ); ?></h2>
234 <div class="editwidget"<?php echo $width; ?>>
235 <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
237 <form action="widgets.php" method="post">
238 <div class="widget-inside">
240 if ( is_callable( $control_callback ) )
241 call_user_func_array( $control_callback, $control['params'] );
243 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
246 <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
247 <div class="widget-position">
248 <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
250 foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
251 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
252 if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
255 if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
257 $sidebars_widgets[$sbname] = array();
259 $j = count($sidebars_widgets[$sbname]);
260 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
264 echo "\t\t<select name='{$sbname}_position'>\n";
265 echo "\t\t<option value=''>" . __('— Select —') . "</option>\n";
266 for ( $i = 1; $i <= $j; $i++ ) {
267 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
268 $selected = selected( $i, $key + 1, false );
269 echo "\t\t<option value='$i'$selected> $i </option>\n";
271 echo "\t\t</select>\n";
278 <div class="widget-control-actions">
280 if ( isset($_GET['addnew']) ) { ?>
281 <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
284 submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
286 submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
287 <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
288 <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
289 <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
290 <?php wp_nonce_field("save-delete-widget-$widget_id"); ?>
297 require_once( ABSPATH . 'wp-admin/admin-footer.php' );
306 __('Error while saving.'),
307 __('Error in displaying the widget settings form.')
310 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
313 <h2><?php echo esc_html( $title ); ?></h2>
315 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
316 <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
318 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
319 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
324 * Fires before the Widgets administration page content loads.
328 do_action( 'widgets_admin_page' ); ?>
330 <div class="widget-liquid-left">
331 <div id="widgets-left">
332 <div id="available-widgets" class="widgets-holder-wrap">
333 <div class="sidebar-name">
334 <div class="sidebar-name-arrow"><br /></div>
335 <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3>
337 <div class="widget-holder">
338 <div class="sidebar-description">
339 <p class="description"><?php _e('To activate a widget drag it to a sidebar or click on it. To deactivate a widget and delete its settings, drag it back.'); ?></p>
341 <div id="widget-list">
342 <?php wp_list_widgets(); ?>
351 $theme_sidebars = array();
352 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
353 if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
354 $wrap_class = 'widgets-holder-wrap';
355 if ( !empty( $registered_sidebar['class'] ) )
356 $wrap_class .= ' ' . $registered_sidebar['class'];
359 <div class="<?php echo esc_attr( $wrap_class ); ?>">
360 <div class="widget-holder inactive">
361 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
362 <div class="clear"></div>
368 $theme_sidebars[$sidebar] = $registered_sidebar;
378 $single_sidebar_class = '';
379 $sidebars_count = count( $theme_sidebars );
381 if ( $sidebars_count > 1 ) {
382 $split = ceil( $sidebars_count / 2 );
384 $single_sidebar_class = ' class="single-sidebar"';
388 <div class="widget-liquid-right">
389 <div id="widgets-right"<?php echo $single_sidebar_class; ?>>
390 <div class="sidebars-column-1">
393 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) {
394 $wrap_class = 'widgets-holder-wrap';
395 if ( !empty( $registered_sidebar['class'] ) )
396 $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
399 $wrap_class .= ' closed';
401 if ( $split && $i == $split ) {
403 </div><div class="sidebars-column-2">
408 <div class="<?php echo esc_attr( $wrap_class ); ?>">
409 <?php wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); // Show the control forms for each of the widgets in this sidebar ?>
420 <form action="" method="post">
421 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
426 <div class="widgets-chooser">
427 <ul class="widgets-chooser-sidebars"></ul>
428 <div class="widgets-chooser-actions">
429 <button class="button-secondary"><?php _e( 'Cancel' ); ?></button>
430 <button class="button-primary"><?php _e( 'Add Widget' ); ?></button>
437 * Fires after the available widgets and sidebars have loaded, before the admin footer.
441 do_action( 'sidebar_admin_page' );
442 require_once( ABSPATH . 'wp-admin/admin-footer.php' );