WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] generate input fields in widget config (5 posts)

  1. jerodev
    Member
    Posted 1 year ago #

    Hi,

    I was wondering if it is possible to generate input fields in the widget config field using javascript. And if so, how can I get the data from them?

    I want the admin to be able to select a value form a combobox and put a button next to it to create another combobox to select another value. But how do I name this new combobox and how do I get the data in the widget class?

    Thank you,
    Jerodev

  2. bcworkz
    Member
    Posted 1 year ago #

    I haven't done this, but I think it's possible. You may be better off defining the second box from the beginning, you can hide it with css if you want. It's only value could be "Pick from 1st combo".

    Based on the first combo pick, javascript can populate the correct values for the second, using ajax if need be. When the form is submitted, the values are passed to the widget's update function as any other field. As long as update() knows what field name to expect, whether preassigned or assigned by javascript, it all should work out.

  3. jerodev
    Member
    Posted 1 year ago #

    The problem is that I would like to be able to create an unknown number of comboboxes.
    I don't know the number in advance.

  4. bcworkz
    Member
    Posted 1 year ago #

    There must be some upper limit? Assume worst case, anything with default value was not used.

    Or if you want to generate the entire combo box on the fly, give them sequential names like info01, info02, info03 etc. Then use isset() in the widget's update() to determine whether a particular name is used.

    Since the values are stored in an object, you can build name strings on the fly, then use them to access each value:

    for ($i = 1; $i <= 10; $i++){
    $name = 'info0' . strval($i);
    if(isset($instance[$name])){
    //do stuff
    }}
  5. jerodev
    Member
    Posted 1 year ago #

    Thanks for the help.
    I have solved the problem by generating the fields with javascript and storing the values of all combo boxes in a hidden input with a correct wordpress name. I set this value with every onchange of a combo box.

Topic Closed

This topic has been closed to new replies.

About this Topic