Ecommerce Shopify WordPress Discussion

htmlspecialchars don't escape html character [closed]

Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 19 hours ago. This post was edited and submitted for review 19 hours ago. Improve this question In PHP I'm using htmlspecialchars() to present code snippets on my (Wordpress) site: $code = get_post_meta(get_the_id(),'code_snippet')[0]; $code_html = htmlspecialchars($code); echo $code_html; When the text snippet is fetched by get_post_meta(), which will get the code snippet as text, this doesn't work. It prints the code but whenever there is an < or > it interprets it as HTML and collapses everything in between as if it were a tag. However, if I copy the same text from the Custom Field in the dashboard and paste it into the code (so assign $code the pasted string) it does work. And I don't understand why. For context, this is on a Wordpress site. The code snippets are stored in a Custom Field in the post's data. I have also tried esc_html()which is the Wordpress equivalent, with the same effect. Edit: I've been asked to provide proof of what is going on, hope this will be enough to explain what is happening: Here are screenshots of the first case, which doesn't work. The code snippet is unformatted so it is a bit hard to read, but the highlighted for loop should say for (float i = -ceil(width); i <= ceil(width); i++) {. Notice the <. As you can see in the output this doesn't happen. The output has been made with the same code as above. Instead, it interprets the < and > as HTML tags and collapses the text. Now, if I don't get the text with the get_post_meta(), but instead paste the code (shortened here) directly as a string variable it does work. So, $code = "for (float i = -ceil(width); i <= ceil(width); i++) {"; $html_code = htmlspecialchars($code); echo $code; The screenshots are taken directly form the site.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.