WordPress Code To Improve Bulk Insert Or Update Post Speed

WordPress Code To Improve Bulk Insert Or Update Post Speed

When you hit tens of thousands of posts in WordPress, the speed of wp_insert_post or wp_update_post gets significantly slower. To improve the speed, you simply add the following code and you will see that the speed improves significantly.

Add these two lines of code before your insert or update loop

wp_defer_term_counting(true); 
wp_defer_comment_counting(true);

And add these two lines after your insert or update loop

wp_defer_term_counting(false);
wp_defer_comment_counting(false);

By adding these few lines of code, you are temporarily disabling the term counts until all posts have been inserted or updated to speed up the process

Enjoyed the content ? Share it with your friends !

Virtualmin How To Change PHP Execution Time Limit

Virtualmin How To Change PHP Execution Time Limit

In order to change the execution time limit of your scripts, you will need to change 2 options in Virtualmin. First, which you should probably already know is the PHP max_execution_time variable. Secondly, you will need to change the Maximum PHP Script Run Time option.

Changing the PHP maximum_execution_time variable
  1. Assuming you have already entered Virtualmin and selected the domain you want to manage, click on the Services category.
  2. It will then open up to more options. Click either PHP 5 Configuration or PHP 7.0 Configuration according to what your domain is using (by default domains use PHP 7.0)1. change php 5 or 7 timeout limit
  3. Finally, change the timeout you want accordingly as shown below. Use the value 0 for unlimited or infinite execution time

Changing the Maximum PHP Script Run Time Option
  1. Click on the Server Configuration category
  2. Click the sub-category Website Options
  3. You should see the Maximum PHP Script Run Time option. Change it to whatever you wish. Use the value 0 for unlimited or infinite execution time2. change website options timeout limit

If you require any assistance, please feel free to ask in the comments below.

Enjoyed the content ? Share it with your friends !

PHP PhantomJS Change User Agent String

PHP PhantomJS Change User Agent String

Noticed that google isn’t really useful in this case (there’s no copy and paste answer). Google for me only shows a bunch of people having issues and no easy solution when in fact the solution is quite simple

First things first, you have to check to see if your PHP-PhantomJs library version is 3.x or 4.x

If it is 3.x, to change the user-agent string, you just have to do this

$request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

If it is 4.x, to change the user-agent string, you just have to do this

$request->addHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

Of course, feel free to change the new value of your user-agent to whatever you want it to be.

To verify that it is working, if your testing locally, then you can use tools such as Fiddler to check the headers. Alternatively, you can dump out the $request and $response variables, like so,

var_dump($request); // $request is the return value of either createRequest or createCaptureRequest function
var_dump($response); // $response is the return value of createResponse function

If you have any questions feel free to let me know in the comments below.

 

 

Enjoyed the content ? Share it with your friends !

PHP WordPress Remove Paragraph and Line Break Tags

PHP WordPress Remove Paragraph and Line Break Tags

I find myself needing to remove the paragraph and line break tags that are automatically added into the post or category description quite often. So here is how it can be done

For Categories or Custom Taxonomies

add_filter( 'term_description', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'term_description', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'term_description', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('term_description', 'wpautop'); /*Remove line break filter completely*/

For Posts

add_filter( 'the_content', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'the_content', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'the_content', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('the_content', 'wpautop'); /*Remove line break filter completely*/

 

Enjoyed the content ? Share it with your friends !

PHP Check If A Resource Pointed By Url Is Valid

PHP Check If A Resource Pointed By Url Is Valid

Sometimes we might have to check if a particular video or image exists and works, and to do so, some people might choose to download the entire resource file using something like file_get_contents which might not be the most effective and efficient way of doing so.

A better way is to use curl and simply check the http status code returned.

/*
* Check if a resource is valid (or exists)
* Works with pretty much anything - jpg, mp4 ...
*
* @url : the url must be a direct link (or hotlink) to the resource.
*/
function is_resource_valid($resource_url)
{
    $resource_exists = false;
     
    $ch = curl_init($resource_url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
     
    if($status_code == '200'){
        $resource_exists = true;
    }		
    
    return $resource_exists;
}

 

Enjoyed the content ? Share it with your friends !