36

Both answers so far are correct but a more thorough answer seems warranted. If you use this: $content = wpautop( $post->post_content ); you're applying the one function that adds paragraph tags to post content. wpautop() is one of many functions (including plugin functions at times) that hooks onto the_content, so if you do this: $content = ...


28

I believe this should work: $getPost = get_the_content(); $postwithbreaks = wpautop( $getPost, true/false ); echo $postwithbreaks; The second argument in wpautop can be up to you whether it's true of false, see the link below. It is described as follows: (boolean or integer) (optional) Preserve line breaks. When set to true, any line breaks remaining ...


24

Open to anybody who can simplify this but here's what I came up with that worked for me. First thing's first - get the gallery, using get_post_gallery(), as soon as the loop starts: <?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); $gallery = get_post_gallery(); $content = ...


21

You can use the WordPress function get_extended to fetch the different parts of a string (the part before and the part after the <!--more--> tag). get_extended returns an array with three keys, of which the keys main and extended are important: $arr['main'] contains the part before the more tag, and $arr['extended'] the part after the more tag. This ...


17

Use wp_strip_all_tags() to remove the content of script and style elements too: echo wp_strip_all_tags( get_the_content() );


17

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


16

If we want to remove all inline styles, then just simply need to add the following code in functions.php. add_filter('the_content', function( $content ){ //--Remove all inline styles-- $content = preg_replace('/ style=("|\')(.*?)("|\')/','',$content); return $content; }, 20);


13

To build upon @s_ha_dum's answer I think you want. $content = get_the_content(); echo wp_filter_nohtml_kses( $content ); //or strip_tags


13

To be honest, the function in_the_loop() is what you are looking for: add_filter( 'the_content', 'custom_content' ); function custom_content( $content ) { if ( in_the_loop() ) { // My custom content that I add to the_content() } return $content; } What in_the_loop does is to check if global for $wp_query (that is the main query ...


12

You can add a custom plugin, to WordPress and also the TinyMCE visual editor. The follow source is a example that simple works and add a string before and after all shortcode. Usage The shortcode will find via regex, relevant if you need it for different shortcodes and different mark on this. The script add custom content to the shortcode, here <b>FB-...


10

You don't really have a Loop. <?php get_header(); ?> <div class="content"> <div class="welcome_area"> <div class="welcome_area_title"><?php the_title('');?></div> <div class="welcome_area_text"><?php if (have_posts()) { while (have_posts()) { the_post(); the_content(); } } ?> What is happening ...


9

Wordpress automatically ads the <p> tags to the content. So it shows up while loading the content. This is with the filter wpautop. So we will remove this filter for the image post type only. You can manage this by adding the following code in functions.php file. // Add the filter to manage the p tags add_filter( 'the_content', '...


9

Found the solution: $content = apply_filters('the_content', $post->post_content);


8

The Core filters on the_content are: 131 add_filter( 'the_content', 'wptexturize' ); 132 add_filter( 'the_content', 'convert_smilies' ); 133 add_filter( 'the_content', 'convert_chars' ); 134 add_filter( 'the_content', 'wpautop' ); 135 add_filter( 'the_content', 'shortcode_unautop' ); 136 add_filter( 'the_content', '...


8

I guess you have: the_content(); wp_link_pages(); in your theme file. So you can instead try the following (PHP 5.4+): /** * Append the wp_link_pages to the content. */ ! is_admin() && add_filter( 'the_content', function( $content ) { if( in_the_loop() ) { $args = [ 'echo' => false ]; // <-- Adjust the arguments to ...


8

New content pagination filter in WordPress 4.4 As of WordPress 4.4 we can use the content_pagination filter ( see ticket #9911 ) /** * Filter the "pages" derived from splitting the post content. * * "Pages" are determined by splitting the post content based on the presence * of `<!-- nextpage -->` tags. * * @since 4.4.0 * * @param array $...


8

You can use the_content with an high prioriety (lower number). add_filter( 'the_content', function( $content ) { return 'Hello World '.$content; }, 0); You can even use negative priority: add_filter( 'the_content', function( $content ) { return 'Hello World '.$content; }, -10); Note that this will apply everytime 'the_content' is used, no matter the ...


8

I was having the same issue. My the_content filter was being called multiple times and this was slowing down page load as my the_content filter was calling an external API. So in my case, the API was being queried multiple times for the same data. I tried using in_the_loop(), is_singular() and is_main_query() but sometimes, depending on the theme, those ...


8

When in the loop, this will produce excerpt from $post->post_content directly: <?php echo wp_trim_excerpt(); ?> Read more HERE. Alternative Solution: If you are not in the loop, then, you may use similar implementation as done in the wp_trim_excerpt function: $text = strip_shortcodes( $post->post_content ); $text = apply_filters( 'the_content', $...


7

Figured it out, hooking into content_save_pre: function remove_empty_lines( $content ){ // replace empty lines $content = preg_replace("/&nbsp;/", "", $content); return $content; } add_action('content_save_pre', 'remove_empty_lines');


7

This is merely an addition to @Otto's answer. Just to make it a little bit better understandable. Basically what @Otto is saying, you have to reverse the logic, that means: if you can reliably determine the main query, then you can add - and remove - your hooking into the the_content filter. For example the main query can reliably be recognized at the ...


6

The WordPress function, at least the one I tend to use, would be wp_filter_nohtml_kses. That should remove all of the HTML, but you need to be careful not to put some HTML back by running the_content filters on your HTML-less string. I am not sure why strip_tags didn't work, but I suspect that you doing something else in other code posted or not posted and ...


6

If this post type is called "image", you can create a single template to handle the display of just the image post type. Just copy your 'single.php' file and rename the copy 'single-image.php'. Now you can control just the image posts. To strip out tags, I like to use the strip_tags() function. If you print the content of the post with the_content() it ...


6

If you are trying to do it for the current page you are on you can just use this: global $post; $content = $post->post_content;` This will get the content for the current post instead of having to set the ID specifically.


6

I wrote an article on this here, but here's a summary of the points: the_content can only be used 'inside the loop' 'inside the loop' can only be correctly 'simulated' by calling setup_postdata() and global $post. You then need to clear up after yourself by calling wp_reset_postdata() The below code provides a function to retrieve post content from the ...


6

Second method to get post content with p tag is echo wpautop( $post->post_content );


6

EDIT - Now that 4.4 is out, you should use the content_pagination filter. See birgire's answer below. You can add formatting to raw post content by applying the content filters directly to $post->post_content: echo apply_filters( 'the_content', $post->post_content ); This will bypass pagination by not using the get_the_content function, which is ...


6

post_content is a property of the WP_Post object. WP_Post is an object representing the post data from the database. So post_content contains the raw content as stored in the database. the_content() is a template tag that displays the content of the current post. The 'current post' is whatever the global $post variable is set to at the time the function is ...


5

Yes, it is possible. You can use the_content hook and assign your own filter to this hook. Then you should (that is the part when things get a little bit messy) parse and replace content of that post. So for example you can do it like so: function my_the_content_filter($content) { $content = str_replace('<h2 ', '<img src=""...><h2 ', $...


5

Add a filter to the_content and put your code in there so you don't need your custom content function: function wpa_content_filter( $content ) { // run your code on $content and return $content; } add_filter( 'the_content', 'wpa_content_filter' ); You may need to adjust priority to run your filter before or after others: // high priority, run ...


Only top voted, non community-wiki answers of a minimum length are eligible