Adding Shortcode in WordPress
In this article, we’ll demonstrate how simple it is to add a shortcode in WordPress. We’ll also demonstrate how to use WordPress to build your own unique shortcodes.
Adding dynamic content to your WordPress posts, pages, and sidebars is simple when you use shortcodes. Shortcodes are commonly used in WordPress plugins and themes to provide specific content like sliders, image galleries, and contact forms.
Here’s how we can add shortcode in WordPress on our page or posts. We simply click on the block inserter and search for “shortcode”.
Then, just type in the shortcode in the field. In my case, I’m adding a form that I already have which is
.Once I publish the page, I should be able to see the form under the “Welcome to WordPress….. ” text.
Adding Shortcode on Custom Page or Custom Template using PHP
We need to use the built-in WordPress function to add shortcode in custom PHP template page.
For example, if we are just writing the shortcode within the HTML or PHP tag, it will not work as intended.
<?php
/*
Template Name: Custom Page
*/
get_header();
[CP_CALCULATED_FIELDS id="5" ]
?>
We will need to use the function “do_shortcode()” like below.
<?php
/*
Template Name: Custom Page
*/
get_header();
echo do_shortcode('[CP_CALCULATED_FIELDS id="5"]');
?>
More info about the shortcode function here : https://developer.wordpress.org/reference/functions/do_shortcode/
Also, we can create our own shortcode for our custom PHP page as well.
// function that runs when shortcode is called
function test_shortcode() {
$message = 'Hello world!';
return $message;
}
// register shortcode
add_shortcode('hello', 'test_shortcode');
Then, you can use “echo do_shortcode(‘[HELLO]’); ” to display the content of your custom shortcode
Learn how to adjust empty trash automatically on WordPress : https://www.kintechie.com/how-to-adjust-automatically-empty-trash-on-wordpress/
One thought on “How to Use Shortcode in WordPress (Including Custom Template)”
Comments are closed.