Halloween Sale 15% off Ends in See the plans
PHP

Personal Message Box for Individual Members

Lets administrators write a private note on any user's profile that only that user sees, through a [wpide_my_message] shortcode and on their own dashboard.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'edit_user_profile', function ( $user ) {
    if ( ! current_user_can( 'edit_users' ) ) {
        return;
    }

    wp_nonce_field( 'wpide_member_message_' . $user->ID, 'wpide_member_message_nonce' );
    printf(
        '<h2>Personal message</h2><table class="form-table" role="presentation"><tr><th scope="row"><label for="wpide-member-message">Message for this user</label></th><td><textarea id="wpide-member-message" name="wpide_member_message" rows="4" class="large-text">%s</textarea><p class="description">Shown only to this user. Leave empty to remove.</p></td></tr></table>',
        esc_textarea( get_user_meta( $user->ID, 'wpide_member_message', true ) )
    );
} );

add_action( 'edit_user_profile_update', function ( $user_id ) {
    if ( ! isset( $_POST['wpide_member_message_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_member_message_nonce'] ), 'wpide_member_message_' . $user_id ) || ! current_user_can( 'edit_users' ) ) {
        return;
    }

    $message = isset( $_POST['wpide_member_message'] ) ? wp_kses_post( wp_unslash( $_POST['wpide_member_message'] ) ) : '';

    if ( '' === trim( $message ) ) {
        delete_user_meta( $user_id, 'wpide_member_message' );
    } else {
        update_user_meta( $user_id, 'wpide_member_message', $message );
    }
} );

add_shortcode( 'wpide_my_message', function () {
    $message = is_user_logged_in() ? get_user_meta( get_current_user_id(), 'wpide_member_message', true ) : '';

    return $message ? '<div class="wpide-my-message" role="note">' . wpautop( wp_kses_post( $message ) ) . '</div>' : '';
} );

add_action( 'admin_notices', function () {
    global $pagenow;

    $message = in_array( $pagenow, array( 'index.php', 'profile.php' ), true ) ? get_user_meta( get_current_user_id(), 'wpide_member_message', true ) : '';

    if ( $message ) {
        echo '<div class="notice notice-info">' . wpautop( wp_kses_post( $message ) ) . '</div>';
    }
} );

Notes

Useful for client portals ("Your invoice is ready") and member dashboards. Basic formatting and links are allowed.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.