WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic OPTIONS (12 posts)

  1. ssdesign
    Member
    Posted 8 years ago #

    Hi,
    I am planning to develop a wordpress plugin and have following question.

    I want to have dynamically generated form.
    for eg. I want to have a form where a user can fill Educational Qualification. I am planning to provide a ADD ANOTHER QUALIFICATION button.

    This button will create new form fields using AJAX technique. Hence, I wil have no control over the number of qualifications a User is adding or deleting on the OPTIONS page.

    Now coming to my question,
    Is it possible to dynamically know the number of fields a user has added? will get_options return everything? or I need to predefine everything in the plugin PHP?

    Looking for some suggestions.

    Thanks

  2. Austin Matzko
    Member
    Posted 8 years ago #

    will get_options return everything?

    There is no WordPress function named "get_options." "get_option" will return specifically requested options, such as the site's url: get_option('siteurl');

  3. ssdesign
    Member
    Posted 8 years ago #

    sorry for that miss type. I was refering to get_option

    This means that I can not create a plugin where the form fields are not determinate?

    I need to pre-define everything in code.

    Am i right?

  4. Austin Matzko
    Member
    Posted 8 years ago #

    You could save all the form fields as one array or object and store it in and option named something like 'ssdesigns_options' or whatever.

  5. mutube
    Member
    Posted 8 years ago #

    filosofo: If you're wanting to allow people to extend a form you need a few basic steps.

    Firstly, take the form element name & append a number, e.g. name="qualification1". For each added qualification increment that number - so you'll end up with a list of form elements like 1 to X.

    When the form is submitted you can then do a loop through the returned form until you hit a blank (i.e. the end of the array).


    $x=1;
    while($_POST{'qualification' . $x}!='') { $qualifications[$x]=$_POST{'qualification'.$x};
    }
    $options['qualifications']=$qualifications;

    Haven't checked the code, but that *should* give you an array of qualifications. You can should be able to save this data with update_option as normal (apparently WordPress will serialise arrays automatically, although I've not tried it).

    Test this with a sample HTML form you've built up yourself.

    Incidentally, it's not really AJAX if you're just adding extra fields to a form. Well... you could do it that way, but you'd be better off with some simple Javascript.

    Hope that helps,

  6. ssdesign
    Member
    Posted 8 years ago #

    hi mutube, I will try this.

    Looks like it should work.

    Thanks.

  7. ssdesign
    Member
    Posted 8 years ago #

    ok,
    Now in my existing code, I am doing this:

    To add default options:
    add_option('wphr_qualification', __('your qualification', 'wphr'));

    To update options:
    update_option('wphr_qualification', $_POST['wphr_qualification']);

    To get options:
    $wphr_qualification= stripslashes(get_option('wphr_qualification'));

    Now in the case of dynamic content as discussed above, how should I re-write my code to utilise values from:

    $options['qualifications']=$qualifications;

    Any suggestion?

  8. mutube
    Member
    Posted 8 years ago #

    $x=1;
    while($_POST{'wphr_qualification' . $x}!='') { $qualifications[$x]=$_POST{'wphr_qualification'.$x};
    }

    update_option('wphr_qualification', $qualifications);

    Will do just fine. When you retrieve with $qualifications=get_option('wphr_qualification'); they will be available like:

    $qualifications["1"] for the first Qual.

    You do know by the way that you can store more than one option under a heading using arrays, e.g.

    $options['one']="1";
    $options['two']="1";
    $options['three']="1";

    update_option('wphr_options', $options);

    Just in case you're overcomplicating things for yourself. That's where the $options['qualifications']=$qualifications; came into it.

    Good luck,

  9. ssdesign
    Member
    Posted 8 years ago #

    Excellent, this really worked.

    Thanks and I will add credits to you when I finish this piece of work.

  10. mutube
    Member
    Posted 8 years ago #

    No problem & thanks :) Post back if you need any more help.

    Good luck with the site.

  11. ssdesign
    Member
    Posted 8 years ago #

    mutube,

    Had one more question, if u want, I can post a seperate topic.

    Can we add some code in the plugin so that when you activate it, automatically a PAGE is created in WordPress?

  12. mutube
    Member
    Posted 8 years ago #

    Sorry nearly missed this - you're wanting a new post to be generated in the blog when the plugin is activated?

    Is this a blank page or to have something in it?

    To be honest you're possibly asking for trouble (multiple pages on stop/restart/etc.) but it's certainly do-able!

    Let me know what you're trying to achieve and will give it a look.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.