函数文档

like_escape()

💡 云策文档标注

概述

like_escape() 是一个已弃用的 WordPress 函数,用于在数据库搜索前转义字符串。它已被 wpdb::esc_like() 替代,且文档不完善、功能未按描述工作。

关键要点

  • 函数已弃用:自 WordPress 4.0.0 起,推荐使用 wpdb::esc_like() 替代。
  • 功能描述:转义字符串中的通配符(% 和 _),使其安全用于 LIKE 查询。
  • 参数:接受一个必需字符串参数 $text,返回转义后的字符串。
  • 弃用机制:通过 _deprecated_function() 标记为弃用,并在使用时发出通知。

代码示例

function like_escape($text) {
    _deprecated_function( __FUNCTION__, '4.0.0', 'wpdb::esc_like()' );
    return str_replace( array( "%", "_" ), array( "\%", "\_" ), $text );
}

注意事项

  • 此函数文档不完善,且实际行为可能与描述不符,开发者应避免使用。
  • 相关用途:在 WP_User_Search::prepare_query() 等弃用类中仍有使用,但建议更新代码。

📄 原文内容

Formerly used to escape strings before searching the DB. It was poorly documented and never worked as described.

Description

See also

Parameters

$textstringrequired
The text to be escaped.

Return

string text, safe for inclusion in LIKE query.

Source

function like_escape($text) {
	_deprecated_function( __FUNCTION__, '4.0.0', 'wpdb::esc_like()' );
	return str_replace( array( "%", "_" ), array( "\%", "\_" ), $text );
}

Changelog

Version Description
4.0.0 Deprecated. Use wpdb::esc_like()
2.5.0 Introduced.