函数文档

choose_primary_blog()

💡 云策文档标注

概述

choose_primary_blog() 函数用于处理用户选择主站点的显示逻辑,允许用户从所属站点中设置主站点,并确保数据同步。

关键要点

  • 函数显示用户的主站点,并提供选择界面,适用于多站点环境。
  • 通过 get_blogs_of_user() 获取用户所属站点列表,使用 get_user_meta() 和 update_user_meta() 管理主站点设置。
  • 包含数据验证和同步机制,如当站点列表与主站点不同步时自动更新。

代码示例

function choose_primary_blog() {
    $all_blogs = get_blogs_of_user( get_current_user_id() );
    $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );
    if ( count( $all_blogs ) > 1 ) {
        $found = false;
        ?>
        <select name="primary_blog">
            <?php foreach ( $all_blogs as $blog ) : ?>
                <?php if ( $blog->userblog_id === $primary_blog ) {
                    $found = true;
                }
                ?>
                <option value="<?php echo $blog->userblog_id; ?>" <?php selected( $blog->userblog_id, $primary_blog ); ?>><?php echo esc_url( get_home_url( $blog->userblog_id ) ); ?></option>
            <?php endforeach; ?>
        </select>
        <?php
        if ( ! $found && $primary_blog ) {
            update_user_meta( get_current_user_id(), 'primary_blog', $primary_blog );
        }
    } elseif ( 1 === count( $all_blogs ) ) {
        $blog = reset( $all_blogs );
        echo esc_url( get_home_url( $blog->userblog_id ) );
        if ( $blog->userblog_id !== $primary_blog ) {
            update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
        }
    } else {
        _e( 'Not available' );
    }
}

注意事项

  • 函数依赖于多站点功能,仅在 WordPress 多站点环境中有效。
  • 使用 selected() 函数输出 HTML selected 属性,确保选项正确选中。
  • 通过 esc_url() 清理 URL 输出,增强安全性。

📄 原文内容

Handles the display of choosing a user’s primary site.

Description

This displays the user’s primary site and allows the user to choose which site is primary.

Source

function choose_primary_blog() {
?>
<table class="form-table" role="presentation">
<tr>

<th scope="row"><label for="primary_blog"></label></th>
<td>
1 ) {
$found = false;
?>
<select name="primary_blog" id="primary_blog">
userblog_id === $primary_blog ) {
$found = true;
}
?>
<option value="<?php echo $blog->userblog_id; ?>"<?php selected( $primary_blog, $blog->userblog_id ); ?>>userblog_id ) ); ?></option>

</select>
userblog_id );
}
} elseif ( 1 === count( $all_blogs ) ) {
$blog = reset( $all_blogs );
echo esc_url( get_home_url( $blog->userblog_id ) );
if ( $blog->userblog_id !== $primary_blog ) { // Set the primary blog again if it's out of sync with blog list.
update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id );
}
} else {
_e( 'Not available' );
}
?>
</td>
</tr>
</table>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/ms.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ms.php#L766">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ms.php#L766-L810">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/selected/">selected()</a><code>wp-includes/general-template.php

Outputs the HTML selected attribute.

get_home_url()wp-includes/link-template.php

Retrieves the URL for a given site where the front end is accessible.

get_blogs_of_user()wp-includes/user.php

Gets the sites a user belongs to.

get_user_meta()wp-includes/user.php

Retrieves user meta field for a user.

update_user_meta()wp-includes/user.php

Updates user meta field based on user ID.

_e()wp-includes/l10n.php

Displays translated text.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

get_current_user_id()wp-includes/user.php

Gets the current user’s ID.

Show 3 moreShow less

Changelog

Version Description
3.0.0 Introduced.