: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
unset( $old_sidebars_widgets[ $new_sidebar ] );
// Remove orphaned widgets, we're only interested in previously active sidebars.
foreach ( $old_sidebars_widgets as $sidebar => $widgets ) {
if ( str_starts_with( $sidebar, 'orphaned_widgets' ) ) {
unset( $old_sidebars_widgets[ $sidebar ] );
$old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets );
if ( ! empty( $old_sidebars_widgets ) ) {
// Go through each remaining sidebar...
foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) {
// ...and check every new sidebar...
foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) {
// ...for every widget we're trying to revive.
foreach ( $old_widgets as $key => $widget_id ) {
$active_key = array_search( $widget_id, $new_widgets, true );
// If the widget is used elsewhere...
if ( false !== $active_key ) {
// ...and that elsewhere is inactive widgets...
if ( 'wp_inactive_widgets' === $new_sidebar ) {
// ...remove it from there and keep the active version...
unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] );
// ...otherwise remove it from the old sidebar and keep it in the new one.
unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] );
} // End if ( $active_key ).
} // End foreach ( $old_widgets as $key => $widget_id ).
} // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ).
} // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ).
} // End if ( ! empty( $old_sidebars_widgets ) ).
// Restore widget settings from when theme was previously active.
$new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets );
return $new_sidebars_widgets;
* Compares a list of sidebars with their widgets against an allowed list.
* @global array $wp_registered_widgets The registered widgets.
* @param array $sidebars_widgets List of sidebars and their widget instance IDs.
* @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets.
* @return array Sidebars with allowed widgets.
function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) {
if ( empty( $allowed_widget_ids ) ) {
$allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] );
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if ( is_array( $widgets ) ) {
$sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids );
return $sidebars_widgets;
* Display the RSS entries in a list.
* @param string|array|object $rss RSS url.
* @param array $args Widget arguments.
function wp_widget_rss_output( $rss, $args = array() ) {
if ( is_string( $rss ) ) {
$rss = fetch_feed( $rss );
} elseif ( is_array( $rss ) && isset( $rss['url'] ) ) {
$rss = fetch_feed( $rss['url'] );
} elseif ( ! is_object( $rss ) ) {
if ( is_wp_error( $rss ) ) {
if ( is_admin() || current_user_can( 'manage_options' ) ) {
echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</p>';
$args = wp_parse_args( $args, $default_args );
$items = (int) $args['items'];
if ( $items < 1 || 20 < $items ) {
$show_summary = (int) $args['show_summary'];
$show_author = (int) $args['show_author'];
$show_date = (int) $args['show_date'];
if ( ! $rss->get_item_quantity() ) {
echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
foreach ( $rss->get_items( 0, $items ) as $item ) {
$link = $item->get_link();
while ( ! empty( $link ) && stristr( $link, 'http' ) !== $link ) {
$link = substr( $link, 1 );
$link = esc_url( strip_tags( $link ) );
$title = esc_html( trim( strip_tags( $item->get_title() ) ) );
$title = __( 'Untitled' );
$desc = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
$desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) );
// Change existing [...] to […].
if ( str_ends_with( $summary, '[...]' ) ) {
$summary = substr( $summary, 0, -5 ) . '[…]';
$summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>';
$date = $item->get_date( 'U' );
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
$author = $item->get_author();
if ( is_object( $author ) ) {
$author = $author->get_name();
$author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
echo "<li>$title{$date}{$summary}{$author}</li>";
} elseif ( $show_summary ) {
echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>";
echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>";
* Display RSS widget options form.
* The options for what fields are displayed for the RSS form are all booleans
* and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
* @param array|string $args Values for input fields.
* @param array $inputs Override default display options.
function wp_widget_rss_form( $args, $inputs = null ) {
$inputs = wp_parse_args( $inputs, $default_inputs );
$args['title'] = isset( $args['title'] ) ? $args['title'] : '';
$args['url'] = isset( $args['url'] ) ? $args['url'] : '';
$args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0;
if ( $args['items'] < 1 || 20 < $args['items'] ) {
$args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary'];
$args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author'];
$args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date'];
if ( ! empty( $args['error'] ) ) {
echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $args['error'] ) . '</p>';
$esc_number = esc_attr( $args['number'] );
<p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label>
<input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p>
<?php endif; if ( $inputs['title'] ) : ?>
<p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label>
<input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p>
<?php endif; if ( $inputs['items'] ) : ?>
<p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label>
<select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]">
for ( $i = 1; $i <= 20; ++$i ) {
echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>";
<?php endif; if ( $inputs['show_summary'] || $inputs['show_author'] || $inputs['show_date'] ) : ?>
<?php if ( $inputs['show_summary'] ) : ?>
<input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> />
<label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label><br />
<?php endif; if ( $inputs['show_author'] ) : ?>
<input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> />
<label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label><br />
<?php endif; if ( $inputs['show_date'] ) : ?>
<input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/>
<label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label><br />
endif; // End of display options.
foreach ( array_keys( $default_inputs ) as $input ) :
if ( 'hidden' === $inputs[ $input ] ) :
$id = str_replace( '_', '-', $input );
<input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" />
* Process RSS feed widget data and optionally retrieve feed items.
* The feed widget can not have more than 20 items or it will reset back to the
* The resulting array has the feed title, feed url, feed link (from channel),
* feed items, error (if any), and whether to show summary, author, and date.
* All respectively in the order of the array elements.
* @param array $widget_rss RSS widget feed data. Expects unescaped data.
* @param bool $check_feed Optional. Whether to check feed for errors. Default true.
function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
$items = (int) $widget_rss['items'];
if ( $items < 1 || 20 < $items ) {
$url = sanitize_url( strip_tags( $widget_rss['url'] ) );
$title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
$show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
$show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
$rss = fetch_feed( $url );
if ( is_wp_error( $rss ) ) {
$error = $rss->get_error_message();
$link = esc_url( strip_tags( $rss->get_permalink() ) );
while ( stristr( $link, 'http' ) !== $link ) {
$link = substr( $link, 1 );
return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
* Registers all of the default WordPress widgets on startup.
* Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered.
function wp_widgets_init() {
if ( ! is_blog_installed() ) {
register_widget( 'WP_Widget_Pages' );
register_widget( 'WP_Widget_Calendar' );
register_widget( 'WP_Widget_Archives' );
if ( get_option( 'link_manager_enabled' ) ) {
register_widget( 'WP_Widget_Links' );
register_widget( 'WP_Widget_Media_Audio' );
register_widget( 'WP_Widget_Media_Image' );
register_widget( 'WP_Widget_Media_Gallery' );
register_widget( 'WP_Widget_Media_Video' );
register_widget( 'WP_Widget_Meta' );
register_widget( 'WP_Widget_Search' );
register_widget( 'WP_Widget_Text' );
register_widget( 'WP_Widget_Categories' );
register_widget( 'WP_Widget_Recent_Posts' );
register_widget( 'WP_Widget_Recent_Comments' );
register_widget( 'WP_Widget_RSS' );
register_widget( 'WP_Widget_Tag_Cloud' );
register_widget( 'WP_Nav_Menu_Widget' );
register_widget( 'WP_Widget_Custom_HTML' );
register_widget( 'WP_Widget_Block' );
* Fires after all default WordPress widgets have been registered.
do_action( 'widgets_init' );
* Enables the widgets block editor. This is hooked into 'after_setup_theme' so
* that the block editor is enabled by default but can be disabled by themes.
function wp_setup_widgets_block_editor() {
add_theme_support( 'widgets-block-editor' );
* Whether or not to use the block editor to manage widgets. Defaults to true
* unless a theme has removed support for widgets-block-editor or a plugin has
* filtered the return value of this function.
* @return bool Whether to use the block editor to manage widgets.
function wp_use_widgets_block_editor() {
* Filters whether to use the block editor to manage widgets.
* @param bool $use_widgets_block_editor Whether to use the block editor to manage widgets.
'use_widgets_block_editor',
get_theme_support( 'widgets-block-editor' )
* Converts a widget ID into its id_base and number components.
* @param string $id Widget ID.
* @return array Array containing a widget's id_base and number components.
function wp_parse_widget_id( $id ) {
if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) {
$parsed['id_base'] = $matches[1];
$parsed['number'] = (int) $matches[2];
// Likely an old single widget.
$parsed['id_base'] = $id;
* Finds the sidebar that a given widget belongs to.
* @param string $widget_id The widget ID to look for.
* @return string|null The found sidebar's ID, or null if it was not found.
function wp_find_widgets_sidebar( $widget_id ) {
foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
foreach ( $widget_ids as $maybe_widget_id ) {
if ( $maybe_widget_id === $widget_id ) {
return (string) $sidebar_id;
* Assigns a widget to the given sidebar.
* @param string $widget_id The widget ID to assign.
* @param string $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar.
function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
$sidebars = wp_get_sidebars_widgets();
foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
foreach ( $widgets as $i => $maybe_widget_id ) {
if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
// We could technically break 2 here, but continue looping in case the ID is duplicated.
$sidebars[ $sidebar_id ][] = $widget_id;
wp_set_sidebars_widgets( $sidebars );
* Calls the render callback of a widget and returns the output.
* @global array $wp_registered_widgets The registered widgets.
* @global array $wp_registered_sidebars The registered sidebars.
* @param string $widget_id Widget ID.
* @param string $sidebar_id Sidebar ID.
function wp_render_widget( $widget_id, $sidebar_id ) {
global $wp_registered_widgets, $wp_registered_sidebars;
if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
$sidebar = $wp_registered_sidebars[ $sidebar_id ];
} elseif ( 'wp_inactive_widgets' === $sidebar_id ) {