A plugin with database access – capturing searched words

We're going to leave the document types plugin behind now, and create a new onefeaturing active use of a database. Let's create a simple plugin that stores all the words that visitors search for (when using the blog's search feature).

Overall, this plugin is very basic, and doesn't require its own directory like the previous plugin that we worked on. That's why this whole plugin will be done inside one file: capture_searches_new.php. I'm calling the plugin Capture Searched Words New.

The database table structure for this plugin will be as follows. The table name is wp_searchedwords:

Field Type Null Key Default Extra
Id INT NOT NULL PRI - auto_increment
Word VARCHAR(255) - - NULL -
created DATETIME NOT NULL - Today 00:00:01 -

 

Now, let's write the plugin code.

Even though I say that the table is named wp_searchedwords, it won't always be the case. It's all based on the table prefix that's set for your website (the default one is indeed wp_). Here, I'm going to refer to the table as wp_searchedwords anyway, for convenience.