Which of the following is the correct syntax to create a shortcode function in WordPress?
easy📝 Syntax Q3 of 15
Wordpress - Shortcodes and Blocks
Which of the following is the correct syntax to create a shortcode function in WordPress?
Afunction my_shortcode() { echo 'Hello'; }
Bfunction my_shortcode() { print 'Hello'; }
Cfunction my_shortcode() { return 'Hello'; }
Dfunction my_shortcode() { alert('Hello'); }
Step-by-Step Solution
Solution:
Step 1: Understand shortcode function output
Shortcode functions must return a string, not echo or print it.
Step 2: Analyze options
Only function my_shortcode() { return 'Hello'; } returns the string correctly. The echo and print options output directly, which is incorrect. The alert option uses JavaScript, not PHP.
Final Answer:
function my_shortcode() { return 'Hello'; } -> Option C
Quick Check:
Shortcode function must return string [OK]
Quick Trick:Shortcode functions must return, not echo output [OK]
Common Mistakes:
Using echo or print inside shortcode functions
Confusing PHP with JavaScript syntax
Master "Shortcodes and Blocks" in Wordpress
9 interactive learning modes - each teaches the same concept differently