Write Down Simple Note on WP Dashboard

Every now and then whenever I work on a site, sometimes I need to write down certain notes for my site. I definitely feel urge to have a simple note taker built into my site. So that I don’t need to go back and forth all over my computer to look for my notes. Over the years, it has become an utmost necessity for me. So I finally decided to take things on my own hand and come up with a simple solution.

Quick Note Dashboard WidgetSimple Quick Note Widget on WordPress Dashboard.

What I am trying to do is to create a simple widget (take a look at the screenshot) on WordPress Dashboard that can save simple information on our database. Please bare in mind that I am not trying to save multiple notes which I can think can easily be achieved using Custom Post Type. Even though this basic widget looks very similar to the “Quick Draft” widget, they are fundamentally very different. The “Quick Draft” widget saves drafts on the “Posts” section, whereas my form will save the data straight to the “wp_options” table of your WP installation. Ok, enough intro now lets get back to the actual work.

Registering the Widget

As I mentioned earlier, I wanted a widget on my “Dashboard”. Very normally I have to register a custom dashboard widget. In one of my previous post, I already discussed on this issue. So, take a look at the post if you don’t understand the following snippet.

function ib_dashboard_widget_quick_note() {
   wp_add_dashboard_widget('ib_quick_note','Quick Note','ib_quick_note_form');}

So far so good. Let’s head for the real function.

Magic Function

If you read the previous portion of my code, you will see that I declared a callback function within the “wp_add_dashboard_widget” function which is “ib_quick_note_form”. This is the function that will create the content within my widget. It includes a form and it also performs the read and write operation on our database. Here is the snippet.

function ib_quick_note_form() {
   $data = htmlspecialchars(get_option('quick_note'));
   if (isset($_POST['submitted'])) {
      $note = trim($_POST['quick_note']);
      if (get_option('quick_note') !== false) {
         update_option('quick_note', $note);
      } else { add_option ('quick_note', $note); }
      echo '<meta https-equiv="refresh" content="0">';
   } else {
   echo '<form method="post" action="'.admin_url('index.php').'">';
   echo '<div class="textarea-wrap" id="quick-note">';
   echo '<textarea id="quick_note" name="quick_note" rows="5" cols="15" placeholder="Enter your note here.">'.$data.'</textarea></div>';
   echo '<input type="submit" id="save" value="Save Note" class="button-primary">';
   echo '<input type="hidden" name="submitted" id="submitted" value="true"></form>'; }	

That’s all you need. Now, simply copy both the snippet on your theme’s functions.php page and update it. You are done.

How it Works?

The first portion of my snippet basically registers a custom dashboard widget. However on the second portion (callback function) of my snippet, at the very beginning of my snippet I declared a variable ($data) which should hold the value of our option name (quick_note) from the WP’s option table. This is particularly important to display the data from database to our <textarea> field (if there is any). Right after, I am checking if the “Save Note” button has be pressed or not. If it is pressed, we will hold the content added within the textarea field and hold it on a variable ($note). Then we go on checking if there is any option name (quick_note) does exist on our “options” database or not. If there isn’t any, it should create one or if it exists then we will simply update the option value with the user’s input. Finally, we are displaying the current option value on our textarea field.

Note: This snippet was intended to create, edit and update simple texts on your database. I personally tested this snippet on my local server and on this site’s back end. It performed without any hiccups on both cases. I strongly suggest you to use this widget if you want to save simple texts. However, if you want to save large snippets or programming codes using this widget, it may or may not perform as expected. Your suggestions, recommendations and comments are highly encouraged. Thanks.

I published this snippet as a Plugin (AdminPad). Feel free to try it out.

References: add_option, get_option, wp_dashboard_setup

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.