How to Create a WooCommerce Plugin to Display Product Descriptions with a Shortcode
Blog / PHP / Plugin / WordPress Elementor
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 namedwc-product-description-echo.php
. - Open this file in your code editor and add the following header information:
- Inside the
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.
Add the Function:
- Below the header information in your
wc-product-description-echo.php
file, add the following function:
- Below the header information in your
// 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 withis_product()
. - Get Product Description:
wc_get_product( $post->ID )
retrieves the product object, andget_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.
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 ourwc_product_description_echo
function. - add_action: The
init
action hook is used to initialize the shortcode registration when WordPress loads.
Complete Code
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
Activate the Plugin:
- Go to the WordPress admin dashboard.
- Navigate to
Plugins > Installed Plugins
. - Find
WooCommerce Product Description Echo
and clickActivate
.
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.
- You can now use the
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.