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 !

WordPress Find Categories By Name

WordPress Find Categories By Name

The below works for both categories, and other custom taxonomies (e.g genres) you may have.

Find category by an exact match

$cat = get_term_by( 'name', $cat_name, 'category' );

Find category by partial match anywhere

$categories = get_terms( 'category', array( 'name__like' => 'cat_name' ) );

Find category by partial match from the start of the string

$categories = get_terms( 'category', array( 'search' => 'cat_name' ) );
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 !

WordPress regex search and replace without plugin

WordPress regex search and replace without plugin

Plugins sometimes are unnecessarily bloated and heavy, causing lots of load on the server. Hence, inserting source codes manually is sometimes the better option.

Warning: make a backup of your data before you proceed

In the theme editor, go to your functions.php file and add the following code to the bottom of the file. The scenario below is that I had a bunch of <iframe> tags where I forgot to add the attribute 'allowfullscreen' and I would rather avoid using a filter for 'the_content' as it will cause unnecessarily load. Your use case will most likely be different, so feel free to adapt the code so that it works for you.

If you need to test your regex, you can do so using regex101

add_action('init','update_post_content');

function update_post_content()
{
    $my_posts = get_posts( array('post_type' => 'post') ); // Modify the post type - post, page etc

    foreach ( $my_posts as $my_post ):
        $post_content = $my_post->post_content;
        if( strstr($post_content, 'allowfullscreen') ) // if the allowfullscreen attribute exist already, then skip
            continue;
    
        $updated_post_content = preg_replace("/^(iframe).+?(?=><)/", "$0 allowfullscreen", $post_content); 
    	$my_post->post_content = $updated_post_content;

    	wp_update_post( $my_post );

    endforeach;
}

After you have successfully modified whatever you want to modify, remember to remove the code (unless you intentionally wish to keep it)

 

Enjoyed the content ? Share it with your friends !

WordPress Category List by Alphabet Code Without Plugin

WordPress Category List by Alphabet Code Without Plugin

In your functions.php, insert the code below. In the page or post or wherever you want, just use the shortcode [sorted_category_list] and the sorted category list will appear 🙂

add_shortcode('sorted_category_list', 'sorted_category_list_shortcode');

function sorted_category_list_shortcode() 
{
    $html = "";
    
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC',
        'include_last_update_time' => false,
    ) );
    

    if( empty($categories) ) {
        $html .= "<p>Category list is empty</p>";
    } else {
    
        // Sort categories 
        $sorted_categories = [];
        foreach( $categories as $category ) 
        {
            $category_name = $category->name;
            if( count($category_name) > 0 ) {
                $first_char = $category_name[0];
                $first_char = strtoupper($first_char);
                if( ctype_alnum($first_char) ) {  // if $first_char is alphanumeric
                    $sorted_categories[$first_char][] = $category;
                } else {
                    $sorted_categories['Others'][] = $category;
                }
            }
        }			
        
        foreach( $sorted_categories as $initial=>$categories ) 
        {
            $html .= "<p style='font-weight:bold;'>{$initial}</p>";
            $html .= "<ul>";
            foreach($categories as $category) 
            {
                $category_name = $category->name;
                $category_id = $category->term_id;
                if( $category_id > 0 ) {
                    $category_link = get_category_link($category_id);
                    $html .= "<li><a href='{$category_link}'>{$category_name}</a></li>"; 
                } else {
                    $html .= "<li>{$category_name}</li>";
                }				
            }
            $html .= "</ul>";	
        }

    }
    return $html;
}

 

Enjoyed the content ? Share it with your friends !