Title: choose_primary_blog
Published: April 25, 2014
Last modified: February 24, 2026

---

# choose_primary_blog()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#description)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#source)󠁿

    ```php
    function choose_primary_blog() {
    	?>
    	<table class="form-table" role="presentation">
    	<tr>
    	<?php /* translators: My Sites label. */ ?>
    		<th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th>
    		<td>
    		<?php
    		$all_blogs    = get_blogs_of_user( get_current_user_id() );
    		$primary_blog = (int) get_user_meta( get_current_user_id(), 'primary_blog', true );
    		if ( count( $all_blogs ) > 1 ) {
    			$found = false;
    			?>
    			<select name="primary_blog" id="primary_blog">
    				<?php
    				foreach ( (array) $all_blogs as $blog ) {
    					if ( $blog->userblog_id === $primary_blog ) {
    						$found = true;
    					}
    					?>
    					<option value="<?php echo $blog->userblog_id; ?>"<?php selected( $primary_blog, $blog->userblog_id ); ?>><?php echo esc_url( get_home_url( $blog->userblog_id ) ); ?></option>
    					<?php
    				}
    				?>
    			</select>
    			<?php
    			if ( ! $found ) {
    				$blog = reset( $all_blogs );
    				update_user_meta( get_current_user_id(), 'primary_blog', $blog->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>
    	<?php
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ms.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ms.php#L766)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ms.php#L766-L810)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#related)󠁿

| Uses | Description | 
| [selected()](https://developer.wordpress.org/reference/functions/selected/)`wp-includes/general-template.php` |

Outputs the HTML selected attribute.

  | 
| [get_home_url()](https://developer.wordpress.org/reference/functions/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()](https://developer.wordpress.org/reference/functions/get_blogs_of_user/)`wp-includes/user.php` |

Gets the sites a user belongs to.

  | 
| [get_user_meta()](https://developer.wordpress.org/reference/functions/get_user_meta/)`wp-includes/user.php` |

Retrieves user meta field for a user.

  | 
| [update_user_meta()](https://developer.wordpress.org/reference/functions/update_user_meta/)`wp-includes/user.php` |

Updates user meta field based on user ID.

  | 
| [_e()](https://developer.wordpress.org/reference/functions/_e/)`wp-includes/l10n.php` |

Displays translated text.

  | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/choose_primary_blog/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fchoose_primary_blog%2F)
before being able to contribute a note or feedback.