πŸ”₯ HOT: Reference/classes/wpdb/esc like - Uncensored 2025

wpdb::esc_like( string $text ): string

First half of escaping for LIKE special characters % and _ before preparing for SQL.

Description

Use this only before wpdb::prepare() or esc_sql() . Reversing the order is very bad for security.

Example Prepared Statement:

$wild = '%';
$find = 'only 43% of planets';
$like = $wild . $wpdb->esc_like( $find ) . $wild;
$sql  = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE %s", $like );

Example Escape Chain:

$sql  = esc_sql( $wpdb->esc_like( $input ) );

Parameters

$textstringrequired
The raw text to be escaped. The input typed by the user should have no extra or deleted slashes.

Return

string Text in the form of a LIKE phrase. The output is not SQL safe.
Call wpdb::prepare() or wpdb::_real_escape() next.

Source

public function esc_like( $text ) {
	return addcslashes( $text, '_%\\' );
}

Changelog

VersionDescription
4.0.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    /**
    * You can clear custom transients from the WordPress options table.
    */


    <?php
    public function wpdoc_revslider_clear_all_transients() {
    global $wpdb;

    $table = $wpdb->options;
    $transient = $wpdb->esc_like( '_transient_revslider_slider_' ) . '%';
    $timeout = $wpdb->esc_like( '_transient_timeout_revslider_slider_' ) . '%';

    $wpdb->query( $wpdb->prepare(
    "DELETE FROM $table WHERE option_name LIKE %s",
    $transient
    ) );

    $wpdb->query( $wpdb->prepare(
    "DELETE FROM $table WHERE option_name LIKE %s",
    $timeout
    ) );
    }

  2. Skip to note 4 content

    <?php function wpdoc_revslider_clear_all_transients() { global $wpdb; $table = $wpdb->options; $transient = $wpdb->esc_like( '_transient_revslider_slider_' ) . '%'; $timeout = $wpdb->esc_like( '_transient_timeout_revslider_slider_' ) . '%'; $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE option_name LIKE %s", $transient ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE option_name LIKE %s", $timeout ) ); }

You must log in before being able to contribute a note or feedback.