| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * The classic widget administration screen, for use in widgets.php. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 // Don't load directly. 10 if ( ! defined( 'ABSPATH' ) ) { 11 die( '-1' ); 12 } 13 14 $widgets_access = get_user_setting( 'widgets_access' ); 15 if ( isset( $_GET['widgets-access'] ) ) { 16 check_admin_referer( 'widgets-access' ); 17 18 $widgets_access = 'on' === $_GET['widgets-access'] ? 'on' : 'off'; 19 set_user_setting( 'widgets_access', $widgets_access ); 20 } 21 22 if ( 'on' === $widgets_access ) { 23 add_filter( 'admin_body_class', 'wp_widgets_access_body_class' ); 24 } else { 25 wp_enqueue_script( 'admin-widgets' ); 26 27 if ( wp_is_mobile() ) { 28 wp_enqueue_script( 'jquery-touch-punch' ); 29 } 30 } 31 32 /** 33 * Fires early before the Widgets administration screen loads, 34 * after scripts are enqueued. 35 * 36 * @since 2.2.0 37 */ 38 do_action( 'sidebar_admin_setup' ); 39 40 get_current_screen()->add_help_tab( 41 array( 42 'id' => 'overview', 43 'title' => __( 'Overview' ), 44 'content' => 45 '<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> 46 <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>', 47 ) 48 ); 49 get_current_screen()->add_help_tab( 50 array( 51 'id' => 'removing-reusing', 52 'title' => __( 'Removing and Reusing' ), 53 'content' => 54 '<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> 55 <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> 56 <p>' . __( 'Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.' ) . '</p>', 57 ) 58 ); 59 get_current_screen()->add_help_tab( 60 array( 61 'id' => 'missing-widgets', 62 'title' => __( 'Missing Widgets' ), 63 'content' => 64 '<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>' . 65 '<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>', 66 ) 67 ); 68 69 get_current_screen()->set_help_sidebar( 70 '<p><strong>' . __( 'For more information:' ) . '</strong></p>' . 71 '<p>' . __( '<a href="https://wordpress.org/documentation/article/appearance-widgets-screen-classic-editor/">Documentation on Widgets</a>' ) . '</p>' . 72 '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>' 73 ); 74 75 // These are the widgets grouped by sidebar. 76 $sidebars_widgets = wp_get_sidebars_widgets(); 77 78 if ( empty( $sidebars_widgets ) ) { 79 $sidebars_widgets = wp_get_widget_defaults(); 80 } 81 82 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) { 83 if ( 'wp_inactive_widgets' === $sidebar_id ) { 84 continue; 85 } 86 87 if ( ! is_registered_sidebar( $sidebar_id ) ) { 88 if ( ! empty( $widgets ) ) { // Register the inactive_widgets area as sidebar. 89 register_sidebar( 90 array( 91 'name' => __( 'Inactive Sidebar (not used)' ), 92 'id' => $sidebar_id, 93 'class' => 'inactive-sidebar orphan-sidebar', 94 '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.' ), 95 'before_widget' => '', 96 'after_widget' => '', 97 'before_title' => '', 98 'after_title' => '', 99 ) 100 ); 101 } else { 102 unset( $sidebars_widgets[ $sidebar_id ] ); 103 } 104 } 105 } 106 107 // Register the inactive_widgets area as sidebar. 108 register_sidebar( 109 array( 110 'name' => __( 'Inactive Widgets' ), 111 'id' => 'wp_inactive_widgets', 112 'class' => 'inactive-sidebar', 113 'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ), 114 'before_widget' => '', 115 'after_widget' => '', 116 'before_title' => '', 117 'after_title' => '', 118 ) 119 ); 120 121 retrieve_widgets(); 122 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" ); 127 128 $number = isset( $_POST['multi_number'] ) ? (int) $_POST['multi_number'] : ''; 129 if ( $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 ) ); 133 break; 134 } 135 } 136 } 137 138 $sidebar_id = $_POST['sidebar']; 139 $position = isset( $_POST[ $sidebar_id . '_position' ] ) ? (int) $_POST[ $sidebar_id . '_position' ] - 1 : 0; 140 141 $id_base = $_POST['id_base']; 142 $sidebar = $sidebars_widgets[ $sidebar_id ] ?? array(); 143 144 // Delete. 145 if ( isset( $_POST['removewidget'] ) && $_POST['removewidget'] ) { 146 147 if ( ! in_array( $widget_id, $sidebar, true ) ) { 148 wp_redirect( admin_url( 'widgets.php?error=0' ) ); 149 exit; 150 } 151 152 $sidebar = array_diff( $sidebar, array( $widget_id ) ); 153 $_POST = array( 154 'sidebar' => $sidebar_id, 155 'widget-' . $id_base => array(), 156 'the-widget-id' => $widget_id, 157 'delete_widget' => '1', 158 ); 159 160 /** 161 * Fires immediately after a widget has been marked for deletion. 162 * 163 * @since 4.4.0 164 * 165 * @param string $widget_id ID of the widget marked for deletion. 166 * @param string $sidebar_id ID of the sidebar the widget was deleted from. 167 * @param string $id_base ID base for the widget. 168 */ 169 do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base ); 170 } 171 172 $_POST['widget-id'] = $sidebar; 173 174 foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 175 if ( $name !== $id_base || ! is_callable( $control['callback'] ) ) { 176 continue; 177 } 178 179 ob_start(); 180 call_user_func_array( $control['callback'], $control['params'] ); 181 ob_end_clean(); 182 183 break; 184 } 185 186 $sidebars_widgets[ $sidebar_id ] = $sidebar; 187 188 // Remove old position. 189 if ( ! isset( $_POST['delete_widget'] ) ) { 190 foreach ( $sidebars_widgets as $sidebar_widget_id => $sidebar_widget ) { 191 if ( is_array( $sidebar_widget ) ) { 192 $sidebars_widgets[ $sidebar_widget_id ] = array_diff( $sidebar_widget, array( $widget_id ) ); 193 } 194 } 195 196 array_splice( $sidebars_widgets[ $sidebar_id ], $position, 0, $widget_id ); 197 } 198 199 wp_set_sidebars_widgets( $sidebars_widgets ); 200 wp_redirect( admin_url( 'widgets.php?message=0' ) ); 201 exit; 202 } 203 204 // Remove inactive widgets without JS. 205 if ( isset( $_POST['removeinactivewidgets'] ) ) { 206 check_admin_referer( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); 207 208 if ( $_POST['removeinactivewidgets'] ) { 209 foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) { 210 $pieces = explode( '-', $widget_id ); 211 $multi_number = array_pop( $pieces ); 212 $id_base = implode( '-', $pieces ); 213 $widget = get_option( 'widget_' . $id_base ); 214 unset( $widget[ $multi_number ] ); 215 update_option( 'widget_' . $id_base, $widget ); 216 unset( $sidebars_widgets['wp_inactive_widgets'][ $key ] ); 217 } 218 219 wp_set_sidebars_widgets( $sidebars_widgets ); 220 } 221 222 wp_redirect( admin_url( 'widgets.php?message=0' ) ); 223 exit; 224 } 225 226 // Output the widget form without JS. 227 if ( isset( $_GET['editwidget'] ) && $_GET['editwidget'] ) { 228 $widget_id = $_GET['editwidget']; 229 230 if ( isset( $_GET['addnew'] ) ) { 231 // Default to the first sidebar. 232 $keys = array_keys( $wp_registered_sidebars ); 233 $sidebar = reset( $keys ); 234 235 if ( isset( $_GET['base'] ) && isset( $_GET['num'] ) ) { // Multi-widget. 236 // Copy minimal info from an existing instance of this widget to a new instance. 237 foreach ( $wp_registered_widget_controls as $control ) { 238 if ( $_GET['base'] === $control['id_base'] ) { 239 $control_callback = $control['callback']; 240 $multi_number = (int) $_GET['num']; 241 $control['params'][0]['number'] = -1; 242 $control['id'] = $control['id_base'] . '-' . $multi_number; 243 $widget_id = $control['id']; 244 245 $wp_registered_widget_controls[ $control['id'] ] = $control; 246 break; 247 } 248 } 249 } 250 } 251 252 if ( isset( $wp_registered_widget_controls[ $widget_id ] ) && ! isset( $control ) ) { 253 $control = $wp_registered_widget_controls[ $widget_id ]; 254 $control_callback = $control['callback']; 255 } elseif ( ! isset( $wp_registered_widget_controls[ $widget_id ] ) && isset( $wp_registered_widgets[ $widget_id ] ) ) { 256 $name = esc_html( strip_tags( $wp_registered_widgets[ $widget_id ]['name'] ) ); 257 } 258 259 if ( ! isset( $name ) ) { 260 $name = esc_html( strip_tags( $control['name'] ) ); 261 } 262 263 if ( ! isset( $sidebar ) ) { 264 $sidebar = $_GET['sidebar'] ?? 'wp_inactive_widgets'; 265 } 266 267 if ( ! isset( $multi_number ) ) { 268 $multi_number = $control['params'][0]['number'] ?? ''; 269 } 270 271 $id_base = $control['id_base'] ?? $control['id']; 272 273 // Show the widget form. 274 $width = ' style="width:' . max( $control['width'], 350 ) . 'px"'; 275 $key = isset( $_GET['key'] ) ? (int) $_GET['key'] : 0; 276 277 require_once ABSPATH . 'wp-admin/admin-header.php'; 278 ?> 279 <div class="wrap"> 280 <h1><?php echo esc_html( $title ); ?></h1> 281 <div class="editwidget"<?php echo $width; ?>> 282 <h2> 283 <?php 284 /* translators: %s: Widget name. */ 285 printf( __( 'Widget %s' ), $name ); 286 ?> 287 </h2> 288 289 <form action="widgets.php" method="post"> 290 <div class="widget-inside"> 291 <?php 292 if ( is_callable( $control_callback ) ) { 293 call_user_func_array( $control_callback, $control['params'] ); 294 } else { 295 echo '<p>' . __( 'There are no options for this widget.' ) . "</p>\n"; 296 } 297 ?> 298 </div> 299 300 <p class="describe"><?php _e( 'Select both the sidebar for this widget and the position of the widget in that sidebar.' ); ?></p> 301 <div class="widget-position"> 302 <table class="widefat"><thead><tr><th><?php _e( 'Sidebar' ); ?></th><th><?php _e( 'Position' ); ?></th></tr></thead><tbody> 303 <?php 304 foreach ( $wp_registered_sidebars as $sidebar_name => $sidebar_data ) { 305 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr( $sidebar_name ) . "'" . 306 checked( $sidebar_name, $sidebar, false ) . " /> $sidebar_data[name]</label></td><td>"; 307 308 if ( 'wp_inactive_widgets' === $sidebar_name || str_starts_with( $sidebar_name, 'orphaned_widgets' ) ) { 309 echo ' '; 310 } else { 311 if ( ! isset( $sidebars_widgets[ $sidebar_name ] ) || ! is_array( $sidebars_widgets[ $sidebar_name ] ) ) { 312 $widget_count = 1; 313 314 $sidebars_widgets[ $sidebar_name ] = array(); 315 } else { 316 $widget_count = count( $sidebars_widgets[ $sidebar_name ] ); 317 318 if ( isset( $_GET['addnew'] ) || ! in_array( $widget_id, $sidebars_widgets[ $sidebar_name ], true ) ) { 319 ++$widget_count; 320 } 321 } 322 323 $selected = ''; 324 325 echo "\t\t<select name='{$sidebar_name}_position'>\n"; 326 echo "\t\t<option value=''>" . __( '— Select —' ) . "</option>\n"; 327 328 for ( $i = 1; $i <= $widget_count; $i++ ) { 329 if ( in_array( $widget_id, $sidebars_widgets[ $sidebar_name ], true ) ) { 330 $selected = selected( $i, $key + 1, false ); 331 } 332 333 echo "\t\t<option value='$i'$selected> $i </option>\n"; 334 } 335 336 echo "\t\t</select>\n"; 337 } 338 339 echo "</td></tr>\n"; 340 } 341 ?> 342 </tbody></table> 343 </div> 344 345 <div class="widget-control-actions"> 346 <div class="left-actions"> 347 <?php if ( ! isset( $_GET['addnew'] ) ) : ?> 348 <input type="submit" name="removewidget" id="removewidget" class="button-link button-link-delete widget-control-remove" value="<?php esc_attr_e( 'Delete' ); ?>" /> 349 <span class="widget-control-close-wrapper"> 350 | <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 351 </span> 352 <?php else : ?> 353 <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 354 <?php endif; ?> 355 </div> 356 <div class="right-actions"> 357 <?php submit_button( __( 'Save Widget' ), 'primary alignright', 'savewidget', false ); ?> 358 <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr( $widget_id ); ?>" /> 359 <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" /> 360 <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" /> 361 <?php wp_nonce_field( "save-delete-widget-$widget_id" ); ?> 362 </div> 363 </div> 364 365 </form> 366 </div> 367 </div> 368 <?php 369 require_once ABSPATH . 'wp-admin/admin-footer.php'; 370 exit; 371 } 372 373 $messages = array( 374 __( 'Changes saved.' ), 375 ); 376 377 $errors = array( 378 __( 'Error while saving.' ), 379 __( 'Error in displaying the widget settings form.' ), 380 ); 381 382 require_once ABSPATH . 'wp-admin/admin-header.php'; 383 ?> 384 385 <div class="wrap"> 386 <h1 class="wp-heading-inline"> 387 <?php 388 echo esc_html( $title ); 389 ?> 390 </h1> 391 392 <?php 393 if ( current_user_can( 'customize' ) ) { 394 printf( 395 ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>', 396 esc_url( 397 add_query_arg( 398 array( 399 array( 'autofocus' => array( 'panel' => 'widgets' ) ), 400 'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 401 ), 402 admin_url( 'customize.php' ) 403 ) 404 ), 405 __( 'Manage with Live Preview' ) 406 ); 407 } 408 409 $nonce = wp_create_nonce( 'widgets-access' ); 410 ?> 411 <div class="widget-access-link"> 412 <a id="access-on" href="widgets.php?widgets-access=on&_wpnonce=<?php echo urlencode( $nonce ); ?>"><?php _e( 'Enable accessibility mode' ); ?></a><a id="access-off" href="widgets.php?widgets-access=off&_wpnonce=<?php echo urlencode( $nonce ); ?>"><?php _e( 'Disable accessibility mode' ); ?></a> 413 </div> 414 415 <hr class="wp-header-end"> 416 417 <?php 418 if ( isset( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) { 419 wp_admin_notice( 420 $messages[ $_GET['message'] ], 421 array( 422 'id' => 'message', 423 'additional_classes' => array( 'updated' ), 424 'dismissible' => true, 425 ) 426 ); 427 } 428 if ( isset( $_GET['error'] ) && isset( $errors[ $_GET['error'] ] ) ) { 429 wp_admin_notice( 430 $errors[ $_GET['error'] ], 431 array( 432 'id' => 'message', 433 'additional_classes' => array( 'error' ), 434 'dismissible' => true, 435 ) 436 ); 437 } 438 439 /** 440 * Fires before the Widgets administration page content loads. 441 * 442 * @since 3.0.0 443 */ 444 do_action( 'widgets_admin_page' ); 445 ?> 446 447 <div class="widget-liquid-left"> 448 <div id="widgets-left"> 449 <div id="available-widgets" class="widgets-holder-wrap"> 450 <div class="sidebar-name"> 451 <button type="button" class="handlediv hide-if-no-js" aria-expanded="true"> 452 <span class="screen-reader-text"> 453 <?php 454 /* translators: Hidden accessibility text. */ 455 _e( 'Available Widgets' ); 456 ?> 457 </span> 458 <span class="toggle-indicator" aria-hidden="true"></span> 459 </button> 460 <h2><?php _e( 'Available Widgets' ); ?> <span id="removing-widget"><?php _ex( 'Deactivate', 'removing-widget' ); ?> <span></span></span></h2> 461 </div> 462 <div class="widget-holder"> 463 <div class="sidebar-description"> 464 <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> 465 </div> 466 <div id="widget-list"> 467 <?php wp_list_widgets(); ?> 468 </div> 469 <br class='clear' /> 470 </div> 471 <br class="clear" /> 472 </div> 473 474 <?php 475 476 $theme_sidebars = array(); 477 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) { 478 if ( str_contains( $registered_sidebar['class'], 'inactive-sidebar' ) || str_starts_with( $sidebar, 'orphaned_widgets' ) ) { 479 $wrap_class = 'widgets-holder-wrap'; 480 if ( ! empty( $registered_sidebar['class'] ) ) { 481 $wrap_class .= ' ' . $registered_sidebar['class']; 482 } 483 484 $is_inactive_widgets = 'wp_inactive_widgets' === $registered_sidebar['id']; 485 ?> 486 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 487 <div class="widget-holder inactive"> 488 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?> 489 490 <?php if ( $is_inactive_widgets ) { ?> 491 <div class="remove-inactive-widgets"> 492 <form method="post"> 493 <p> 494 <?php 495 $attributes = array( 'id' => 'inactive-widgets-control-remove' ); 496 497 if ( empty( $sidebars_widgets['wp_inactive_widgets'] ) ) { 498 $attributes['disabled'] = ''; 499 } 500 501 submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes ); 502 ?> 503 <span class="spinner"></span> 504 </p> 505 <?php wp_nonce_field( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); ?> 506 </form> 507 </div> 508 <?php } ?> 509 </div> 510 <?php if ( $is_inactive_widgets ) { ?> 511 <p class="description"><?php _e( 'This will clear all items from the inactive widgets list. You will not be able to restore any customizations.' ); ?></p> 512 <?php } ?> 513 </div> 514 <?php 515 516 } else { 517 $theme_sidebars[ $sidebar ] = $registered_sidebar; 518 } 519 } 520 521 ?> 522 </div> 523 </div> 524 <?php 525 526 $sidebar_index = 0; 527 $split = 0; 528 $single_sidebar_class = ''; 529 $sidebars_count = count( $theme_sidebars ); 530 531 if ( $sidebars_count > 1 ) { 532 $split = (int) ceil( $sidebars_count / 2 ); 533 } else { 534 $single_sidebar_class = ' single-sidebar'; 535 } 536 537 ?> 538 <div class="widget-liquid-right"> 539 <div id="widgets-right" class="wp-clearfix<?php echo $single_sidebar_class; ?>"> 540 <div class="sidebars-column-1"> 541 <?php 542 543 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) { 544 $wrap_class = 'widgets-holder-wrap'; 545 if ( ! empty( $registered_sidebar['class'] ) ) { 546 $wrap_class .= ' sidebar-' . $registered_sidebar['class']; 547 } 548 549 if ( $sidebar_index > 0 ) { 550 $wrap_class .= ' closed'; 551 } 552 553 if ( $split && $sidebar_index === $split ) { 554 ?> 555 </div><div class="sidebars-column-2"> 556 <?php 557 } 558 559 ?> 560 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 561 <?php 562 // Show the control forms for each of the widgets in this sidebar. 563 wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); 564 ?> 565 </div> 566 <?php 567 568 ++$sidebar_index; 569 } 570 571 ?> 572 </div> 573 </div> 574 </div> 575 <form method="post"> 576 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?> 577 </form> 578 <br class="clear" /> 579 </div> 580 581 <div class="widgets-chooser"> 582 <ul class="widgets-chooser-sidebars"></ul> 583 <div class="widgets-chooser-actions"> 584 <button class="button widgets-chooser-cancel"><?php _e( 'Cancel' ); ?></button> 585 <button class="button button-primary widgets-chooser-add"><?php _e( 'Add Widget' ); ?></button> 586 </div> 587 </div> 588 589 <?php 590 591 /** 592 * Fires after the available widgets and sidebars have loaded, before the admin footer. 593 * 594 * @since 2.2.0 595 */ 596 do_action( 'sidebar_admin_page' ); 597 require_once ABSPATH . 'wp-admin/admin-footer.php';
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue May 5 08:20:14 2026 | Cross-referenced by PHPXref |