How To Add Blog Content Via Shortcode
Shortcodes are the easiest way to add dynamic content to your WordPress posts, pages, and sidebar. There are many plugins and themes that use shortcodes for contact forms, image galleries, and sliders.
This article will guide you on creating shortcodes and using them around the WordPress site both on Gutenberg and theme files. Shortcodes are displayed inside the square brackets [myshortcode].
To better understand shortcodes, lets take a look at the background of why they were added in the first place.
WordPress filters all content to make sure that no one uses posts and page content to insert malicious code in the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.
But what if you wanted to run some custom code inside your posts to display related posts, banner ads, contact forms, galleries, etc?. This is where Shortcode API comes in. Basically, it allows developers to add their code inside a function and then register that function with WordPress as a shortcode, so users can easily use it without having any coding knowledge. When WordPress finds the shortcode it will automatically run the code associated with it.
Adding shortcode with Gutenberg
To add the shortcode in the classic editor, search for the shortcode and enter the shortcode within square brackets. Refer to the image below.
How to Add a Shortcode in WordPress Theme Files
Shortcodes are meant to be used inside WordPress posts, pages, and widgets. However, sometimes you may want to use a shortcode inside a WordPress theme file. WordPress makes it easy to do that, but you will need to edit your WordPress theme files.
Basically, you can add a shortcode to any WordPress theme template by simply adding the following code.
<?php echo
do_shortcode("[your_shortcode]"); ?>
How to Create Your Own Custom Shortcode in WordPress
Shortcodes can be really useful when you want to add dynamic content or custom code inside the WordPress post and pages. However, If you are comfortable with writing PHP code, then here is a sample code that you can use as a template.
In this code, we first created a function that runs some code and returns the output. After that, we created a new shortcode called 'greeting' and told WordPress to run the function we created.
You can now use add this shortcode to your posts, pages, and widgets using the following code, It will run the function you created and show the desired output.
[greeting]
Try this stuff to your WordPress site and respond below if you have queries on it.
How To Add Blog Content Via Shortcode
Source: https://medium.com/timeless/how-to-add-a-shortcode-in-wordpress-c594f6ca784
Posted by: handylocatell.blogspot.com
0 Response to "How To Add Blog Content Via Shortcode"
Post a Comment