Products
GG网络技术分享 2025-03-18 16:12 6
I\'m using the standart \"esc_html\" in Wordpress for insert data (textarea) to the db.
All works nice but how can I reverse the text?
For example:
I insert the text - You ready?! Let\'s go!
And in the db it\'s as I expected save it as - You ready?! Let's go!
Later, when I tried to display this text in my tooltip it\'s also display the text as You ready?! Let's go!
Please, I would like to know if there is any reverse for this function since I tried to use htmlspecialchars_decode() and realized it\'s not the same function.
Thanks!
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我在Wordpress中使用标准“esc_html”将数据(textarea)插入数据库。</ p >
一切正常但是如何反转文本?</ p>
例如:</ p>
我插入文本 - < 强> 你准备好了吗?! 我们走了!</ em> </ strong> </ p>
在数据库中它正如我预期的那样保存为 - 你准备好了吗?! 让我们走吧!</ code> </ p>
后来,当我试图在我的工具提示中显示这个文本时,它还显示文本为你准备好了吗?! 让我们走吧!</ code> </ p>
请问,我想知道这个函数是否有任何反转,因为我试图使用htmlspecialchars_decode()并意识到它不是 相同的功能。</ p>
谢谢!</ p>
</ div>
The answer is: generally speaking, you don\'t escape data when saving it to the database. You sanitize it. (And so you don\'t really need to worry about \\\"reverting\\\" the content from esc_* functions).
You sanitize the input data when you\'re about to insert it into the database to prevent SQL injection attacks. If you\'re using the insert() method from the $wpdb
object or the wp_insert_post() function to create a new post/page, then the sanizitation is done for you automatically.
If you\'re writing a custom query (eg. $wpdb->query( \\\"INSERT INTO table VALUES(\'a string\', \'another string\', \'2018-09-21 10:35:52\')\\\" );
) then you need to use the prepare() method to sanitize the query before running it (eg. $wpdb->query( $wpdb->prepare( \\\"INSERT INTO table VALUES(%s, %s, %s)\\\", array($string1, $string2, $date_string) ) );
).
When you\'re about to display the data on screen use any of the different esc_* functions to make sure nothing malicious is being printed on screen (like <script>alert(\'Hello!\');</script>
).
###
Have you tried wp_specialchars_decode() ?
https://developer.wordpress.org/reference/functions/wp_specialchars_decode/###
There is a PHP function for this!
$string = \\\"You ready?! Let's go!\\\";$decodedString = htmlspecialchars_decode($string, ENT_QUOTES);
Decoded String: \'You ready?! Let\'s go!\'
Demand feedback