apply_filters(‘the_content’, $content) vs do_shortcode($content)

Lets say I have a theme option or custom postmeta text area. Now I want to execute multiple shortcodes, general texts, images etc.

What will be best practice and why?

Option 1:

$content = //my text area data;
echo apply_filters('the_content', $content);

Option 2:

$content = //my text area data;
echo do_shortcode($content);

Kindly explain me which one will be best practice and why.


Let me describe the scenario in details. I develop themes for clients with their requirements. Sometimes, I need to add post meta on post/pages/custom post types, so that they can add shortcodes (slider, contact form etc) or just a simple text. It’s a text filed.

To make the shortcode working, I use option 1. Now, I heard from someone that this is a wrong way, and I should use do_shortcode. But they did not give me an explanation of why it’s wrong way. That’s why I am asking.

This whole process can be done in the default text editor of wp. But I need to create those options for template specific usage, that’s what my clients want.



There are sometimes these questions that nags you and hunts you down later in life again, and this is one such question.

This question had me thinking about an alternative solution to the problem. As I already stated, custom fields and meta boxes are there to store small pieces of meta data, and not to act as an extension to post content where you can execute shortcode and functions. Also, as I already stated, your method is incorrect and should not be used.

I did find interesting in your post that you used custom fields and meta boxes to inadvertently display custom content from user input. So I sat and thought of a possible way to make this work and use the custom field data and meta box data correctly.

This is my idea:


NOTE: This can be modified to suit any need

On a single post, a user wants/requires to show custom content dynamically after the post to suit his/her needs. This should be dynamic. The content should be a custom query, and the user needs to choose what to show whenever he/she wants and what he/she wants.


Shortcodes will not work here, as shortcodes can’t be executed in custom fields. Neither will do_shortcode work, as it is not dynamic and is hardcoded, something we don’t want. As in your question, we are going to make use of a custom field. Again I stress, do not use the custom field to execute a custom query or shortcodes.


We will use the custom field to only save our query arguments; that is all. So, we create a custom field called custom_query_arguments. In your post editor screen, you will now see your custom field, ready for use.

The next step will be to add our custom query arguments to our field. Let’s say, we need to show three posts from category Best Answerorted by title. So our query arguments should look like this (in string format):


This is what you should now enter in your custom field. Once entered, save the value of your custom field.

Next will be to construct the custom query in your single.php. We need to get the value from our custom field, which is actually our query arguments, and feed that to a new instance of WP_Query to retrieve the posts. We also need to check whether or not we actually have a value saved in that custom field, if the custom field is empty, show nothing.


You can try something like this in file single.php just after the single post.

$args = get_post_meta( get_queried_object_id(), 'custom_query_arguments', true );
// Check if the custom field has a value
if( ! empty( $args ) ) {

    $q = new WP_Query( $args );

    if( $q->have_posts() ) {
        while( $q->have_posts() ) {


If the user wants to remove the custom query, he/she can simply just delete the value of the custom field and leave the custom field blank. If he/she needs to show the same query, but from category 10 and a total of 5 posts, he/she can just replace the original value with the following:



It is important to use the correct synatx and format when entering information into these custom fields and meta boxes. Syntax errors or incorrect information will lead to undesired output or even fatal errors. It is important to let your clients know of such information.


I don’t understand what you are trying to accomplish, but from what I can tell you, those are two separate things.


apply_filters('the_content', $content); is used to apply the content filters to raw unfiltered post content, which usually comes from the use of $post->post_content. These filters include the famous filter wp_autop which adds p tags to the_content()

apply_filters('the_content', $content); is usually used in conjunction with get_posts where one works directly with the WP_Post objects without using setup_postdata( $post ) which makes the template tags like the_content() available for use.


do_shortcode is used to add a shortcode anywhere in template files outside the text editor in the page editor screen back end, basically filtering shortcodes through their hooks.

The correct use is as follows.

Example: Adding the gallery shortcode in a template file

echo do_shortcode( '' )


From your comments, I would not use a shortcode at all then.

If you are not going to add a shortcode via the text editor and going to add it directly (hardcode) via do_shortcode in a template file. I would rather then just add the function to the template.


If you have the following shortcode function,

function footag_func( $atts ) {
    return "foo = {$atts['foo']}";
add_shortcode( 'footag', 'footag_func' );

You can simply call the function directly in a template like:

echo footag_func();

It is much faster this way as the shortcode need not be parsed.


To be honest here, you are completely doing this wrong from your edit. That is why I could not understand your initial question

Sometimes, I need to add post meta on post/pages/custom post types, so that they can add shortcodes (slider, contact form, etc.) or just a simple text. It’s a text field.

To make the shortcode working, I use option 1…..

Custom fields are not text fields and are surely not meant to be used to execute shortcodes and for that matter sliders or contact forms. Custom fields should never be used to replace the text editor in posts and pages.

As I stated before, apply_filters('the_content', $content); is meant to be used to apply formatting to raw post content.

You have two choices here

  • Use do_shortcode directly in template files, which I would not recommend as using the function is faster as the shortcode need not be parsed.

  • Use the shortcode directly in the text editor for the particular page/post

I would seriously recommend that you take a fresh new look at your structures and what you want to achieve. Custom fields are not text editors and cannot execute shortcodes or sliders.

My recommendation would be to look maybe into custom widgets or a system which you can use with custom fields.

Leave a Comment