Tag: snippet

  • How to display product price using shortcode

    Here is the code snippet that you can use to create a shortcode and use it in any of your posts or pages to display the price of a specific product. You just have to specify the woocommerce product ID of that product like the given example. [woo_product_price id=20]

    function wptips_woo_price_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'id' => null,
        ), $atts, 'woocommerce_price' );
    
        $html = '';
    
        if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
             $_product = wc_get_product( $atts['id'] );
             $html = $_product->get_price();
        }
        return $html;
    }
    add_shortcode( 'woo_product_price', 'wptips_woo_price_shortcode' );
  • How to add text before or after price in WooCommerce

    You can use the WooCommerce filter woocommerce_get_price_html and woocommerce_cart_item_price to add a string before or after the price in WooCommerce product and cart pages.

    Here is the code that you can copy and paste in your active theme’s functions.php file:

    function wptips_custom_html_addon_to_price( $price, $product ) {
     $html_price_prefix = '<span class="price-prefix">prefix</span>'; // custom prefix html that you want to add before price
     $html_price_suffix = '<span class="price-suffix"> suffix</span>'; // custom suffix html that you want to add after price
    
     if ( !empty( $price ) ) {
    	 $price = $html_price_prefix . ' ' . $price . ' ' . $html_price_suffix;
    	  return $price;
    	} else {
    	  return $price;
    	}
    }
    add_filter( 'woocommerce_get_price_html', 'wptips_custom_html_addon_to_price', 999, 2 );
    add_filter( 'woocommerce_cart_item_price', 'wptips_custom_html_addon_to_price', 999, 2 );
    

    You can use the above simple code snippet in your active theme, or use the code in a custom plugin to add text next to the price in WooCommerce. You can also add a prefix or suffix HTML or TEXT to the price on the product page and cart page as well.

  • Remove meta generator tags by WordPress and other plugins

    Meta generator tag in WordPress reveals the plugin or WordPress core version which makes WordPress vulnerable. So, here i list snippets to disable meta tag generator by various plugins.

    Remove WordPress & Woocommerce meta tag generator

    This code will disable meta tag generated by WordPress, Woocommerce and many other standard plugins that hook up with the wp_generator action.

    remove_action('wp_head', 'wp_generator');
    add_filter( 'the_generator', '__return_null' );

    Remove meta generator tag of revslider plugin

    // remove revslider meta generator tag
    add_filter( 'revslider_meta_generator', '__return_empty_string' );

    Remove meta generator tag of wpbakery page builder plugin

    // remove wpbakery visual_composer meta generator tag
    add_action('wp_head', 'wpboys_remove_vc_metadata', 1);
    function wpboys_remove_vc_metadata() {
      if ( class_exists( 'Vc_Manager' ) ) {
    	remove_action('wp_head', array(visual_composer(), 'addMetaData'));
      }
    }

    Remove meta generator tag of sitepress wpml plugin

    // remove wpml meta generator tag
    if ( !empty ( $GLOBALS['sitepress'] ) ) {
            function wpboys_remove_wpml_generator() {
                remove_action(
                    current_filter(),
                    array ( $GLOBALS['sitepress'], 'meta_generator_tag' )
                );
            }
            add_action( 'wp_head', 'wpboys_remove_wpml_generator', 0 );
        }

    Complete code Snippet to remove meta generator tags from your WordPress Website

    // Disable meta generator tags
    // author : wpboys
    remove_action('wp_head', 'wp_generator');
    add_filter( 'the_generator', '__return_null' );
    // remove revslider meta generator tag
    add_filter( 'revslider_meta_generator', '__return_empty_string' );
    // remove wpbakery visual_composer meta generator tag
    add_action('wp_head', 'wpboys_remove_vc_metadata', 1);
    function wpboys_remove_vc_metadata() {
      if ( class_exists( 'Vc_Manager' ) ) {
    	remove_action('wp_head', array(visual_composer(), 'addMetaData'));
      }
    }
    // remove wpml meta generator tag
    if ( !empty ( $GLOBALS['sitepress'] ) ) {
            function wpboys_remove_wpml_generator() {
                remove_action(
                    current_filter(),
                    array ( $GLOBALS['sitepress'], 'meta_generator_tag' )
                );
            }
            add_action( 'wp_head', 'wpboys_remove_wpml_generator', 0 );
        }

    Remove generator tags from Feed

    WordPress-based website feeds have a generator tag that reveals the WordPress core version. You can remove the generator tag using the following code :

        remove_action( 'rss2_head', 'the_generator' );
        remove_action( 'rss_head',  'the_generator' );
        remove_action( 'rdf_header', 'the_generator' );
        remove_action( 'atom_head', 'the_generator' );
        remove_action( 'commentsrss2_head', 'the_generator' );
        remove_action( 'opml_head', 'the_generator' );
        remove_action( 'app_head',  'the_generator' );
        remove_action( 'comments_atom_head', 'the_generator' );