Bird
0
0

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:
  1. Step 1: Understand shortcode function output

    Shortcode functions must return a string, not echo or print it.
  2. 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.
  3. Final Answer:

    function my_shortcode() { return 'Hello'; } -> Option C
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes