switch_to_blog example

if ( ! can_edit_network( $details->site_id ) ) {
    wp_die( __( 'Sorry, you are not allowed to access this page.' ), 403 );
}

$parsed_scheme = parse_url( $details->siteurl, PHP_URL_SCHEME );
$is_main_site  = is_main_site( $id );

if ( isset( $_REQUEST['action'] ) && 'update-site' === $_REQUEST['action'] ) {
    check_admin_referer( 'edit-site' );

    switch_to_blog( $id );

    // Rewrite rules can't be flushed during switch to blog.     delete_option( 'rewrite_rules' );

    $blog_data           = wp_unslash( $_POST['blog'] );
    $blog_data['scheme'] = $parsed_scheme;

    if ( $is_main_site ) {
        // On the network's main site, don't allow the domain or path to change.         $blog_data['domain'] = $details->domain;
        $blog_data['path']   = $details->path;
    }

function current_user_can_for_blog( $blog_id$capability, ...$args ) {
    $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;

    $can = current_user_can( $capability, ...$args );

    if ( $switched ) {
        restore_current_blog();
    }

    return $can;
}

/** * Returns whether the author of the supplied post has the specified capability. * * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. * * Example usage: * * author_can( $post, 'edit_posts' ); * author_can( $post, 'edit_post', $post->ID ); * author_can( $post, 'edit_post_meta', $post->ID, $meta_key ); * * @since 2.9.0 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter * by adding it to the function signature. * * @param int|WP_Post $post Post ID or post object. * @param string $capability Capability name. * @param mixed ...$args Optional further parameters, typically starting with an object ID. * @return bool Whether the post author has the given capability. */

            return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ]$blog_id );
        }

        $current = get_current_blog_id() == $blog_id;

        if ( $current ) {
            $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
        } else {
            switch_to_blog( $blog_id );
            $allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
            restore_current_blog();
        }

        /* * This is all super old MU back compat joy. * 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name. */
        if ( false === $allowed_themes[ $blog_id ] ) {
            if ( $current ) {
                $allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
            }

function confirm_another_blog_signup( $domain$path$blog_title$user_name$user_email = '', $meta = array()$blog_id = 0 ) {

    if ( $blog_id ) {
        switch_to_blog( $blog_id );
        $home_url  = home_url( '/' );
        $login_url = wp_login_url();
        restore_current_blog();
    } else {
        $home_url  = 'http://' . $domain . $path;
        $login_url = 'http://' . $domain . $path . 'wp-login.php';
    }

    $site = sprintf(
        '<a href="%1$s">%2$s</a>',
        esc_url( $home_url ),
        


        $sites = get_sites( $qv );
        $site  = reset( $sites );

        // Do not allow embeds for deleted/archived/spam sites.         if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) {
            return false;
        }

        if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
            switch_to_blog( $site->blog_id );
            $switched_blog = true;
        }
    }

    $post_id = url_to_postid( $url );

    /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
    $post_id = apply_filters( 'oembed_request_post_id', $post_id$url );

    if ( ! $post_id ) {
        if ( $switched_blog ) {
            

function wp_cache_switch_to_blog( $blog_id ) {
    global $wp_object_cache;

    $wp_object_cache->switch_to_blog( $blog_id );
}

/** * Resets internal cache keys and structures. * * If the cache back end uses global blog or site IDs as part of its cache keys, * this function instructs the back end to reset those keys and perform any cleanup * since blog or site IDs have changed since cache init. * * This function is deprecated. Use wp_cache_switch_to_blog() instead of this * function when preparing the cache for a blog switch. For clearing the cache * during unit tests, consider using wp_cache_init(). wp_cache_init() is not * recommended outside of unit tests as the performance penalty for using it is high. * * @since 3.0.0 * @deprecated 3.5.0 Use wp_cache_switch_to_blog() * @see WP_Object_Cache::reset() * * @global WP_Object_Cache $wp_object_cache Object cache global instance. */

function get_blog_post( $blog_id$post_id ) {
    switch_to_blog( $blog_id );
    $post = get_post( $post_id );
    restore_current_blog();

    return $post;
}

/** * Adds a user to a blog, along with specifying the user's role. * * Use the {@see 'add_user_to_blog'} action to fire an event when users are added to a blog. * * @since MU (3.0.0) * * @param int $blog_id ID of the blog the user is being added to. * @param int $user_id ID of the user being added. * @param string $role User role. * @return true|WP_Error True on success or a WP_Error object if the user doesn't exist * or could not be added. */
$url = 'site-users.php?id=' . $this->site_id;
        } else {
            $url = 'users.php';
        }

        $role_links  = array();
        $avail_roles = array();
        $all_text    = __( 'All' );

        if ( $count_users ) {
            if ( $this->is_site_users ) {
                switch_to_blog( $this->site_id );
                $users_of_blog = count_users( 'time', $this->site_id );
                restore_current_blog();
            } else {
                $users_of_blog = count_users();
            }

            $total_users = $users_of_blog['total_users'];
            $avail_roles =& $users_of_blog['avail_roles'];
            unset( $users_of_blog );

            $all_text = sprintf(
                
return __( '<strong>Error:</strong> Site URL you&#8217;ve entered is already taken.' );

    /* * Need to back up wpdb table names, and create a new wp_blogs entry for new blog. * Need to get blog_id from wp_blogs, and create new table names. * Must restore table names at the end of function. */

    if ( ! $blog_id = insert_blog($domain$path$site_id) )
        return __( '<strong>Error:</strong> There was a problem creating site entry.' );

    switch_to_blog($blog_id);
    install_blog($blog_id);
    restore_current_blog();

    return $blog_id;
}

/** * Get the admin for a domain/path combination. * * @since MU (3.0.0) * @deprecated 4.4.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $domain Optional. Network domain. * @param string $path Optional. Network path. * @return array|false The network admins. */

function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
    $orig_scheme = $scheme;

    if ( empty( $blog_id ) || ! is_multisite() ) {
        $url = get_option( 'home' );
    } else {
        switch_to_blog( $blog_id );
        $url = get_option( 'home' );
        restore_current_blog();
    }

    if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
        if ( is_ssl() ) {
            $scheme = 'https';
        } else {
            $scheme = parse_url( $url, PHP_URL_SCHEME );
        }
    }

    
            printf(
                '<a href="%1$s" class="edit">%2$s</a>',
                esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ),
                $blogname
            );

            $this->site_states( $blog );
            ?> </strong> <?php         if ( 'list' !== $mode ) {
            switch_to_blog( $blog['blog_id'] );
            echo '<p>';
            printf(
                /* translators: 1: Site title, 2: Site tagline. */
                __( '%1$s &#8211; %2$s' ),
                get_option( 'blogname' ),
                '<em>' . get_option( 'blogdescription' ) . '</em>'
            );
            echo '</p>';
            restore_current_blog();
        }
    }

    

    $args = apply_filters( 'wp_initialize_site_args', $args$site$network );

    $orig_installing = wp_installing();
    if ( ! $orig_installing ) {
        wp_installing( true );
    }

    $switch = false;
    if ( get_current_blog_id() !== $site->id ) {
        $switch = true;
        switch_to_blog( $site->id );
    }

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';

    // Set up the database tables.     make_db_current_silent( 'blog' );

    $home_scheme    = 'http';
    $siteurl_scheme = 'http';
    if ( ! is_subdomain_install() ) {
        if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
            

    $show_site_icons = apply_filters( 'wp_admin_bar_show_site_icons', true );

    foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
        switch_to_blog( $blog->userblog_id );

        if ( true === $show_site_icons && has_site_icon() ) {
            $blavatar = sprintf(
                '<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16"%s />',
                esc_url( get_site_icon_url( 16 ) ),
                esc_url( get_site_icon_url( 32 ) ),
                ( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' )
            );
        } else {
            $blavatar = '<div class="blavatar"></div>';
        }

        

            if ( ! $blogs ) {
                fwrite( STDERR, "Error: Could not find blog\n" );
                exit;
            }
            $blog    = array_shift( $blogs );
            $blog_id = (int) $blog->blog_id;
        }

        if ( function_exists( 'is_multisite' ) ) {
            if ( is_multisite() ) {
                switch_to_blog( $blog_id );
            }
        }

        return $blog_id;
    }

    /** * @param int $user_id * @return int|void */
    public function set_user( $user_id ) {
        

function get_site_icon_url( $size = 512, $url = '', $blog_id = 0 ) {
    $switched_blog = false;

    if ( is_multisite() && ! empty( $blog_id ) && get_current_blog_id() !== (int) $blog_id ) {
        switch_to_blog( $blog_id );
        $switched_blog = true;
    }

    $site_icon_id = (int) get_option( 'site_icon' );

    if ( $site_icon_id ) {
        if ( $size >= 512 ) {
            $size_data = 'full';
        } else {
            $size_data = array( $size$size );
        }
        
Home | Imprint | This part of the site doesn't use cookies.