0

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

I already looked here,here or here.

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!

3
  • What is missing? Is it too easy or just not possible ? Commented Nov 23, 2016 at 7:19
  • Did you solve this? Commented Apr 27, 2017 at 12:22
  • no, sadly not... it seems that no one knows how to do it. Commented Apr 27, 2017 at 14:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.