0

I want to add few settings fields dynamically on settings page depending on user action. I am planning to register these new fields dynamically once user asks for it and show the corresponding HTML using jQuery. Is it possible to register settings once you are already on settings page? Also, is there a better way to do this?

1

Better to avoid such a thing. The settings API has two "faces", one the UI, and the other is handling the form submission. The form submission is handled on a different URL than the UI (/wp-admin/options.php) and therefor your "user action" depended code will not run and the handling of storing the setting in the DB might fail.

You need to register properly all fields, and then you can use CSS or JS to show/hide the relevant UI.

1

you can use setting API like so:

 <?php 
 // ------------------------------------------------------------------
 // Add all your sections, fields and settings during admin_init
 // ------------------------------------------------------------------
 //

 function eg_settings_api_init() {
    // Add the section to reading settings so we can add our
    // fields to it
    add_settings_section(
        'eg_setting_section',
        'Example settings section in reading',
        'eg_setting_section_callback_function',
        'reading'
    );

    // Add the field with the names and function to use for our new
    // settings, put it in our new section
    add_settings_field(
        'eg_setting_name',
        'Example setting Name',
        'eg_setting_callback_function',
        'reading',
        'eg_setting_section'
    );

    // Register our setting so that $_POST handling is done for us and
    // our callback function just has to echo the <input>
    register_setting( 'reading', 'eg_setting_name' );
 } // eg_settings_api_init()

 add_action( 'admin_init', 'eg_settings_api_init' );


 // ------------------------------------------------------------------
 // Settings section callback function
 // ------------------------------------------------------------------
 //
 // This function is needed if we added a new section. This function 
 // will be run at the start of our section
 //

 function eg_setting_section_callback_function() {
    echo '<p>Intro text for our settings section</p>';
 }

 // ------------------------------------------------------------------
 // Callback function for our example setting
 // ------------------------------------------------------------------
 //
 // creates a checkbox true/false option. Other types are surely possible
 //

 function eg_setting_callback_function() {
    echo '<input name="eg_setting_name" id="eg_setting_name" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'eg_setting_name' ), false ) . ' /> Explanation text';
 }

after that you can use jQuery or Javascript to hide/show this setting field/section depending on user action

check this link for more

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.