wp_get_sidebars_widgets example

'<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>',
    )
);

get_current_screen()->set_help_sidebar(
    '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    '<p>' . __( '<a href="https://wordpress.org/documentation/article/appearance-widgets-screen-classic-editor/">Documentation on Widgets</a>' ) . '</p>' .
    '<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
);

// These are the widgets grouped by sidebar. $sidebars_widgets = wp_get_sidebars_widgets();

if ( empty( $sidebars_widgets ) ) {
    $sidebars_widgets = wp_get_widget_defaults();
}

foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
    if ( 'wp_inactive_widgets' === $sidebar_id ) {
        continue;
    }

    if ( ! is_registered_sidebar( $sidebar_id ) ) {
        
/** * Checks if a given request has access to get sidebars. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */
    public function get_items_permissions_check( $request ) {
        $this->retrieve_widgets();
        foreach ( wp_get_sidebars_widgets() as $id => $widgets ) {
            $sidebar = $this->get_sidebar( $id );

            if ( ! $sidebar ) {
                continue;
            }

            if ( $this->check_read_permission( $sidebar ) ) {
                return true;
            }
        }

        
$index = "sidebar-$index";
    } else {
        $index = sanitize_title( $index );
        foreach ( (array) $wp_registered_sidebars as $key => $value ) {
            if ( sanitize_title( $value['name'] ) === $index ) {
                $index = $key;
                break;
            }
        }
    }

    $sidebars_widgets = wp_get_sidebars_widgets();
    if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
        /** This action is documented in wp-includes/widget.php */
        do_action( 'dynamic_sidebar_before', $index, false );
        /** This action is documented in wp-includes/widget.php */
        do_action( 'dynamic_sidebar_after', $index, false );
        /** This filter is documented in wp-includes/widget.php */
        return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
    }

    $sidebar = $wp_registered_sidebars[ $index ];

    

    public function override_sidebars_widgets_for_theme_switch() {
        global $sidebars_widgets;

        if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) {
            return;
        }

        $this->old_sidebars_widgets = wp_get_sidebars_widgets();
        add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
        $this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset.
        // retrieve_widgets() looks at the global $sidebars_widgets.         $sidebars_widgets = $this->old_sidebars_widgets;
        $sidebars_widgets = retrieve_widgets( 'customize' );
        add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
        // Reset global cache var used by wp_get_sidebars_widgets().         unset( $GLOBALS['_wp_sidebars_widgets'] );
    }

    

    public function get_items_permissions_check( $request ) {
        $this->retrieve_widgets();
        if ( isset( $request['sidebar'] ) && $this->check_read_sidebar_permission( $request['sidebar'] ) ) {
            return true;
        }

        foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
            if ( $this->check_read_sidebar_permission( $sidebar_id ) ) {
                return true;
            }
        }

        return $this->permissions_check( $request );
    }

    /** * Retrieves a collection of widgets. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */

    /** This action is documented in wp-admin/widgets.php */
    do_action( 'sidebar_admin_setup' );

    $id_base      = wp_unslash( $_POST['id_base'] );
    $widget_id    = wp_unslash( $_POST['widget-id'] );
    $sidebar_id   = $_POST['sidebar'];
    $multi_number = ! empty( $_POST['multi_number'] ) ? (int) $_POST['multi_number'] : 0;
    $settings     = isset( $_POST[ 'widget-' . $id_base ] ) && is_array( $_POST[ 'widget-' . $id_base ] ) ? $_POST[ 'widget-' . $id_base ] : false;
    $error        = '<p>' . __( 'An error has occurred. Please reload the page and try again.' ) . '</p>';

    $sidebars = wp_get_sidebars_widgets();
    $sidebar  = isset( $sidebars[ $sidebar_id ] ) ? $sidebars[ $sidebar_id ] : array();

    // Delete.     if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {

        if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
            wp_die( $error );
        }

        $sidebar = array_diff( $sidebar, array( $widget_id ) );
        $_POST   = array(
            
Home | Imprint | This part of the site doesn't use cookies.