• currently I facing the problem how to add an option field or setting dynamical with a button click.
    There are a few similar questions in other boards but no one helped me respectively many where not answered.

    So I hope that I can give you all the informations you need to give me a hint what I can do.

    My idea was to create a section like here:

    `function initialise_content_options(){

    add_settings_section(
    ‘content_design_section’,
    ‘Content design options’,
    ‘content_design_callback’,
    ‘theoretisch_staticPage_options&tab=content_options’
    );

    if (!isset($_POST[“add_or_delete”])) {
    $numberOfSections = 1;
    }else{
    $numberOfSections = $_POST[“add_or_delete”];
    }

    for ($i = 0; $i < $numberOfSections; $i++) {
    // here I create a section with many fields`

    with a loop which creates the other sections dynamical.

    In the section above (content_design_section) I print a description and an add button. So if the user clicks on that button I increase the value of a hidden field (with the name add_or_delete) with javascript. Then I read the value of the hidden field with $_POST["add_or_delete"] and create the number of sections with its fields in the loop. This is inside the loop:

    `$option_name = ‘content_options_group’.$numberOfSections;

    // fetch existing options
    $option_values = get_option($option_name);

    // is called to automate saving the values of the fields
    register_setting(
    ‘content_section’,
    $option_name
    );

    $default_values = array(
    ‘topic’ => ”,
    ‘description’ => ”,
    ‘picUpload’ => ”
    );

    // parse option value into predefined keys
    $data = shortcode_atts($default_values,$option_values);

    add_settings_section(
    ‘section_design_section_’ . $numberOfSections,
    ‘Section ‘ . $numberOfSections,
    ‘section_design_callback’,
    ‘theoretisch_staticPage_options&tab=content_options’
    );

    add_settings_field(
    ‘topic’,
    ‘Topic’,
    ‘topic_callback’,
    ‘theoretisch_staticPage_options&tab=content_options’,
    ‘section_design_section_’ . $numberOfSections,
    array(
    ‘name’ => ‘topic’,
    ‘value’ => esc_attr($data[‘topic’]),
    ‘option_name’ => $option_name
    )
    ); // and some more fields`

    So this is my approach, but it doesn’t work. I think it is because the php is not executed again when I press the add Button.
    So do I only have to reload my site, or is it possible without reload?

    Each section has a delete link too. If that link is clicked the section of this link should be deleted. This currently doesn’t work either, but I think the problem is the same like with the add button.

    If you need more informations please write it in the comments I will add it then.
    Thank you!

Viewing 1 replies (of 1 total)
  • Thread Starter theoretisch

    (@theoretisch)

    Sorry for the bad formatted question.
    Unfortunately I have not the authorization to edit and correct it.
    Hopefully you can see what I mean.

Viewing 1 replies (of 1 total)

The topic ‘Create settings field on button click’ is closed to new replies.