Category: Wordpress

How to get count of posts in a particular category?

You can get get count of posts in a particular category by using below code. Before use please replace “CUSTOM_POST_TYPE” and “CUSTOM_TAXONOMY” with your post type and taxonomy.

$the_query = new WP_Query( array(
    'post_type' => 'CUSTOM_POST_TYPE',
    'tax_query' => array(
        array(
            'taxonomy' => 'CUSTOM_TAXONOMY',
            'field' => 'id',
            'terms' => TERM_ID
        )
    )
) );
$count = $the_query->found_posts;

Have any doubt please comment here.

Redirect non admin users to home page and disallow back end access in WordPress

For redirect non admin users to home page and disallow back end access in WordPress, you need to simply copy and paste the following code in your theme’s functions.php file.

function redirect_non_admin_user(){
    if ( !defined( 'DOING_AJAX' ) && !current_user_can('administrator') ){
        wp_redirect( site_url() );  exit;
    } 
}
add_action( 'admin_init', 'redirect_non_admin_user' );

Have any doubt, then comment here!

Disable WooCommerce Payment methods based on cart item quantity

To disable WooCommerce Payment methods based on cart item quantity, you need to simply copy and paste the following code in your theme’s functions.php file.
Here I’m unsetting the COD when cart item quantity limit reached 10.

add_filter('woocommerce_available_payment_gateways', 'disable_payment_gateway_based_on_quantity', 10, 1);
function disable_payment_gateway_based_on_quantity( $available_gateways ) 
{
    $qty_limit = 10;
    $limit_reached = false;
    foreach(WC()->cart->get_cart() as $cart_item){
        if($cart_item['quantity'] > $qty_limit ){
            $limit_reached = true;
            break;
        }
    }
    if($limit_reached){
        unset($available_gateways['cod']);
    }
    return $available_gateways;
}

You can use the same code for disable WooCommerce Payment methods based on cart item quantity. Change quantity and payment method slug for your needs.

Have any doubt, then comment here!

Create a folder if it doesn’t already exist in PHP

You can check whether the folder is exits or not by using file_exists(). You can also create folder by using mkdir(). See below example code to create folder if it doesn’t already exits in PHP.

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}

Have any doubt, then comment here!

What is WordPress hooks? What are WordPress hooks?

WordPress Hooks are the functions that can be applied to an Action or a Filter in WordPress.
Two types of hooks exist in WordPress. That are

  1. Action
  2. Filter

Action hooks allow you to add additional code to the WordPress core or theme so that you can achieve some new functionality or customizations.
It can be handled by add_action() and do_action().

Filter hooks allow you to control how something happens or change something that’s already being output.
It can be handled by add_filter and apply_filter().

How to get using current WordPress version programmatically?

You can get the WordPress version using the following code:

$version=bloginfo('version');
echo $version;

Here $version displays the WordPress Version you use. You can get different information about the current site by passing parameters to bloginfo(). Some important parameter’s are listed below

  1. name
  2. description
  3. html_type
  4. url
  5. admin_email
  6. language
  7. stylesheet_directory
  8. template_directory

Have any doubt, then comment here!

Disable Payment Gateway for a Specific Country in WooCommerce

If you want to disable payment gateway for a specific country in WooCommerce then you have to decide the payment gateway and country code. Here I’m disabling the cod for India. You have to replace “cod” instead of your payment gateway. Have to replace “IN” instead of your decided country code. Place the following code in your functions.php file.

function disable_payment_gateway_for_a_country( $available_gateways ) {
  global $woocommerce;
  if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() == 'IN' ) 
  {
    unset( $available_gateways['cod'] );
  } 
  return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_for_a_country' );

Have any doubt, then comment here!