Add new currency and symbol to woocommerce using filter

Here is a quick snippet to add new custom currency and symbol to wocommerce. Place the following snippet in functions.php within your theme folder. After adding this code your currency will be available in your WooCommerce settings page.

add_filter( 'woocommerce_currencies', 'add_my_custom_currency' );

function add_my_custom_currency( $currencies ) {
     $currencies['BOO'] = __( 'Your currency name', 'woocommerce' );
     return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_my_custom_currency_symbol', 10, 2);

function add_my_custom_currency_symbol( $currency_symbol, $currency ) {
     switch( $currency ) {
          case 'BOO': $currency_symbol = '$'; break;
     }
     return $currency_symbol;
}

Have any doubt, then comment here!

Redirect to home page after logout in WordPress

Add the following code in functions.php within your theme folder for redirect customers into home page after logout in WordPress

add_filter('logout_url', 'new_logout_url', 10, 2);
function new_logout_url($logouturl, $redir)
{
	$redir = get_option('siteurl');
	return $logouturl . '&redirect_to=' . urlencode($redir);
}

Have any doubt, then comment here!

Add Custom css in wordpress

You can use the following code for add custom CSS in WordPress. add your styles in commented part.

add_action(‘admin_head’, ‘my_study_style’);
function my_study_style()
{
//add your styles here
}

Have any doubt, then comment here!

Show All Categories as Links in WordPress

You can use the function get_the_category_list() for this process.

<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
	foreach($categories as $category) {
		$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
	}
echo trim($output, $separator);
}
?>

For more : http://codex.wordpress.org/Template_Tags/get_the_category

Get All Images From WordPress Media Gallery

Uploaded images are stored as posts with the type “attachment”. Use get_posts() and query for all attachments:

$args = array(
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_mime_type' =>'image',
                'post_status' => 'inherit',
                'post_parent' => null, // any parent
           );
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $post) {
         setup_postdata($post);
         the_title();
         the_attachment_link($post->ID, false);
         the_excerpt();
     }
}

Now All images are displayed.

Have any doubt, then comment here!

create an autocomplete textbox in wordpress

Just add a div under the input tag

HTML Code:

<input type="text" id="city" name="city" autocomplete="off"/> 
<div id="key"></div>

replace the div after the success on you ajax.

Ajax Code:

     var ajaxurl="<?php echo admin_url( 'admin-ajax.php' ); ?>"; 
     var data ={ action: "city_action",  city:cid    };
        $.post(ajaxurl, data, function (response){
                     $('#key').html(response);
           });

PHP Code:

       function city_action_callback() {     
       global $wpdb;
            $city=$_GET['city'];
            $result =   $mytables=$wpdb->get_results("select * from ".$wpdb->prefix . "mycity where city like '%".$city."'" );   
            $data = "";
            echo '<ul>'
            foreach($result as $dis)
            {
                         echo '<li>'.$dis->city.'</li>';
            }
            echo '</ul>'    
         die();
       }

Have any doubt, then comment here!

Get category ID by using category name in wordpress

If you want to get category id by using category name then use the following code

function get_category_id($cat_name){
	$term = get_term_by('name', $cat_name, 'category');
	return $term->term_id;
}

then Just call the function with your category name as a parameter. for example

$category_ID = get_category_id('Books');

Now you can get the category Id in variable $category_ID.

Have any doubt, then comment here!

Get all user meta data by user ID in wordpress

If you want to get all user meta data by using id then use get_user_meta() function.

For example user id is 25. Use the following code for get all user meta data.

<?php
  $all_meta_for_user = get_user_meta( 25 );
  print_r( $all_meta_for_user );
?>

Results:

Array ( 
[first_name] => Array ( [0] => Tom ) 
[last_name] => Array ( [0] => Auger) 
[nickname] => Array ( [0] => tomauger ) 
[description] => etc.... )

Have any doubt, then comment here!

Do something after WooCommerce order completed

If you want to do something after WooCommerce order completed then use this code.

woocommerce_order_status_completed action hook is a built-in hook of WooCommerce. It allows you to execute a function as when the order is completed. Add your stuff in inside of my_function().

add_action( 'woocommerce_order_status_completed', 'my_function' );
/*
 * Do something after WooCommerce sets an order on completed
 */
function my_function($order_id) {
	
	// order object (optional but handy)
	$order = new WC_Order( $order_id );
	// do some stuff here
	
}

Have any doubt, then comment here!