Support » Plugins » Plugin modifies the_content but stalls out if the_content is too long

  • I’m working on a plugin for my site that modifies some of the post content using a function called “linkify_text” and the hook add_filter(‘the_content’,’linkify_text’);

    It runs on my site just fine except when the_content of a post or page is extremely long, in which case it either throws up a 500 Internal Server Error (if WP-Cache is enabled) or just serves a page with no code after <div class=”entry”>.

    I am going crazy trying to figure out why this is happening. Is there some sort of limit to how much can be processed by a plugin? Am I maybe running into a php server limitation? What is going on here?


Viewing 6 replies - 1 through 6 (of 6 total)
  • Oh, I should note that another symptom is that when attempting to load a post or page that’s too long, a core dump occurs, dumping a ~27MB file into my WordPress root.

    Still more details… Server logs show an entry of “Premature end of script headers” every time this happens. Looks like something is timing out? It doesn’t take long at all for it to spit out the blank page, so I can’t imagine I’m hitting any 30-second execution limits or anything.

    Okay, since nobody has responded still, here’s the specific code I’m implementing in my plugin. It’s pretty simple.

    There’s an array named $cities that has a bunch of stuff in the form of ‘city’ => “”

    After the array is this:

    foreach ($cities as $city => $linkedcity) {
    	$text=preg_replace('/'.$city.'(?!((?!<\/?blockquote>).)*<\/blockquote>)(?![^<]*(<\/a>|>))/s','<a href="'.$linkedcity.'" title="'.$city.' Real Estate Statistics on Redfin">'.$city.'</a>',$text);
        return $text;

    Then I just run add_filter(‘the_content’,’linkify’) where ‘linkify’ is the name of the function the array and the code above are located in.

    When I run this on a page that’s over 5.5KB (or so) it crashes as described above.

    Is there anyone that can help me with this?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    How many cities we talking about here? preg_replace is very intensive stuff with complex regexp’s.

    You might be better off using the array version of preg_replace instead. Like this:

    foreach ($cities as $city => $linkedcity) {
    $pattern[] = '/'.$city.'(?!((?!<\/?blockquote>).)*<\/blockquote>)(?![^<]*(<\/a>|>))/s';
    $replace[] = '<a href="'.$linkedcity.'" title="'.$city.' Real Estate Statistics on Redfin">'.$city.'</a>';

    This may be faster.

    Alternatively, perhaps there’s some particular city that’s mucking up the regular expression and causing the process to crash. This is possible since regular expressions in php are processed by the external PCRE library, which has had bugs before and which may be an older one, depending on the version of PHP and how it was compiled and such. So it might be worth figuring out exactly which city and the resulting regular expression is causing the issue. The fact that you’re dumping core indicates that this could be the cause.

    Oooh! I made a little progress in narrowing down the problem!

    So the regular expression I’m using in the preg_replace is this:

    But if I take out this part:

    And just leave it as this:

    The error goes away. Anybody have any idea why the blockquote part of my regex would cause php to crash like that?

    Thanks for the response, Otto. To answer your questions, it’s a list of 373 cities, and they’re all just plain A-Z text with no special characters or even digits. It bombs out on long pages whenever there’s any match, not on any specific one.

    I’ll give the preg_replace array a try. Thanks for the suggestion.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Plugin modifies the_content but stalls out if the_content is too long’ is closed to new replies.