Using WordPress ‘choose_primary_blog()’ PHP function

The choose_primary_blog() WordPress PHP function handles the display of a user’s primary site and lets the user decide which site should be the primary one.

Usage

The choose_primary_blog() function is straightforward to use since it doesn’t require any parameters. Simply call the function when you need to display or change the primary site.

choose_primary_blog();

Parameters

The choose_primary_blog() function doesn’t require any parameters.

More Information

See WordPress Developer Resources: choose_primary_blog()

This function is particularly useful when managing multi-site networks, allowing users to easily switch their primary site.

Examples

Displaying the Primary Blog

In this example, the function is used to allow the user to display and change their primary blog. It can be called in a user profile page or settings page.

// Call the choose_primary_blog() function.
choose_primary_blog();

Integrating with a Custom User Page

This code integrates the choose_primary_blog() function into a custom user page.

function display_user_settings_page() {
    // Other user settings code here...

    // Allow user to choose their primary blog.
    choose_primary_blog();

    // Other user settings code here...
}

Displaying Primary Blog in a Dashboard Widget

This function can be used in a dashboard widget to let users select their primary blog right from the dashboard.

function add_dashboard_widget() {
    wp_add_dashboard_widget('choose_primary_blog_widget', 'Choose Primary Blog', 'choose_primary_blog');
}
add_action('wp_dashboard_setup', 'add_dashboard_widget');

This example shows how to add a custom link in the admin bar that directs to a page where choose_primary_blog() is called.

function add_primary_blog_link( $wp_admin_bar ) {
    $args = array(
        'id'    => 'primary_blog',
        'title' => 'Choose Primary Blog',
        'href'  => '/your-custom-page',
        'meta'  => array( 'class' => 'primary-blog' )
    );
    $wp_admin_bar->add_node( $args );
}
add_action('admin_bar_menu', 'add_primary_blog_link', 999);

Creating a Shortcode for Primary Blog Selection

A shortcode can be created to place the primary blog selection function anywhere shortcodes are accepted.

function choose_primary_blog_shortcode() {
    ob_start();
    choose_primary_blog();
    return ob_get_clean();
}
add_shortcode('choose_primary_blog', 'choose_primary_blog_shortcode');

In this example, you can use [choose_primary_blog] in your WordPress editor to insert the primary blog selection.