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 !

Things to take note when dealing with WordPress cron

Things to take note when dealing with WordPress cron

WordPress crons might be rather tricky for someone new to them so here are some stuff to keep in mind when dealing with them in development.

1. Cron does not include/require files that your admin dashboard (/wp-admin) does by default.

When your cron function is running, you will need to manually required files. For example, for the function wp_insert_post, you will require /wp-load.php as shown below

require_once( ABSPATH . "wp-includes/wp-load.php" );

2. Cron does not run under admin, so the security level is higher

I have not fully investigated this, but believe it is so. I was developing a plugin where the cron would create a post automatically using wp_insert_post and I came across a problem where my content would get filtered. Certain tags and characters like <iframe> might be filtered away. Specific to my case, I simply had to remove the WordPress filter and add them back again after the cron ended

3. Install the WP Crontrol plugin to make your cron development easier 

Trust me, you will need it. It helps with everything related to cron – scheduling, function callbacks, creating/editing crons, etc..

Enjoyed the content ? Share it with your friends !