1. Code
  2. Plugins

Custom Database Tables: Safety First

This post is part of a series called Custom Database Tables.
Custom Database Tables: Creating the Table
Custom Database Tables: Creating an API

This is part two of a series about custom database tables in WordPress. In part one we covered the reasons for, and against, using custom tables. We looked at some of the details that would need to be considered - column naming, column types - as well as how to create the table. Before we go any further we need to cover how to interact with this new table safely. In a previous article I covered general sanitisation and validation – in this tutorial we’ll look at this in more detail in the context of databases.

Safety when interacting with a database table is paramount – which is why we’re covering it early on in the series. If not done correctly then you can leave your table open to manipulation via SQL injection. It could allow a hacker to extract information, replace content or even alter the way your site behaves - and the damage they could do is not restricted to your custom table.

Let's suppose we want allow administrators to delete records from our activity log. A common mistake I’ve seen is the following:

So what’s wrong here? Plenty: They’ve not checked permissions, so anyone can delete an activity log. Nor have they checked nonces, so even with permission checks, an admin user could be tricked into deleting a log. This was all covered in this tutorial. But their third mistake compounds the first two: the unsafe_delete_log() function uses the passed value in an SQL command without escaping it first. This leaves it wide open to manipulation.

Let’s suppose its intended use is

What if an attacker visited (or tricked an admin into visiting):;%20DROP%20TABLE%20wp_posts. The log_id contains an SQL command, which is subsequently injected into $sql and would be executed as:

The result: the entire wp_posts table is deleted. I’ve seen code like this on forums – and the result is that anyone visiting their site can update or delete any table in their database.

If the first two mistakes were corrected, then it does make it harder for this type of attack to work – but not impossible, and it wouldn’t protect against an ‘attacker’ who has permission to delete activity logs. It’s incredibly important to protect your site against SQL injections. It’s also incredibly simple: WordPress provides the prepare method. In this particular example:

The SQL command would now execute as

Sanitising Database Queries

Most sanitisation can be performed solely using the $wpdb global – notably through its prepare method. It also provides methods for inserting and updating data into tables safely. These usually work by replacing an unknown input, or associating an input, with a format placeholder. This format tells WordPress what data it is to expect:

  • %s denotes a string
  • %d denotes an integer
  • %f denotes a float

We start by looking at three methods which not only sanitize queries - but build them for you too.

Inserting Data

WordPress provides the method $wpdb->insert(). It’s a wrapper for inserting data into the database and handles the sanitisation. It takes three parameters:

  • Table name – the name of the table
  • Data – array of data to insert as column->value pairs
  • Formats – array of formats for the corresponding value in the data array (e.g. %s, %d,%f)

Note that the keys of the data should be columns: if there is a key that does not match a column an error may be thrown.

In the examples that follow we have explicitly set the data - but of course, in general, this data would have come from user input - so it could be anything. As discussed in this article the data should have been validated first, so as to return any errors to the user - but we still need to sanitize the data before adding it to our table. We'll be looking at validation in the next article of this series.

Updating Data

For updating data in the database we have $wpdb->update(). This method accepts five arguments:

  • Table name – the name of the table
  • Data – array of data to update as column->value pairs
  • Where – array of data to match as column->value pairs
  • Data Format – array of formats for the corresponding ‘data’ values
  • Where Format – array of formats for the corresponding ‘where’ values

This updates any rows that matching the where array with values from the data array. Again, as with $wpdb->insert() the keys of the data array must match a column. It returns false on error, or the number of rows updated.

In the following example we update any records with log ID '14' (which should be at most one record, as this is our primary key). It updates the user ID to 2 and activity to 'edited'.


Since 3.4 WordPress has also provided the $wpdb->delete() method for easily (and safely) deleting row(s). This method takes three parameters:

  • Table name – the name of the table
  • Where – array of data to match as column->value pairs
  • Formats – array of formats for the corresponding value type (e.g. %s, %d,%f)

If you want your code to be compatible with WordPress pre-3.4, then you will need to use the $wpdb->prepare method to sanitise the appropriate SQL statement. An example of this was given above. The $wpdb->delete method returns the number of rows delete, or false otherwise – so you can determine if the deletion was successful.


In light of the above methods, and the more general $wpdb->prepare() method discussed next, this function is a bit redundant. Its provided as a useful wrapper for the $wpdb->escape() method, itself a glorified addslashes. Since its usually more appropriate, and advisable, to use the above three methods, or $wpdb->prepare(), you will probably find that you rarely need to use esc_sql().

