0
0
Wordpressframework~15 mins

Shortcodes with parameters in Wordpress - Mini Project: Build & Apply

Choose your learning style9 modes available
Shortcodes with parameters
📖 Scenario: You are building a WordPress site and want to create a shortcode that shows a greeting message. The message should change based on a name you provide as a parameter.
🎯 Goal: Create a WordPress shortcode called [greet] that accepts a name parameter and displays a greeting like "Hello, [name]!".
📋 What You'll Learn
Create a function called greet_shortcode that accepts attributes.
Use shortcode_atts to set a default name parameter to "Guest".
Return a greeting string using the name parameter.
Register the shortcode greet with the greet_shortcode function.
💡 Why This Matters
🌍 Real World
Shortcodes let WordPress users add dynamic content easily inside posts or pages without coding each time.
💼 Career
Knowing how to create and customize shortcodes is useful for WordPress developers and site builders to extend site functionality.
Progress0 / 4 steps
1
Create the shortcode function
Create a function called greet_shortcode that accepts one parameter called $atts.
Wordpress
Need a hint?

Start by defining a function named greet_shortcode that takes $atts as input.

2
Set default parameter with shortcode_atts
Inside the greet_shortcode function, use shortcode_atts to create a variable called $atts with a default name set to "Guest".
Wordpress
Need a hint?

Use shortcode_atts with an array setting 'name' => 'Guest' and pass $atts as the second argument.

3
Return the greeting message
In the greet_shortcode function, return a string that says "Hello, " followed by the name parameter from $atts, and then an exclamation mark.
Wordpress
Need a hint?

Use string concatenation to build the greeting using $atts['name'].

4
Register the shortcode
After the greet_shortcode function, add a line to register the shortcode greet with the greet_shortcode function using add_shortcode.
Wordpress
Need a hint?

Use add_shortcode with the shortcode name 'greet' and the function name 'greet_shortcode'.