18

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.

EDIT

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.

16

QUESTION AND ANSWER REVISITED

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

What I did found interesting in your post is 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:

THE SCENARIO:

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 needs. This should be dynamic. The content should be a custom query, and the user needs to choose what to show whenever he wants and what he wants

THE POSSIBLE SOLUTION:

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 fields. Again I stress, do not use the custom field to execute a custom query or shortcodes

THE PLAN:

We will use the custom field to only save our query arguments, that is all. So, what we do is, 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. Lets say, we need to show 3 posts from category 1 sorted by title. So our query arguments should look like this: (In string format)

'posts_per_page=3&cat=1&orderby=title'

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. What is needed here, 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

THE CODE:

You can try something like this in 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() ) {
            $q->the_post();

            the_title();

        }
        wp_reset_postdata();
    }

} 

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

'posts_per_page=5&cat=10&orderby=title'

FEW NOTES:

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

ORIGINAL ANSWER

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

OPTION 1

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 usally used in conjuction 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

OPTION 2

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]' )

EDIT 1

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

Example:

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

EDIT 2

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 filed.

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. – тнє Sufi Jan 3 '15 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. – тнє Sufi Jan 3 '15 at 13:29
  • Where is that text field, is it the same as in the back end page editor screen – Pieter Goosen Jan 3 '15 at 13:34
  • It can be a post meta. Or it can be a theme option. I use the_content filter for both. – тнє Sufi Jan 3 '15 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. – Pieter Goosen Jan 3 '15 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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