30

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


16

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);


16

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


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


15

This function inserts your ad code after the specified paragraph. add_filter('the_content', 'wpse_ad_content'); function wpse_ad_content($content) { if (!is_single()) return $content; $paragraphAfter = 2; //Enter number of paragraphs to display ad after. $content = explode("</p>", $content); $new_content = ''; for ($i = 0; $i < ...


14

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


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

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


11

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


10

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);


9

the_content is also a filter, into which the content is passed as an argument. You simply prepend your content and then return like so. add_filter('the_content','prepend_this'); function prepend_this($content) { $content = "string to prepend" . $content; return $content }


9

In order to use "the_content()" it must be used within The Loop. See here: http://codex.wordpress.org/Function_Reference/the_content "Displays the contents of the current post. This tag must be within The_Loop."


9

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

I ran into this problem a year or so ago, and found a fix here. Open PHP.INI in a text editor of your choice (normally you can find php.ini in your php install dir) Change the recursion limit to 200x normal, that is, set: pcre.recursion_limit=20000000 Change the backtrack limit to 100x normal, that is, set: pcre.backtrack_limit=10000000 Stop and start the ...


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

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


7

First off: The ID of a post or page is always an integer. "about" is either your about page's title, slug or both. Including the following in your "homepage's" page template or in the sidebar combined with conditional tag(s) will display the about page's content: <?php // query for the about page $your_query = new WP_Query( 'pagename=about' ); ...


7

The following bit of code adds a class to the first paragraph output by the_content: function first_paragraph($content){ return preg_replace('/<p([^>]+)?>/', '<p$1 class="intro">', $content, 1); } add_filter('the_content', 'first_paragraph'); Add the above to your theme's functions.php file. Then in your CSS add something like: p.intro ...


7

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


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

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


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

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


6

The easiest way to do that is to hijack the gallery shortcode (no extra regex needed), store it somewhere and add it to the end. Prototype <?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Move Galleries To End Of Content */ add_action( 'after_setup_theme', array ( 'T5_Move_Galleries', 'init' ) ); class T5_Move_Galleries { public static $...


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