1

I'm trying to count the total number of paragraphs within each blog post but it is not getting executed and hence returning nothing.

How do you want to use this code?

Currently in my function.php I have the following code that I use to show up ads inside my post:

add_filter( 'the_content', '_some_func' );

function _some_func( $content ) {

    $ad_code = "the ad code";

    if ( is_singular( 'post' ) ) {
        return prefix_insert_after_paragraph( $ad_code, 12, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {

        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }

        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }

    return implode( '', $paragraphs );
}

Now I want to take it to next level by servicing code based on now many paragraphs are there, like this:

add_filter( 'the_content', '_some_func' );

function _some_func( $content ) {

    if( __check_paragraph_count_blog() > 15 )
        $ad_code = "the ad code";

    if ( is_singular( 'post' ) ) {
        return prefix_insert_after_paragraph( $ad_code, 12, $content );
    }

    return $content;
}

So, I tried to write the following __check_paragraph_count_blog() function to get the total paragraph count so that I can use it in the above call.

function __check_paragraph_count_blog() {
    global $post;
    if ( is_singular( 'post' ) ) {
        $count = substr_count( $post->post_content, '</p>' );
        return $count;
    } else {
        return 0;
    }
}

There are 2 problem with my __check_paragraph_count_blog() function:

  • It is always returning 0
  • It considers the </p> tags inside <code> and <pre> tags too.

So, if anyone can help me fix my __check_paragraph_count_blog() function so that it work properly it will be a really big help.

  • 1
    if it's not being executed you'll need to show us how it is being called before anyone can try to help figure out why it's not being executed. – C C Aug 27 '16 at 13:20
  • @CC I've updated the code. – iSaumya Aug 27 '16 at 13:22
  • You're still no telling which action hook you are using. – cjbj Aug 27 '16 at 15:42
  • @cjbj I'm not using any action hook. I'm just trying to write a simple function which will return me the number of paragraphs. Do I need to use any action hook for that? – iSaumya Aug 27 '16 at 16:26
  • If you just dump a function in functions.php it will be executed immediately when the theme is inititiated. This is long before WP determines which post to show. So there is no post, there are no paragraphs and the count is zero. – cjbj Aug 27 '16 at 16:42
4

I'm pretty sure that is_singular() is going to return false when you are inside the WP loop, since there are more than one posts being looped through. Try is_single() or just look at the post object and examine the post_type attribute.

add_filter( 'the_content', '_some_func', 15 );

function _some_func( $content ) {

    if( __check_paragraph_count_blog( $content ) > 15 )
        $ad_code = "the ad code";
    else
        $ad_code = '';

    return prefix_insert_after_paragraph( $ad_code, 12, $content );

}


function __check_paragraph_count_blog( $content ) {
    global $post;
    if ( $post->post_type == 'post' ) {
        $count = substr_count( $content, '</p>' );
        return $count;
    } else {
        return 0;
    }
}
  • If you don't mind, can you please write a small snippet about how do you want to me code the __check_paragraph_count_blog() function ? – iSaumya Aug 27 '16 at 18:17
  • edited to show code modification. – C C Aug 27 '16 at 18:36
  • Hey there thanks for your code. Do, I need to hook it to wp action or just having this function within my functions.php will do the job? – iSaumya Aug 27 '16 at 18:49
  • I believe you are calling that function from another function which is already being called by a hook to the_content. You should probably give it a try then come back if still not working right. – C C Aug 27 '16 at 19:06
  • Hi there, I did tried it and it didn't worked ;'( – iSaumya Aug 27 '16 at 19:08
1

You first need to define what is a paragraph, is it a paragraph as being seen in the editor, or the one being seen on the html page. The distinction is critical as there is a difference of how wordpress stores the content in the DB and how it is "sent" to the html (and obviously a difference in taking into account shortcodes, oembeds and other manipulations.

In the DB itself, wordpress do not store the content as true HTML. Paragraphs are being separated by a new line character and not by a P tag. A filter running wpautop is responsible to do the conversion from a new line to the P element. If you want to be sure that you are working with the HTML version of the content you should use a priority of 11 or higher for your hook.

The other thing that is problematic with your code is that filters/actions should not be added before the wp_loaded hook, there are some exceptions for specific cases but even then you should not add filters in some random places in your code.

Last and not least, if you want to properly parse html, you should use an html parser and not string comparison and neither regexp. You might find it easier to actually use JS on client side then trying to handle badly formatted HTML on server side.

  • Hi here by a paragraph I meant those that are seen in the html page. I've also tried this approach to count but again didn't worked. $pattern = "/<p>.*?<\/p>/gm"; $paragraph_count = preg_match_all($pattern,$post->post_content); – iSaumya Aug 27 '16 at 19:30
0

If you just dump a magic function in functions.php it will be evaluated immediately when the theme is inititiated (someone better in PHP and OOP should be able to explain how this happens, but I assume you are defining a class).

This evalution is long before WP determines which post to show. So there is no post, there are no paragraphs and the count is zero.

How to fix this depends on what you want to do. Currently you are just writing to the browser console, so any hook after the post has been loaded would be fine. Like this:

add_action ('wp', 'wpse237301_check_paragraph_count_blog');
  • give me 2 min. I'm updating my question with more clarification. – iSaumya Aug 27 '16 at 17:35
  • Hi, I've updated the question. – iSaumya Aug 27 '16 at 17:47

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.