0

Im creating Wordpress widget, where I have to add parts of code dynamically by JQUERY.ajax() a JQUERY.append() to the widget form. I need it, to be able to save it by function update(). I have PHP functions for each field of form, but in wp_widget I have to get the names and IDs by its function.

If I make function to get name outside the wp_widget child:

function gentitle($title,$num=''){
            $widget=new WP_Widget();
            return
            sprintf('%1$s[%2$s]',
                $widget->get_field_name($title),
                $num
            );
        }

it returns errors and if I make the forms child classes of my widget, it fails too.

I need to be able to get the names into form functions and to be able to get them with AJAX and put it inside widget form...

In class it works, but I cant use it for the functions that generates the form (cos $v=new testwidget();$v->genname; returns error).

class awidget_test extends WP_Widget {
    function awidget_test() {
        $widget_ops=array('classname'=>'atestwidget','description'=>'Just testing something');
        $control_ops=array('width'=>300,'height'=>350,'id_base'=>'atest-widget');
        $this->WP_Widget('atestwidget-widget','Actually testing',$widget_ops,$control_ops);
    }
    function widget($args,$instance){
        extract($args);
    }
    function update($new_instance,$instance) {
        return $instance;
    }
        function gentitle($title,$num=''){
            return
            sprintf('%1$s[%2$s]',
                $this->get_field_name($title),
                $num
            );
        }
    function form($instance) {

            echo $this->gentitle('blabla');

    }
}

Any ideas?

1 Answer 1

0

I'm not sure I fully understand what you're trying to achieve, but I'd suggested to attempt to construct the field's ID and name from data you've got available from the source code of the generated widget. If you check out the code, you'll find that by every widget, there's a hidden form field called id_base and widget-id - this should be enough to figure out the correct field name, right?

Developer tools screenshot of text widget's source code

1
  • Well I think it should be easier to achieve now, I need just get these informations and send it with other ajax datas and make name like this: widget-{.id_base}[{.multi_number}][${field_name}]
    – sjiamnocna
    Commented Feb 17, 2015 at 15:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.