| [ 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 exit; 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 $sidebar = array_key_first( $wp_registered_sidebars ); 233 234 if ( isset( $_GET['base'] ) && isset( $_GET['num'] ) ) { // Multi-widget. 235 // Copy minimal info from an existing instance of this widget to a new instance. 236 foreach ( $wp_registered_widget_controls as $control ) { 237 if ( $_GET['base'] === $control['id_base'] ) { 238 $control_callback = $control['callback']; 239 $multi_number = (int) $_GET['num']; 240 $control['params'][0]['number'] = -1; 241 $control['id'] = $control['id_base'] . '-' . $multi_number; 242 $widget_id = $control['id']; 243 244 $wp_registered_widget_controls[ $control['id'] ] = $control; 245 break; 246 } 247 } 248 } 249 } 250 251 if ( isset( $wp_registered_widget_controls[ $widget_id ] ) && ! isset( $control ) ) { 252 $control = $wp_registered_widget_controls[ $widget_id ]; 253 $control_callback = $control['callback']; 254 } elseif ( ! isset( $wp_registered_widget_controls[ $widget_id ] ) && isset( $wp_registered_widgets[ $widget_id ] ) ) { 255 $name = esc_html( strip_tags( $wp_registered_widgets[ $widget_id ]['name'] ) ); 256 } 257 258 if ( ! isset( $name ) ) { 259 $name = esc_html( strip_tags( $control['name'] ) ); 260 } 261 262 if ( ! isset( $sidebar ) ) { 263 $sidebar = $_GET['sidebar'] ?? 'wp_inactive_widgets'; 264 } 265 266 if ( ! isset( $multi_number ) ) { 267 $multi_number = $control['params'][0]['number'] ?? ''; 268 } 269 270 $id_base = $control['id_base'] ?? $control['id']; 271 272 // Show the widget form. 273 $width = ' style="width:' . max( $control['width'], 350 ) . 'px"'; 274 $key = isset( $_GET['key'] ) ? (int) $_GET['key'] : 0; 275 276 require_once ABSPATH . 'wp-admin/admin-header.php'; 277 ?> 278 <div class="wrap"> 279 <h1><?php echo esc_html( $title ); ?></h1> 280 <div class="editwidget"<?php echo $width; ?>> 281 <h2> 282 <?php 283 /* translators: %s: Widget name. */ 284 printf( __( 'Widget %s' ), $name ); 285 ?> 286 </h2> 287 288 <form action="widgets.php" method="post"> 289 <div class="widget-inside"> 290 <?php 291 if ( is_callable( $control_callback ) ) { 292 call_user_func_array( $control_callback, $control['params'] ); 293 } else { 294 echo '<p>' . __( 'There are no options for this widget.' ) . "</p>\n"; 295 } 296 ?> 297 </div> 298 299 <p class="describe"><?php _e( 'Select both the sidebar for this widget and the position of the widget in that sidebar.' ); ?></p> 300 <div class="widget-position"> 301 <table class="widefat"><thead><tr><th><?php _e( 'Sidebar' ); ?></th><th><?php _e( 'Position' ); ?></th></tr></thead><tbody> 302 <?php 303 foreach ( $wp_registered_sidebars as $sidebar_name => $sidebar_data ) { 304 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr( $sidebar_name ) . "'" . 305 checked( $sidebar_name, $sidebar, false ) . " /> $sidebar_data[name]</label></td><td>"; 306 307 if ( 'wp_inactive_widgets' === $sidebar_name || str_starts_with( $sidebar_name, 'orphaned_widgets' ) ) { 308 echo ' '; 309 } else { 310 if ( ! isset( $sidebars_widgets[ $sidebar_name ] ) || ! is_array( $sidebars_widgets[ $sidebar_name ] ) ) { 311 $widget_count = 1; 312 313 $sidebars_widgets[ $sidebar_name ] = array(); 314 } else { 315 $widget_count = count( $sidebars_widgets[ $sidebar_name ] ); 316 317 if ( isset( $_GET['addnew'] ) || ! in_array( $widget_id, $sidebars_widgets[ $sidebar_name ], true ) ) { 318 ++$widget_count; 319 } 320 } 321 322 $selected = ''; 323 324 echo "\t\t<select name='{$sidebar_name}_position'>\n"; 325 echo "\t\t<option value=''>" . __( '— Select —' ) . "</option>\n"; 326 327 for ( $i = 1; $i <= $widget_count; $i++ ) { 328 if ( in_array( $widget_id, $sidebars_widgets[ $sidebar_name ], true ) ) { 329 $selected = selected( $i, $key + 1, false ); 330 } 331 332 echo "\t\t<option value='$i'$selected> $i </option>\n"; 333 } 334 335 echo "\t\t</select>\n"; 336 } 337 338 echo "</td></tr>\n"; 339 } 340 ?> 341 </tbody></table> 342 </div> 343 344 <div class="widget-control-actions"> 345 <div class="left-actions"> 346 <?php if ( ! isset( $_GET['addnew'] ) ) : ?> 347 <input type="submit" name="removewidget" id="removewidget" class="button-link button-link-delete widget-control-remove" value="<?php esc_attr_e( 'Delete' ); ?>" /> 348 <span class="widget-control-close-wrapper"> 349 | <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 350 </span> 351 <?php else : ?> 352 <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 353 <?php endif; ?> 354 </div> 355 <div class="right-actions"> 356 <?php submit_button( __( 'Save Widget' ), 'primary alignright', 'savewidget', false ); ?> 357 <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr( $widget_id ); ?>" /> 358 <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" /> 359 <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" /> 360 <?php wp_nonce_field( "save-delete-widget-$widget_id" ); ?> 361 </div> 362 </div> 363 364 </form> 365 </div> 366 </div> 367 <?php 368 require_once ABSPATH . 'wp-admin/admin-footer.php'; 369 exit; 370 } 371 372 $messages = array( 373 __( 'Changes saved.' ), 374 ); 375 376 $errors = array( 377 __( 'Error while saving.' ), 378 __( 'Error in displaying the widget settings form.' ), 379 ); 380 381 require_once ABSPATH . 'wp-admin/admin-header.php'; 382 ?> 383 384 <div class="wrap"> 385 <h1 class="wp-heading-inline"> 386 <?php 387 echo esc_html( $title ); 388 ?> 389 </h1> 390 391 <?php 392 if ( current_user_can( 'customize' ) ) { 393 printf( 394 ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>', 395 esc_url( 396 add_query_arg( 397 array( 398 array( 'autofocus' => array( 'panel' => 'widgets' ) ), 399 'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 400 ), 401 admin_url( 'customize.php' ) 402 ) 403 ), 404 __( 'Manage with Live Preview' ) 405 ); 406 } 407 408 $nonce = wp_create_nonce( 'widgets-access' ); 409 ?> 410 <div class="widget-access-link"> 411 <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> 412 </div> 413 414 <hr class="wp-header-end"> 415 416 <?php 417 if ( isset( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) { 418 wp_admin_notice( 419 $messages[ $_GET['message'] ], 420 array( 421 'id' => 'message', 422 'additional_classes' => array( 'updated' ), 423 'dismissible' => true, 424 ) 425 ); 426 } 427 if ( isset( $_GET['error'] ) && isset( $errors[ $_GET['error'] ] ) ) { 428 wp_admin_notice( 429 $errors[ $_GET['error'] ], 430 array( 431 'id' => 'message', 432 'additional_classes' => array( 'error' ), 433 'dismissible' => true, 434 ) 435 ); 436 } 437 438 /** 439 * Fires before the Widgets administration page content loads. 440 * 441 * @since 3.0.0 442 */ 443 do_action( 'widgets_admin_page' ); 444 ?> 445 446 <div class="widget-liquid-left"> 447 <div id="widgets-left"> 448 <div id="available-widgets" class="widgets-holder-wrap"> 449 <div class="sidebar-name"> 450 <button type="button" class="handlediv hide-if-no-js" aria-expanded="true"> 451 <span class="screen-reader-text"> 452 <?php 453 /* translators: Hidden accessibility text. */ 454 _e( 'Available Widgets' ); 455 ?> 456 </span> 457 <span class="toggle-indicator" aria-hidden="true"></span> 458 </button> 459 <h2><?php _e( 'Available Widgets' ); ?> <span id="removing-widget"><?php _ex( 'Deactivate', 'removing-widget' ); ?> <span></span></span></h2> 460 </div> 461 <div class="widget-holder"> 462 <div class="sidebar-description"> 463 <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> 464 </div> 465 <div id="widget-list"> 466 <?php wp_list_widgets(); ?> 467 </div> 468 <br class='clear' /> 469 </div> 470 <br class="clear" /> 471 </div> 472 473 <?php 474 475 $theme_sidebars = array(); 476 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) { 477 if ( str_contains( $registered_sidebar['class'], 'inactive-sidebar' ) || str_starts_with( $sidebar, 'orphaned_widgets' ) ) { 478 $wrap_class = 'widgets-holder-wrap'; 479 if ( ! empty( $registered_sidebar['class'] ) ) { 480 $wrap_class .= ' ' . $registered_sidebar['class']; 481 } 482 483 $is_inactive_widgets = 'wp_inactive_widgets' === $registered_sidebar['id']; 484 ?> 485 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 486 <div class="widget-holder inactive"> 487 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?> 488 489 <?php if ( $is_inactive_widgets ) { ?> 490 <div class="remove-inactive-widgets"> 491 <form method="post"> 492 <p> 493 <?php 494 $attributes = array( 'id' => 'inactive-widgets-control-remove' ); 495 496 if ( empty( $sidebars_widgets['wp_inactive_widgets'] ) ) { 497 $attributes['disabled'] = ''; 498 } 499 500 submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes ); 501 ?> 502 <span class="spinner"></span> 503 </p> 504 <?php wp_nonce_field( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); ?> 505 </form> 506 </div> 507 <?php } ?> 508 </div> 509 <?php if ( $is_inactive_widgets ) { ?> 510 <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> 511 <?php } ?> 512 </div> 513 <?php 514 515 } else { 516 $theme_sidebars[ $sidebar ] = $registered_sidebar; 517 } 518 } 519 520 ?> 521 </div> 522 </div> 523 <?php 524 525 $sidebar_index = 0; 526 $split = 0; 527 $single_sidebar_class = ''; 528 $sidebars_count = count( $theme_sidebars ); 529 530 if ( $sidebars_count > 1 ) { 531 $split = (int) ceil( $sidebars_count / 2 ); 532 } else { 533 $single_sidebar_class = ' single-sidebar'; 534 } 535 536 ?> 537 <div class="widget-liquid-right"> 538 <div id="widgets-right" class="wp-clearfix<?php echo $single_sidebar_class; ?>"> 539 <div class="sidebars-column-1"> 540 <?php 541 542 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) { 543 $wrap_class = 'widgets-holder-wrap'; 544 if ( ! empty( $registered_sidebar['class'] ) ) { 545 $wrap_class .= ' sidebar-' . $registered_sidebar['class']; 546 } 547 548 if ( $sidebar_index > 0 ) { 549 $wrap_class .= ' closed'; 550 } 551 552 if ( $split && $sidebar_index === $split ) { 553 ?> 554 </div><div class="sidebars-column-2"> 555 <?php 556 } 557 558 ?> 559 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 560 <?php 561 // Show the control forms for each of the widgets in this sidebar. 562 wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); 563 ?> 564 </div> 565 <?php 566 567 ++$sidebar_index; 568 } 569 570 ?> 571 </div> 572 </div> 573 </div> 574 <form method="post"> 575 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?> 576 </form> 577 <br class="clear" /> 578 </div> 579 580 <div class="widgets-chooser"> 581 <ul class="widgets-chooser-sidebars"></ul> 582 <div class="widgets-chooser-actions"> 583 <button class="button widgets-chooser-cancel"><?php _e( 'Cancel' ); ?></button> 584 <button class="button button-primary widgets-chooser-add"><?php _e( 'Add Widget' ); ?></button> 585 </div> 586 </div> 587 588 <?php 589 590 /** 591 * Fires after the available widgets and sidebars have loaded, before the admin footer. 592 * 593 * @since 2.2.0 594 */ 595 do_action( 'sidebar_admin_page' ); 596 require_once ABSPATH . 'wp-admin/admin-footer.php';
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Sep 23 08:20:35 2026 | Cross-referenced by PHPXref |