How to Create a WooCommerce Plugin to Display Product Descriptions with a Shortcode

How to Create a WooCommerce Plugin to Display Product Descriptions with a Shortcode

Step 1: Setting Up the Plugin

First, you’ll need to set up your WordPress development environment if you haven’t already. This typically involves having a local server environment like XAMPP, WAMP, or MAMP, along with a fresh installation of WordPress. Once your environment is ready, follow these steps:

1.Create a New Plugin Folder:

  • Navigate to wp-content/plugins in your WordPress installation directory.
  • Create a new folder named wc-product-description-echo.

2.Create the Main Plugin File:

    • Inside the wc-product-description-echo folder, create a file named wc-product-description-echo.php.
    • Open this file in your code editor and add the following header information:
				
					<?php
/**
 * Plugin Name: WooCommerce Product Description Echo
 * Description: A plugin to echo the WooCommerce product description on the front end using a shortcode.
 * Version: 1.0
 * Author: Your Name
 * Author URI: Your Website
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

				
			

Step 2: Create the Function to Echo Product Description

Now, we need to create a function that fetches and returns the product description. This function will be hooked into a shortcode, which users can add to their posts or pages.

  1. Add the Function:

    • Below the header information in your wc-product-description-echo.php file, add the following function:
				
					// Function to echo the product description
function wc_product_description_echo( $atts ) {
    global $post;

    // Check if WooCommerce is active and we're in a single product page
    if ( function_exists( 'is_product' ) && is_product() ) {
        $product = wc_get_product( $post->ID );

        if ( $product ) {
            return $product->get_description();
        }
    }

    return '';
}

				
			

Explanation:

  • Global $post: We use the global $post variable to access the current post object.
  • Check WooCommerce and Product Page: The function checks if WooCommerce is active using function_exists( 'is_product' ) and ensures we are on a single product page with is_product().
  • Get Product Description: wc_get_product( $post->ID ) retrieves the product object, and get_description() fetches the product description.
  • Return Description: If the product exists, the function returns its description; otherwise, it returns an empty string.

Step 3: Register the Shortcode

To make our function accessible via a shortcode, we need to register it with WordPress.

  1. Register the Shortcode:

    • Add the following code below your function:
				
					// Register the shortcode
function register_wc_product_description_shortcode() {
    add_shortcode( 'wc_product_description', 'wc_product_description_echo' );
}

add_action( 'init', 'register_wc_product_description_shortcode' );

				
			

Explanation:

  • add_shortcode: This function registers the shortcode [wc_product_description] and maps it to our wc_product_description_echo function.
  • add_action: The init action hook is used to initialize the shortcode registration when WordPress loads.

Complete Code

				
					<?php
/**
 * Plugin Name: WooCommerce Product Description Echo
 * Description: A plugin to echo the WooCommerce product description on the front end using a shortcode.
 * Version: 1.0
 * Author: Your Name
 * Author URI: Your Website
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Function to echo the product description
function wc_product_description_echo( $atts ) {
    global $post;

    // Check if WooCommerce is active and we're in a single product page
    if ( function_exists( 'is_product' ) && is_product() ) {
        $product = wc_get_product( $post->ID );

        if ( $product ) {
            return $product->get_description();
        }
    }

    return '';
}

// Register the shortcode
function register_wc_product_description_shortcode() {
    add_shortcode( 'wc_product_description', 'wc_product_description_echo' );
}

add_action( 'init', 'register_wc_product_description_shortcode' );

				
			

Step 4: Activate and Use the Plugin

  1. Activate the Plugin:

    • Go to the WordPress admin dashboard.
    • Navigate to Plugins > Installed Plugins.
    • Find WooCommerce Product Description Echo and click Activate.
  2. Use the Shortcode:

    • You can now use the [wc_product_description] shortcode in any post or page to display the product description.
    • Make sure to use this shortcode within the content area of a single product page to see the description.

Conclusion

By following this guide, you have created a custom WooCommerce plugin that displays the product description using a shortcode. This simple yet powerful addition can enhance the functionality of your WooCommerce store and provide a better user experience for your customers.

WooCommerce plugin development

Display product description WooCommerce

Custom shortcode WooCommerce

WordPress WooCommerce tutorial

WooCommerce product description shortcode

Featured Post

Leave a Comment

Your email address will not be published. Required fields are marked *