0

Stepping into advanced plugin development I've been following along with the Plugin Handbook but I did not see it mentioned how you're to reference what has been added to the settings page. So for example, when you visit the sample settings page the menu created is:

function wporg_options_page() {
    // add top level menu page
    add_menu_page(
        'WPOrg',
        'WPOrg Options',
        'manage_options',
        'wporg',
        'wporg_options_page_html'
    );
}

and on this page you have a single field:

function wporg_field_pill_cb($args) {
    // get the value of the setting we've registered with register_setting()
    $options = get_option('wporg_options');
    // output the field
    ?>
    <select id="<?= esc_attr($args['label_for']); ?>"
            data-custom="<?= esc_attr($args['wporg_custom_data']); ?>"
            name="wporg_options[<?= esc_attr($args['label_for']); ?>]"
    >
        <option value="red" <?= isset($options[$args['label_for']]) ? (selected($options[$args['label_for']], 'red', false)) : (''); ?>>
            <?= esc_html('red pill', 'wporg'); ?>
        </option>
        <option value="blue" <?= isset($options[$args['label_for']]) ? (selected($options[$args['label_for']], 'blue', false)) : (''); ?>>
            <?= esc_html('blue pill', 'wporg'); ?>
        </option>
    </select>
    <p class="description">
        <?= esc_html('You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe.', 'wporg'); ?>
    </p>
    <p class="description">
        <?= esc_html('You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes.', 'wporg'); ?>
    </p>
    <?php
}

and if I wanted to create a widget which referenced what has been chosen in the wporg_field_pill_cb function I've browsed through the tag and but I don't see something solid. When I reference

Where in the documentation does it discuss how you can reference what is used in the that setting's page? Do you call the registered setting wporg_options in widget()?

My apologies if this was unclear but my goal is to learn how to, in the sub menu or top menu have a form with settings that will be referenced by the widget or rendered by the widget. I used the above settings plugin as an example to see what in that settings page I would use to get the option value that was selected.

3

The setting API, which its usage is being demonstrated in that code is meant to be use as a framework to create settings pages which result in update of values in options.

Widget settings are is a somewhat more abstract level. They are stored in options, but there is no flexibility how they are stored, they are all stored as array of mostly constant fields of settings where the index is a widget identifier.

This abstraction aka widgets API follows a different flow than the settings API.

There is no problem in calling get_option anywhere that you need it the widget code, but this is the only integration that you should probably have.

You should not set an option when a widget is saved because this kind of side effect will confuse the user, and the customizer will (don't remember why) complain.

You can manage widgets is a setting page, but how widgets are stored and retrieved is one of the least documented areas in core, and while it is surely possible, it is not very trivial and I don't remember seeing anyone doing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.