Decode HTML entities in WordPress PHP

In this article, we are going to tell you, how you can decode HTML entities in your WordPress post using PHP. For example, if you write © in your blog post, it will be displayed as ©.

To do that, you need to create a simple WordPress plugin. Go to your “wp-content” folder, then to the “plugins” folder. Inside this folder, create a new folder named “decode-html-entities”. Inside this newly created folder, create a file named “index.php”. Inside this file, you only need to write the following code:

<?php

/**
 * Plugin Name: Decode HTML entities
 */

function decode_html_entities_from_content($content)
{
	return html_entity_decode($content);
}

function decode_html_entities()
{
	// to filter post by ID
	$post = get_post();
	if ($post->ID == 9)
	{
		add_filter("the_content", "decode_html_entities_from_content");
	}

	// to filter posts by category
	$categories = get_the_category();
	if (!empty($categories))
	{
		foreach ($categories as $category)
		{
			if (strtolower(esc_html($category->name)) == "php")
			{
				add_filter("the_content", "decode_html_entities_from_content");
			}
		}
	}
}

// this action hook executes just before WordPress determines which template page to load
add_action("template_redirect", "decode_html_entities");

After that, you need to go to your admin dashboard and activate the plugin. You will see the plugin named “Decode HTML entities” on your “plugins” page. If you want to know more about the “template_redirect” hook, you can learn it from its official documentation.

That’s how you can decode HTML entities from your WordPress posts in PHP. Check out our more free tutorials on WordPress.

[wpdm_package id=’1882′]