Using WordPress Settings API: Part II
Learn to build a form that shows the settings for our plugin.
The settings page for the Post Metrics plugin shows one option to set the display location. The default value is Beginning of post but even after changing it to End of post when we refresh the page, it shows the default value.
Fetch the setting from database
To make the HTML select element show the actual setting, we will edit the display_location_html
function. This function is responsible for showing the select
element with options for display location. We will use PHP to output the selected attribute from the database For every <option>
, we will check if the selected value from database matches that attribute’s value
field.
selected
is a WordPress function which compares two arguments and marks the option selected if they are equal. This function takes two arguments.
The first argument is the value from the database. The
get_option
function loads the value of the setting from the database.The second argument is the value attribute, which is 0 for the first option tag and 1 for the second tag.
Get hands-on with 1400+ tech skills courses.