Hack the maximum execution time in WordPress

Today I find a problem: time ends... Well, for a WordPress plugin only, the KC S2M+MC one.

I mean I got "Maximum execution time of 60 seconds exceeded" error, when I run a long operation (synchronizing members in Mailchimp list and the site can be very long, there was a list of 400 users to create on the site). The value in my php.ini for 'max_execution_time' was set to 60, but this was not enough.

To extend it we have several ways:

1. By wp-config.php - must add a row:

set_time_limit(300);

It must be above “/* That’s all, stop editing! Happy blogging. */” comment. Just in case.

But this was not good for me - I don't like the idea users of my plugin to touch site's config file. Just in case...

2. Addition in php.ini file - here it must be:

max_execution_time = 300 ;

But most probably users have no access to php.ini file...

3. In .htaccess - the row there is:

php_value max_execution_time 300

Still not good - not all users can find and work with .htaccess file. Also some providers don't allow 'php_value' there. Also they run my plugin, it must cares about everything.

Well, what...? Did you read "The Twelve Chairs" book from Ilf and Petrov? There a personage knows "four hundred comparatively honest ways of taking money away from the population". But no one works in the case it was. So he invents 401-st :-)

I am not invent much, but remember that php_ini values can be changed inside the script. So here is my solution:

4. Inside the script:

ini_set('max_execution_time', '300');

Be ready for long play :-)

This entry was posted in Blog, Hacks and tagged , , , , . Bookmark the permalink.

Leave a Reply