"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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.