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.

2 Answers 2



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 1 sorted 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( '[gallery]' )


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.

  • 1
    Pieter thanks for the explanation. I already know that. But I was asking, which option will be more accurate if the target is only to output shortcodes from post meta/theme options textarea. I am using option1 to get the filtered content, and it became a habit of me. And using option 1 even to just print a shortcode from a textfiled. So was asking. Jan 3, 2015 at 12:06
  • I saw the edit. I got your point. But my scenario is different. It's like, there is a text filed/area, and several shortcodes. Now user can put any of the shortcode/s in that area. So I can not directly use a function. I need to keep that part dynamic. Jan 3, 2015 at 13:29
  • Where is that text field, is it the same as in the back end page editor screen Jan 3, 2015 at 13:34
  • It can be a post meta. Or it can be a theme option. I use the_content filter for both. Jan 3, 2015 at 13:36
  • Sorry, but then none of this makes sense. Why would you want to use shortcodes in custom fields. Why would you even use shortcodes then at all. Jan 3, 2015 at 13:41

apply_filter('the_content') executes all of the filters attached to Wordpress' content output such as do_shortcode, wp_autop and more.

apply_filters is a perfectly fine method of doing what you want, however, if you only want a shortcode replacement and none of the other filters, then use Option 2 from your question.

  • This is really all the info you need to understand about this in 2 sentences. Jan 4 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.