set_user_setting( string $name, string $value ): bool|null

Adds or updates user interface setting.

Description

Both $name and $value can contain only ASCII letters, numbers, hyphens, and underscores.

This function has to be used before any output has started as it calls setcookie().

Parameters

$namestringrequired
The name of the setting.
$valuestringrequired
The value for the setting.

Return

bool|null True if set successfully, false otherwise.
Null if the current user is not a member of the site.

Source

function set_user_setting( $name, $value ) {
	if ( headers_sent() ) {
		return false;
	}

	$all_user_settings          = get_all_user_settings();
	$all_user_settings[ $name ] = $value;

	return wp_set_all_user_settings( $all_user_settings );
}

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Override the user setting to always collapse the admin menu for the current user:

    /**
     * Re-set user setting to always collapse the admin menu.
     *
     * @see set_user_setting()
     */
    function wpdocs_always_collapse_menu() {
    	if ( 'f' != get_user_setting( 'mfold' ) ) {
    		set_user_setting( 'mfold', 'f' );
    	}
    }
    add_action( 'admin_head', 'wpdocs_always_collapse_menu' );

You must log in before being able to contribute a note or feedback.