Using WordPress ‘get_user_setting()’ PHP function

The get_user_setting() WordPress PHP function retrieves user interface setting values based on the setting name.

Usage

get_user_setting( $name, $default_value = false );

Parameters

  • $name (string) – The name of the setting.
  • $default_value (string|false) – Optional. Default value to return when $name is not set. Default: false

More information

See WordPress Developer Resources: get_user_setting

Examples

Get user setting for screen options

This example retrieves the user setting for the number of items to display per page in the admin dashboard.

// Get 'posts_per_page' user setting
$posts_per_page = get_user_setting( 'posts_per_page', 10 );

// Use the retrieved value
echo "Displaying {$posts_per_page} items per page.";

Get user setting with a default value

This example retrieves the user setting for a custom setting called ‘custom_theme_color’, with a default value of ‘blue’.

// Get 'custom_theme_color' user setting
$custom_theme_color = get_user_setting( 'custom_theme_color', 'blue' );

// Use the retrieved value
echo "The custom theme color is {$custom_theme_color}.";

Check if a user setting exists

This example checks if a user setting called ‘my_custom_setting’ exists.

// Check if 'my_custom_setting' exists
if ( false !== get_user_setting( 'my_custom_setting' ) ) {
    echo "The 'my_custom_setting' exists.";
} else {
    echo "The 'my_custom_setting' does not exist.";
}

Get user setting and handle non-existent setting

This example retrieves the user setting for a custom setting called ‘custom_font_size’ and handles the case when the setting does not exist.

// Get 'custom_font_size' user setting
$custom_font_size = get_user_setting( 'custom_font_size', false );

if ( false !== $custom_font_size ) {
    echo "The custom font size is {$custom_font_size}.";
} else {
    echo "The custom font size setting does not exist.";
}

Get multiple user settings

This example retrieves multiple user settings and stores them in an array.

// Get multiple user settings
$user_settings = array(
    'posts_per_page' => get_user_setting( 'posts_per_page', 10 ),
    'custom_theme_color' => get_user_setting( 'custom_theme_color', 'blue' ),
    'custom_font_size' => get_user_setting( 'custom_font_size', '14px' ),
);

// Use the retrieved values
foreach ( $user_settings as $key => $value ) {
    echo "{$key}: {$value}\n";
}