Function Name: do_shortcode()
The do_shortcode() function in WordPress is used to execute any shortcode present in a given content string. A shortcode is a small piece of code that allows the user to perform complex actions by inserting a single line of code in the content area of a WordPress post or page.
The do_shortcode() function takes a single parameter which is the content string. It then searches for any shortcode present in the content string and replaces it with the corresponding output.
The function is often used in WordPress themes and plugins to process shortcodes present in a post or page. This can be useful for adding custom functionality to a WordPress site without having to write complex code.
Here’s an example usage of the do_shortcode() function:
Let’s say you have created a custom shortcode that displays the current date and time. The shortcode looks like this: [current_datetime]
To display the date and time on a WordPress page, you would simply add the shortcode to the content area of the page. For example:
Hello World! The current date and time is: [current_datetime]
The do_shortcode() function would then process the shortcode and replace it with the current date and time when the page is loaded.
Here’s the code you would use to process the shortcode:
$content = ‘Hello World! The current date and time is: [current_datetime]’; echo do_shortcode($content);
This would output:
Hello World! The current date and time is: 2022-09-22 14:30:00
In summary, the do_shortcode() function in WordPress is a powerful tool that allows developers to add dynamic functionality to a WordPress site without having to write complex code.