Halloween Sale 15% off Ends in See the plans
PHP

"My Notes" Personal Dashboard Widget

Adds a "My notes" widget to the dashboard where each user keeps their own private notes and reminders, saved instantly and invisible to everyone else.

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

Code

add_action( 'wp_dashboard_setup', function () {
    wp_add_dashboard_widget( 'wpide_private_notes', 'My notes', function () {
        $note = (string) get_user_meta( get_current_user_id(), 'wpide_private_note', true );
        ?>
        <label for="wpide-private-note" class="screen-reader-text">My notes</label>
        <textarea id="wpide-private-note" rows="6" style="width:100%" maxlength="5000" placeholder="Only you can see these notes."><?php echo esc_textarea( $note ); ?></textarea>
        <p class="description" id="wpide-note-status" aria-live="polite"></p>
        <script>
        ( function () {
            var box = document.getElementById( 'wpide-private-note' );
            var status = document.getElementById( 'wpide-note-status' );
            var saved = box.value;
            box.addEventListener( 'blur', function () {
                if ( box.value === saved ) { return; }
                var body = new FormData();
                body.append( 'action', 'wpide_private_note' );
                body.append( 'nonce', <?php echo wp_json_encode( wp_create_nonce( 'wpide_private_note' ) ); ?> );
                body.append( 'note', box.value );
                status.textContent = 'Saving...';
                fetch( ajaxurl, { method: 'POST', credentials: 'same-origin', body: body } )
                    .then( function ( r ) { return r.json(); } )
                    .then( function ( r ) { saved = box.value; status.textContent = r.success ? 'Saved.' : 'Could not save.'; } );
            } );
        } )();
        </script>
        <?php
    } );
} );

add_action( 'wp_ajax_wpide_private_note', function () {
    check_ajax_referer( 'wpide_private_note', 'nonce' );
    $note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : '';
    update_user_meta( get_current_user_id(), 'wpide_private_note', mb_substr( $note, 0, 5000 ) );
    wp_send_json_success();
} );

Notes

Notes are stored per user, up to 5,000 characters, and saved when you click away from the box.

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.