WordPress Snippets


<?php
/* Tag list */
the_tags(' ', ' ', ' ');
echo get_the_tag_list("<ul class=\"list-unstyled text-center\"><li>", "</li><li>", "</li></ul>");

/* Get All tags */
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
	$tag_link = get_tag_link( $tag->term_id );
			
	$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
	$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;


/* Post thumbnail */
if(has_post_thumbnail()){
	the_post_thumbnail('large', array('class' => 'img-fluid'));   
}

/* Comments */
if(comments_open()): ?>
	<div class="col-md-10 offset-md-1">
		<?php comments_template();?>
	</div>
<?php endif;


/* Post pagination */
the_posts_pagination(array(
	"screen_reader_text" => ' '
	)
);

/* template file included */
get_template_part('');


/* php file or lib included */
get_theme_file_path('');


/* post navigation */
next_post_link();
echo "<br>";
previous_post_link();


/* password protected post */
function alpha_protected_post($excerpt){
	if (! post_password_required()) {
		return $excerpt;
	}else{
		echo get_the_password_form();
	}
}

add_filter('the_excerpt', 'alpha_protected_post');


/* css added in header */
function launcher_style(){
	if (is_page()) {
		$thumb_img = get_the_post_thumbnail_url( null, 'large' );
		?>
		<style>
			.home-side{
				background-image: url(<?php echo $thumb_img;?>);
			}
		</style>
		<?php
	}
}

add_action( 'wp_head', 'launcher_style', 11);


/* custom meta field data retrieve */
$placeholder = get_post_meta( get_the_ID(), 'placeholder', true );


/* post format */
$alpha_format = get_post_format();


/* author info */
get_the_author_meta();


/* author image */
echo get_avatar( get_the_author_meta( 'id') );


/* body class remove or add */
function alpha_body_class($classes){
	unset($classes[array_search('wp-custom-logo', $classes)]);
	$classes[] = "newclass_add";
	return $classes;
}

add_filter( 'body_class','alpha_body_class');


/* post class remove or add */
function alpha_post_class($classes){
	unset($classes[array_search('tag-blog', $classes)]);	
	return $classes;
}

add_filter( 'post_class','alpha_post_class');


/* image srcset remove */
function alpha_image_srcset(){
	return null;
}
add_filter( 'wp_calculate_image_srcset', 'alpha_image_srcset' );


/* If you want to override a function into the child theme, just put in a check */
if(!function_exists('alpha_date')) {
	function alpha_date(){
		echo date("d/m/y");
	}
}

?>
Advertisements

Woocommerce Snippets

/****************************/

Woocommerce:
===============

|| Related products
   plugins > woocommerce > templates > single-product > related.php


|| how to move best seller product on the top
   go to storefront-template-hooks.php
   add_action( 'homepage', 'storefront_best_selling_products', 70 ); 
                      to
   add_action( 'homepage', 'storefront_best_selling_products', 10 );


|| img src directory
   <img src="<?php echo get_template_directory_uri(); ?>/assets/images/bd.jpg" width="" height="" alt="" />

|| custom css add
   <link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(). '/assets/css/custom.css' ?>">

|| Add Custom Fonts, top of the style.css file
   @font-face {
	font-family: calib;
	src: url('assets/fonts/calibrib.ttf');
}

|| search form anywhere
   <form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
	<label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
	<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />
	<input type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" />
	<input type="hidden" name="post_type" value="product" />
   </form>


|| Number of related products
   add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' );
   function jk_related_products_args( $args ) {
	$args['posts_per_page'] = 4; // 4 related products
	$args['columns'] = 3; // arranged in 3 columns
	return $args;
}


|| How to Remove Product Review,  go to functions.php
   	add_filter( 'woocommerce_product_tabs', 'helloacm_remove_product_review', 99);
	function helloacm_remove_product_review($tabs) {
		unset($tabs['reviews']);
		return $tabs;
	}

|| remove sidebar from product view page,  go to functions.php
   /* Storefront Theme – Remove WooCommerce Sidebar on the Single Product Page */
	add_action( 'get_header', 'bbloomer_remove_storefront_sidebar' );
	function bbloomer_remove_storefront_sidebar() {
	    if ( is_product() ) {
	        remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
	    }
	}

|| add to cart text change, go to functions.php
   
   /* custom function add for add to cart text change */
	add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );    // 2.1 + 
	function woo_archive_custom_cart_button_text() {
	    return __( 'BY NOW', 'woocommerce' );
	}

|| how to change product column
   your theme > inc > woocommerce > storefront-woocommerce-template-functions.php
   search storefront_loop_columns

