1

I am working on a Wordpress import tool, where I use a custom city table in my database with longitude, latitude which combine with some other text and then import by wp_insert_post(). This works great but after 3200 - 3600 the script stops and I get a 500 internal server error.

I have tried multiple solutions to fix this problem:

I increased the following php.ini settings:

  • max_input_time
  • max_execution_time
  • memory_limit
  • mysql.connect_timeout
  • max_input_time
  • post_max_size

Increased the WP_MEMORY_LIMIT is wp_config.php:

define('WP_MEMORY_LIMIT', '128M');

And increased my time limit in wp_config.php too.

set_time_limit(240);

Tried it also with my htaccess file:

php_value max_execution_time 240

Debugged my bugs but I still got the 500 error.

I contacted my host and they told me to set the set_time_limit(); which I have set in function.php, in the file of my importer and wp_config.php, but no success.

I viewed my php settings by phpinfo(); and there I have a max_execution_time of 120. I have asked my host to increase it and still the same problem.

This is my import script:

// Get results
        global $wpdb;
        $results = $wpdb->get_results( "SELECT * FROM stn_locations" );

    if( $results ) {

        // Submit button clicked
        if( isset( $_POST['submit'] ) ) {

            foreach( $results as $result ) {

                // Set variables
                $location_name      = $result->stnl_name;
                $location_longitude     = $result->stnl_longitude;
                $location_latitude  = $result->stnl_latitude;
                $location_country       = $result->stnl_country;
                $location_term      = $_POST['term'];

                // Content
                $location_content       = $_POST['stn_filter_import_content'];
                $location_content       = str_replace( '%%location%%', $location_name, $location_content );
                $location_content       = str_replace( '%%category%%', $location_term, $location_content );

                // Build post_title
                $post_title = $location_term . ' ' . $location_name;

                if( $location_country == $_POST['country'] ) {

                    // Import data
                    $import_data = array(
                        'post_title'    => $post_title,
                        'post_content'  => $location_content,
                        'post_status'   => 'publish',
                        'post_type' => 'filter',
                    ); 

                    // Insert post
                    $post_id = wp_insert_post( $import_data );

                    if( ! is_wp_error( $post_id ) ) {

                        // Set Term
                        wp_set_object_terms( $post_id, $location_term, 'service' );

                        // Set meta fields
                        update_post_meta( $post_id, 'longitude', $location_longitude );
                        update_post_meta( $post_id, 'latitude', $location_latitude );

                        $success = true;

                    } else {
                        $error = true;  
                    }


                }

            }

            }
}

Does anyone have a solution, tip, setting or other function for me? Let me know! Thank you.

3

My advice is to not do long running bulk-operations over a web-connection in the first place. PHP works just fine from the command line.

Write your one-time script to do whatever it is that you need it to do. If you need access to WordPress functions in that process, then include the wp-load.php file at the top of the script.

Then, go to a shell prompt on your server, and do "php yourscript.php" to run it. Command line PHP doesn't have run-time limitations on it and it will take as long as it takes.

  • Thanks for you answer! I have never worked with a shell prompt so this is new for me, but I can learn it. The Imports amounted to 30.000 lines. Do you think this is not possible with web-connection? – Robbert Mar 25 '14 at 10:28
  • It's not that it's not possible, it's just not the easiest way. Web connections are typically time-limited for various reasons. Running programs directly via command line is not. – Otto Mar 25 '14 at 21:29

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.