How do I insert a short tag after 350 words in post_content?

The following will allow you to limit content to any word count you like based upon whether the user is part of a certain user role or not. The function can be improved and or made more efficient but at least it provides you the basis for filtering your content without having to physically edit the content or run any complex regular expressions.

add_filter('the_content', 'limited_content', 11);

function limited_content($content) {

    $limit   = 350; //word limit for non-member/non-subscriber
    $words   = str_word_count($content);
    $user_id = get_current_user_id();

    //check if the user is part of the "member" role if so, return the full content
    if( user_can($user_id, 'member') ) {

        return $content;

    //otherwise for non members return 350 of the content
    } elseif ( $words >= $limit) {

        $content = explode(' ', $content, $limit);
        array_pop($content);
        $content = implode(" ", $content);
        return $content . '... <a href="#">sign up to read more</a>';

    //if there's less than 350 words, just return whatever exists
    } else {

        return $content '... <a href="#">access premium content now!</a>';;

    }

}

Modified your edit so you can see the logic properly,

add_filter('the_content', 'limited_content', 11);

function limited_content($content) {

    $cats    = array(2497, 2811);
    $limit   = 350; //word limit for non-member/non-subscriber
    $words   = str_word_count($content);
    $user_id = get_current_user_id();

    if ( in_category( $cats ) || post_is_in_descendant_category( $cats ) ) {

                if( user_can($user_id, 'member') ) {

                    //return full content to members, script will stop at this point        

                } elseif ( $words >= $limit) {

                    //limit content where content exceeds 350 words to non-members

                } else {

                    //if content contains less than 350 words, return all of it, even if non-member
                }

    } else { 

        return $content; //if we are not in/descendant of $cats the content returns normally regardless of user role.
    }

} 

I anticipated that you would say something like this,

Although I’d rather do a one time change/update of the database so as
to avoid continually running a function on the site…
– Brett Bumeter

You might think that what you’re doing is a one-time change, but in fact its not!

Wrapping your content after 350 words in shortcodes for each of your 700 posts is potentially disasterous and here’s why.

The shortcode itself is a function or rather it refers to a function, so when any piece of content that contains this shortcode (in the_content) is called upon, it will execute its logic.

You would need to wrap each of your 700 posts with a shortcode at the mark of 350 words, so that’s 700 shortcodes added to your database in the post_content – which although may be considered a small increase, is still an uneccesary increase in database size.

What happens if for some reason you decide you want to now restrict content after 200 words instead of 350?

You’re in trouble. Can’t be done, you need to physically move the shortcode position.

Or what about the scenario where you decide that you want to BOTH restrict content after 200 words and 350 words depending upon membership levels?

You’re in trouble. Can’t be done, you need to physically move the shortcode position and modify the function that controls the shortcode to allow for conditional restrictions.

What a nightmare right?

This is where filters come into play and that’s why they exist and importantly this is why they are the most efficient way of handling and manipulating data without ever having to modify the original contents of your posts.

This is how your data should be treated. Instead of polluting your post content with logic, keep it pure and pristine, let filters do the crunching and heavy lifting for you to achieve your requirements.

This makes your content very flexible as it can be served in multiple ways to multiple people without you smacking your head against the wall. Ultimately, filters make your membership site powerful, shortcodes make your membership site weak. Filters allow you to scale your ideas, shortcodes box you into a corner.

Shortcodes are useful for repetitious tasks such as inserting verbose content into your post when writing, like maybe a bullet point image, or a special link or alert box that you might regularly use in the middle of your content or subscription links or… you get the gist. But when it comes to manipulating and controlling the output of content, shortcodes are a bad choice.

Leave a Comment