|| How do I add WooCommerce product categories to a custom menu?
    Go to Appearance > Menus
    In the upper right corner, click on Screen Options and ensure the "Products" and "Product Categories" boxes are checked

|| Display My Account link in a template file
   <?php if ( is_user_logged_in() ) { ?>
      <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account','woothemes'); ?>"><?php _e('My Account','woothemes'); ?></a>
   <?php } 
   else { ?>
      <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Login / Register','woothemes'); ?>"><?php _e('Login / Register','woothemes'); ?</a>
   <?php } ?>

|| How to enable registration on "My Account" page
   Go to WooCommerce > Settings > Account and  Enable customer registration on the "My account" page.

|| How To Change Product Images Size?
   1st woocommerce > setting > product > Display > Product Images
   2nd Regenerate Thumbnails plugins install and active
   tutorial link: https://www.youtube.com/watch?v=YVLb3eG0JdI
   

|| product detail page sku and category
   wp-content\plugins\woocommerce\templates\single-product\meta.php

|| Product listing page product name
   content-product.php
   <a href="<?php the_permalink(); ?>">---- </a>

|| product view page review tab remove

<?php
	add_filter( 'woocommerce_product_tabs', 'sb_woo_remove_reviews_tab', 98);
	function sb_woo_remove_reviews_tab($tabs) {

	 unset($tabs['reviews']);

	 return $tabs;
	}
?>

|| product view short description:
   woocommerce/single-product/short-description.php

|| breadcrumbs slash '/' replace as '>'
   function.php

<?php
	add_filter( 'woocommerce_breadcrumb_defaults', 'my_change_breadcrumb_delimiter' );
	function my_change_breadcrumb_delimiter( $defaults ) {
	 // Change the breadcrumb delimiter from '/' to '>'
	 $defaults['delimiter'] = ' > ';
	 return $defaults;
	}
?>

|| Default breadcrumbs remove from product page:
   function.php
   <?php remove_action( 'woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0);?>

|| prodcut view page related product remove
   function.php
   <?php remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );?>

|| Add to cart remove:
        <?php
		/* product listing page add to cart remove */
		function remove_loop_button(){
			remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
		}
		add_action('init','remove_loop_button');
			or
		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
		
		/* product detail page add to cart remove */
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
		
	?>

|| Price Remove
       <?php
		/* product listing page price remove */
		remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
		/* product detail page price remove */
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
		
	?>

|| edit products page:

You need to add a folder to your theme named "woocommerce" and copy the contents from the woocommerce plugin folder under "templates", 
copy archive-product.php and paste your theme woocommerce folder. The loop folder contains the files you want to use. 
so, in your theme, you'd have something like
themefolder/woocommerce/archive-product.php
themefolder/woocommerce/loop/...
<?php
	do_action( 'woocommerce_sidebar' )
?>


|| Dynamic Product Category in sidebar:
   Appearance > Widget:
   drag and drop (WooCommerce Product Categories) in (primary sidebar) or (secondary widget area)

|| Dynamic product category anywhere:
   function.php

<?php
	/* product category */
	function wooCommerceCategories() {

		$taxonomy     = 'product_cat';
		$orderby      = 'name';  
		$show_count   = 0;      // 1 for yes, 0 for no
		$pad_counts   = 0;      // 1 for yes, 0 for no
		$hierarchical = 1;      // 1 for yes, 0 for no  
		$title        = '';  
		$empty        = 0;

		$args = array(
			'taxonomy'     => $taxonomy,
			'orderby'      => $orderby,
			'show_count'   => $show_count,
			'pad_counts'   => $pad_counts,
			'hierarchical' => $hierarchical,
			'title_li'     => $title,
			'hide_empty'   => $empty
		);
		$all_categories = get_categories( $args );
		 foreach ($all_categories as $cat) {
			if($cat->category_parent == 0) {
				$category_id = $cat->term_id;       
				echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; 
				$args2 = array(
						'taxonomy'     => $taxonomy,
						'child_of'     => 0,
						'parent'       => $category_id,
						'orderby'      => $orderby,
						'show_count'   => $show_count,
						'pad_counts'   => $pad_counts,
						'hierarchical' => $hierarchical,
						'title_li'     => $title,
						'hide_empty'   => $empty
				);
				$sub_cats = get_categories( $args2 );
				if($sub_cats) {
					foreach($sub_cats as $sub_category) {
						echo  $sub_category->name ;
					}   
				}
			}       
		}
	}
	/**/
?>

/* function call */

<div class="product_category col-lg-1 col-md-2">
     <?php if(function_exists("wooCommerceCategories")) wooCommerceCategories(); ?>
</div>