WordPress.org

Ready to get started?Download WordPress

Forums

Plugin modifies the_content but stalls out if the_content is too long (7 posts)

  1. The_Tim
    Member
    Posted 5 years ago #

    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?

    Help!

  2. The_Tim
    Member
    Posted 5 years ago #

    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.

  3. The_Tim
    Member
    Posted 5 years ago #

    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.

  4. The_Tim
    Member
    Posted 5 years ago #

    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' => "http://linkhere.com/"

    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?

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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:

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

    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.

  6. The_Tim
    Member
    Posted 5 years ago #

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

    So the regular expression I'm using in the preg_replace is this:
    '/'.$city.'(?!((?!<\/?blockquote>).)*<\/blockquote>)(?![^<]*(<\/a>|>))/s'

    But if I take out this part:
    (?!((?!<\/?blockquote>).)*<\/blockquote>)

    And just leave it as this:
    '/'.$city.'(?![^<]*(<\/a>|>))/s'

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

  7. The_Tim
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic