Plugin authors and website developers who work with WordPress database queries should notice an important change coming in WordPress 4.0.
The function like_escape()
is no longer used in WordPress core code. It is still available as a deprecated function, so it still works in any existing plugins that rely on it. However, a new and different function is available that should be used in all new code.
Deprecated means that anyone using code that calls like_escape()
with WP_DEBUG
enabled will see an error message. If WP_DEBUG_LOG
is also enabled, the error message will appear in the /wp-content/debug.log
file.
Let’s look at an example of core code where I removed like_escape()
and implemented the new function $wpdb->esc_like()
.
3.9 Old Style
$search_orderby_s = like_escape( esc_sql( $q['s'] ) );
$search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
What did this do? It was an old snippet from /wp-includes/query.php
that set up a search for post titles. The input $q['s']
was escaped using two functions before it was added to the post_title LIKE
expression. Now let’s see how I replaced that snippet in the next version.
4.0 New Style
$like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
$search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
Continue reading like_escape() is Deprecated in WordPress 4.0