Setup PHP WordPress Dev Envrionment for Windows

Setup PHP WordPress Dev Envrionment

Requirements :

    1. Have a local php dev environment setup (MAMP/XAMPP/..)

If you have not already setup your local dev environment, you can do so by following my tutorial here

Now that you have a local php dev environment, installing WordPress is very easy.

  1. Go to the wordpress download page here and download the latest wordpress.
  2. Extract the archive to the directory of your web server. The default for XAMPP is C:\xampp\htdocs
  3. Start up Apache and MySQL
  4. Open a browser and go to http://localhost/wordpress/ and you will be greeted a page as shown below
  5. Go to your phpMyAdmin and create a database named wordpress_dev. You need not create any tables. You can refer to the images below
    xampp phpmyadmin
    xampp phpmyadmin
    xampp phpmyadmin home
    xampp phpmyadmin home
    xampp phpmyadmin create tables
    xampp phpmyadmin create tables

    xampp phpmyadmin create database
    xampp phpmyadmin create database
  6. Go back to the browser and continue with the installation, filling up the necessary details. The default username and password for MySQL in XAMPP is root and nothing/blank(there is no password)
  7. Click Continue and setup some of your other WordPress settings
  8. Your done !
Enjoyed the content ? Share it with your friends !

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 !