Function and usage of form_option() in wordpress

Question

Answers ( 1 )

    0
    2024-01-06T10:05:09+00:00

    The form_option() function in WordPress is a convenient tool for handling options in form elements. Here's a detailed explanation and sample usage:

    Function: form_option( string $option )

    Purpose:

    form_option() is used to retrieve and display the value of a specified option from the WordPress database, sanitizing it for safe use in HTML forms. This function ensures that the option value is properly escaped, preventing issues like cross-site scripting (XSS) attacks, which are common concerns when outputting data in HTML.

    Parameters:

    • $option (string) (Required): The name of the option you want to retrieve and display. This is the unique identifier for the option in the WordPress database.

    Return Value:

    The function doesn't return a value. Instead, it directly prints (echoes) the sanitized value of the specified option.

    Usage:

    1. Basic Usage: To use form_option(), you simply pass the name of the option you want to output. For example, if you have an option named 'my_custom_option' stored in your WordPress options table, you can display its value in a form field like this:

      <input type="text" name="custom_option" value="<?php form_option('my_custom_option'); ?>" />
      

      In this example, the function retrieves the value of 'my_custom_option' from the database, sanitizes it, and prints it as the value of the input field.

    2. In a WordPress Settings Page: If you're creating a settings page for a plugin or theme, you can use form_option() to populate the form fields with values stored in the WordPress options table. This is particularly useful for settings that need to be easily configurable by the user.

      <label for="site_title">Site Title:</label>
      <input type="text" id="site_title" name="site_title" value="<?php form_option('blogname'); ?>" />
      

      Here, form_option('blogname') is used to display the site title, a default option in WordPress, in a text input field.

    Notes:

    • The form_option() function is specifically designed for use in HTML forms. It should not be used for general data retrieval where sanitization for forms is not required.
    • Always use form_option() when outputting option values in form fields to ensure the data is safe for HTML display.

    Sample Usage in Context:

    Here's an example of how form_option() might be used in a WordPress theme or plugin settings page:

    <form action="options.php" method="post">
        <h3>My Plugin Settings</h3>
        <label for="api_key">API Key:</label>
        <input type="text" id="api_key" name="api_key" value="<?php form_option('my_plugin_api_key'); ?>" />
        <?php submit_button(); ?>
    </form>
    

    In this form, the value of the 'my_plugin_api_key' option is displayed in an input field. When the form is submitted, the option can be updated with the new value provided by the user.

Leave an answer