Add custom user settings
Every member has a Settings screen in the messenger, opened from their own photo at the bottom of the messenger. It holds their personal choices: email notifications, sounds, who may start a conversation with them, how Enter sends a message and so on. A plugin or a snippet can add its own options to that screen, and Better Messages saves what the member picks through the same request as its own options.
It takes two hooks:
better_messages_user_configadds your options to the screen, with the value the member has nowbetter_messages_user_setting_savedruns when the member changes an option, and is where you store the new value
Better Messages does not store your options for you. Keep them where your code reads them, usually in user meta.
To be able to implement this guide, you need to learn how to insert PHP snippets to your website.
You can find guide here: WP Beginner
A complete example#
The snippet adds two options for members:
- Daily digest, a checkbox: whether the member wants a daily email of unread messages. Your own digest code reads it
- Message text size, a choice of Small, Normal and Large: the text size of the messages, applied on the next page load
<?php
// 1. Show the options on the member's settings screen.
add_filter( 'better_messages_user_config', 'my_bm_user_settings', 10, 2 );
function my_bm_user_settings( $settings, $user_id ) {
// Members only: guests have no WordPress user meta.
if ( $user_id <= 0 ) {
return $settings;
}
$settings[] = array(
'id' => 'my_digest',
'title' => 'Daily digest',
'type' => 'checkboxes',
'options' => array(
array(
'id' => 'my_digest_email',
'label' => 'Email me a daily digest of unread messages',
'value' => 'yes',
'checked' => get_user_meta( $user_id, 'my_digest_email', true ) === 'yes',
'desc' => 'Sent once a day, only when you have unread messages',
),
),
);
$current = my_bm_text_size( $user_id );
$options = array();
foreach ( my_bm_text_sizes() as $value => $label ) {
$options[] = array(
'id' => $value,
'label' => $label,
'value' => $value,
'checked' => $current === $value,
'desc' => '',
);
}
$settings[] = array(
'id' => 'my_text_size',
'title' => 'Message text size',
'type' => 'radio',
'options' => $options,
);
return $settings;
}
// 2. Store what the member picked.
add_action( 'better_messages_user_setting_saved', 'my_bm_user_setting_saved', 10, 3 );
function my_bm_user_setting_saved( $option, $value, $user_id ) {
if ( $user_id <= 0 ) {
return;
}
// A checkbox is saved under its own id, as 'true' or 'false'.
if ( $option === 'my_digest_email' ) {
update_user_meta( $user_id, 'my_digest_email', $value === 'true' ? 'yes' : 'no' );
}
// A radio group is saved under the group id, with the value of the chosen option.
if ( $option === 'my_text_size' && array_key_exists( $value, my_bm_text_sizes() ) ) {
update_user_meta( $user_id, 'my_text_size', $value );
}
}
function my_bm_text_sizes() {
return array(
'small' => 'Small',
'normal' => 'Normal',
'large' => 'Large',
);
}
function my_bm_text_size( $user_id ) {
$size = get_user_meta( $user_id, 'my_text_size', true );
return array_key_exists( $size, my_bm_text_sizes() ) ? $size : 'normal';
}
// 3. Use the value: set the messenger's message text size for this member.
add_action( 'wp_head', 'my_bm_text_size_css' );
function my_bm_text_size_css() {
if ( ! is_user_logged_in() ) {
return;
}
$sizes = array( 'small' => '13px', 'large' => '18px' );
$size = my_bm_text_size( get_current_user_id() );
if ( isset( $sizes[ $size ] ) ) {
echo '<style>html:root{--bm-font-size-base:' . esc_attr( $sizes[ $size ] ) . '}</style>';
}
}
The two groups appear on the settings screen after the options of Better Messages, with the current choice of the member already marked. A change is saved the moment the member clicks it, and the messenger shows its usual "Saved successfully" notice.
The text size uses the token --bm-font-size-base, the same one as Settings → Appearance → Typography (see Change text size). html:root is a little more specific than :root, so the choice of the member wins over the size set in Appearance.
To find the members who want the digest, query the user meta:
$members = get_users( array(
'meta_key' => 'my_digest_email',
'meta_value' => 'yes',
'fields' => 'ID',
) );