WordPress Tip: the_content() vs get_the_content()

Have you find yourself annoyed with formatting of the content? Maybe the added p-tags that WordPress puts into the content that you didn’t.

Then maybe what you are looking for is get_the_content( );

Get even more tips on setting your WordPress theme up perfectly

What’s the Difference?

I’m pretty sure that we all know exactly what the the_content( ); does. Right?

However get_the_content( ); in a WordPress template does one very important thing. It does not pass the content through the_content. Which means that it does not auto-embed videos, or expand shortcodes, among other things.

Just use get_the_content(); and you will remove those tags.

/ Jason Resnick

Jason is a WordPress developer helping small businesses, design and marketing agencies achieve their goals by specializing in Ecommerce and increasing conversions. Learn more about him here.

More Posts | Twitter | Work ON Your Business Challenge | Coaching

18 responses to “WordPress Tip: the_content() vs get_the_content()

  1. Great tip! I hadn’t thought to use get_the_content() to strip the WP-generated Ps.

    The ‘get_’ prefix is really important to be aware of when you write your own functions for WordPress and applies to a bunch of WP functions. The difference is typically return vs. display, ie:

    – bloginfo(‘url’) displays the path outright in your html, while
    – get_bloginfo(‘url’) returns the path, so you can append to/modify it

  2. Personally I find it massively counter-intuitive that these 2 functions return different values.

    get_the_content should return the same content, but as a variable rather than echoing to screen. I find it pretty whack that it’s a completely different function.

  3. It’s complete madness that these two offer different functionality.

    Another important difference, get_the_content does not auto-embed YouTube videos.

    Seriously infuriating trying to strip tags and cleanup the code output from the_content if get_the_contents is different!

    1. I do agree – it is frustrating. For things like videos, I’ve found that in the backend, I try and have the metabox ask for the url, so I know exactly what I’m getting and can display on the front-end.

  4. I’m building a custom template and using the_content() does *NOT* wrap my text with paragraph tags. I know this is supposed to work, and in fact it does work with other templates. However, I have not implemented anything that would affect the_content(). Has anyone ever seen the_content() not wrap text in paragraph tags before?

    WP version is 3.9.1.

    1. Honestly, I haven’t seen that before. I would try and disable plugins to see if the content is being filtered. Did you create the theme yourself or is it a premium? If the latter, then maybe they are doing something specific to that template with the content. I would start there.

      1. Thanks for replying Jason. I fixed it by realizing I was looking at the code for File A, while viewing File B in the browser. It always makes sense to make sure everything is “plugged in” it seems.

      1. It does for maximum control, although for that you can also use a custom filter 🙂

        More filters on the_content:

        remove_filter( 'the_content', 'wptexturize' );
        remove_filter( 'the_content', 'convert_smilies' );
        remove_filter( 'the_content', 'convert_chars' );
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_content', 'shortcode_unautop' );
        remove_filter( 'the_content', 'prepend_attachment' );
        remove_filter( 'the_content', 'capital_P_dangit' );

        Handy for figuring out what filters apply to the_content, including ones that are set by plugins.

        global $wp_filter;

        You can use this for other filters as well.

Leave a Comment

Your email address will not be published. Required fields are marked *