32

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


26

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


18

Here's the solution I used: function wrap_embed_with_div($html, $url, $attr) { return '<div class="video-container">' . $html . '</div>'; } add_filter('embed_oembed_html', 'wrap_embed_with_div', 10, 3);


18

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

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

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


14

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

This is a little tricky because sanitize_user allows spaces in usernames, meaning it difficult to avoid grabbing the whole phrase '@johndoe said that ... ' as opposed to just the actual username '@johndoe' and you have no separator at the end that would help. To avoid that I imposed a requirement that spaces in the username be replaced with '+'. function ...


12

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


12

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


11

What (exactly) happens When calling the_content() inside your template, you are able to call it without any parameters. This means, that the function already has the defaults of null for both arguments: The "more" link text and the boolean switch that allows you to strip teaser content before the "more" link text. The the_content() function calls ...


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


8

With Wordpress filters. Add this to your functions.php: function div_wrapper($content) { // match any iframes $pattern = '~<iframe.*</iframe>|<embed.*</embed>~'; preg_match_all($pattern, $content, $matches); foreach ($matches[0] as $match) { // wrap matched iframe with div $wrappedframe = '<div>' . $...


8

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


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

Found the solution: $content = apply_filters('the_content', $post->post_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

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


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


7

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


7

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


7

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', $...


6

Interesting question. This could be a useful part of a specific language file. It cannot be done in CSS, because CSS is (mostly) character agnostic. But using a filter and PHP it is possible and on topic: add_filter( 'the_content', 't5_chinese_spacing' ); function t5_chinese_spacing( $content ) { return preg_replace( '~([^\p{Han}]*)(\p{Han}+)([^...


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


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