As a simple example:

General Queries

For general SQL commands where (i.e. those not inserting, removing or updating rows) we have to use method $wpdb->prepare(). It accepts a variable number of arguments. The first is the SQL query we wish to execute with all ‘unknown’ data replaced by their appropriate format placeholder. These values are passed as additional arguments, in the order that they appear.

For instance instead of:

we have

The prepare method does two things.

  1. It applies mysql_real_escape_string() (or addslashes()) to the values being inserted. In particular this will prevent values containing quotation marks from jumping out of the query.
  2. It applies vsprintf() when adding the values to the query to ensure they are formatted appropriately (so integers are integers, floats are floats etc). This is why our example at the very beginning of the article stripped out everything but the '1'.

More Complicated Queries

You should find that $wpdb->prepare, along with the insert, update and delete methods are all you really need. Sometimes though there are circumstances where a more ‘manual’ approach is desired - sometimes just from readability point of view. For example, suppose we have an unknown array of activities for which we want all the logs. We *could* dynamically add the %s placeholders to the SQL query, but a more direct approach seems easier:

The idea is to apply esc_sql and sanitize_title_for_query to each element in the array. The first adds slashes to escape the terms – similar to what $wpdb->prepare() does. The second simply applies sanitize_title_with_dashes() – though the behaviour can be completely modified through filters. The actual SQL statement is formed by imploding the now sanitised array into a comma separated string, which is added into the IN(...) part of the query.

If the array is expected to contain integers then it suffices to use intval() or absint() to sanitize each element in the array.


In other cases whitelisting may be appropriate. For instance the unknown input may be an array of columns that are to be returned in the query. Since we know what the columns of the database are we can simply whitelist them – removing any fields which we don’t recognise. However, to make our code human friendly we should be case-insensitive. To do this we’ll convert anything we receive to lowercase – since in part one we specifically used lowercase column names.

Whitelisting is also convenient when setting the ORDER BY part of the query (if this is set by user input): data can be ordered as DESC or ASC only.

LIKE Queries

SQL LIKE statements support the use of wildcards such as % (zero or more characters) and _ (exactly one character) when matching values to the query. For instance the value foobar would match any of the queries:

However, these special characters may actually be present in the term being searched for – and so to prevent them from being interpreted as wildcards – we need to escape them. For this WordPress provides the like_escape() function. Note that this does not prevent SQL injection – but only escapes the % and _ characters: you still need to use esc_sql() or $wpdb->prepare().

Query Wrapper Functions

In the examples we’ve looked we’ve used two other methods of $wpdb:

  • $wpdb->query( $sql ) – This performs any query given to it and returns the number of affected rows.
  • $wpdb->get_results( $sql, $ouput) – This performs the query given to it and returns the matching result set (i.e. the matching rows). $output sets the format of the returned results:
    • ARRAY_A – numerical array of rows, where each row is an associative array, keyed by the columns.
    • ARRAY_N – numerical array of rows, where each row is a numerical array.
    • OBJECT – numerical array of rows, where each row is a row object. Default.
    • OBJECT_K – associative array of rows (keyed by the value of the first column), where each row is an associative array.

There are others we haven’t mentioned too:

  • $wpdb->get_row( $sql, $ouput, $row) – This performs the query and returns one row. $row sets which row is to be returned, by default this is 0, the first matching row. $output sets the format of the row:
    • ARRAY_A – Row is a column=>value pair.
    • ARRAY_N – Row is numerical array of values.
    • OBJECT – Row is returned as an object. Default.
  • $wpdb->get_col( $sql, $column) – This performs the query and returns a numerical array of values from the specified column. $column specifies which column to return as integer. By default this is 0, the first column.
  • $wpdb->get_var( $sql, $column, $row) – This performs the query and returns a particular value. $row and $column are as above, and specify which value to return. For example,

It’s important to note that these methods are just wrappers for performing an SQL query and formatting the result. They do not sanitize the query – so you should not be using them alone when the query contains some ‘unknown’ data.


We've covered quite a lot in this tutorial - and data sanitisation is an important topic to understand. In the next article we'll be applying it to our plug-in. We'll be looking at developing a set of wrapper functions (similar to functions like wp_insert_post(), wp_delete_post() etc.) that will add a layer of abstraction between our plug-in and the database.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.