Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Reading and Writing Settings in WordPress
📖 Scenario: You are building a simple WordPress plugin that stores and retrieves a custom setting for your site. This setting controls whether a special greeting message is shown to visitors.
🎯 Goal: Build a WordPress plugin that creates a setting called show_greeting, saves its value, and reads it to decide if the greeting message should appear.
📋 What You'll Learn
Create a setting named show_greeting with a default value of false
Add a configuration variable to hold the option group name myplugin_options
Write code to update the show_greeting setting to true
Write code to read the show_greeting setting and display a greeting message if it is true
💡 Why This Matters
🌍 Real World
Many WordPress plugins need to save and read settings to customize behavior for site owners.
💼 Career
Understanding how to read and write settings is essential for WordPress plugin development and customization.
Progress0 / 4 steps
1
Create the initial setting
Create a variable called default_settings as an array with the key 'show_greeting' set to false. Then use add_option to add 'myplugin_settings' with default_settings as its value.
Wordpress
Hint
Use add_option('myplugin_settings', $default_settings); to create the setting with default values.
2
Add option group configuration
Create a variable called option_group and set it to the string 'myplugin_options'.
Wordpress
Hint
This variable will help group your plugin's settings.
3
Update the setting value
Use get_option to get the 'myplugin_settings' array into a variable called settings. Then set settings['show_greeting'] to true. Finally, use update_option to save settings back to 'myplugin_settings'.
Wordpress
Hint
Use get_option and update_option to read and write settings.
4
Read the setting and display greeting
Use get_option to get 'myplugin_settings' into settings. Then use an if statement to check if settings['show_greeting'] is true. If yes, echo the string 'Hello, welcome to our site!'.
Wordpress
Hint
Check the setting and show the greeting only if it is true.
Practice
(1/5)
1. Which WordPress function is used to read a saved setting from the database?
easy
A. get_option
B. save_option
C. set_option
D. read_option
Solution
Step 1: Understand the purpose of each function
get_option is the WordPress function designed to retrieve saved settings. The others are not valid WordPress functions.
Step 2: Confirm the correct function for reading settings
Since get_option reads the saved option value, it is the correct choice.