How can I get $id variable in widget's form function?

How can I get $id variable in widget's form function?According to widget's structure I see that widget functi

How can I get $id variable in widget's form function? According to widget's structure I see that widget function have $args as a parameter, which will be extracted in function's body. In my case i want to use $id variable (from extracted $args) which contains current sidebar's id. How can I make 'visible' $id variable in form function? Any ideas?

class Ex_Widget_Example extends WP_Widget {
    //constructor goes here
    function widget($args, $instance) {extract($args);}
    function update($new_instance, $old_instance ) { }
    function form( $instance ) {}
}

For example we have sidebar with id 'my_sidebar'. I put this widget in it. For example my widget function, which we use to output something on page, looks like this:

function widget($args, $instance) {
         extract($args);
         echo $id; //or just echo $args['id'];
}

So on my page, in my 'my_sidebar' sidebar I will see a text 'my_sidebar'. That's great. But how can I get this $id or $args['id'] in widget's form function. In class example I see, that only widget function takes $args as argument. I'm not an expert in OOP pragramming, I tried to create a static variable (unsuccessfully), which would keep id value:

class Ex_Widget_Example extends WP_Widget {
    public static $widget_id;
    //constructor goes here
    function widget($args, $instance) {
             extract($args);
             self::$widget_id = $id;
             echo self::$widget_id //here outputs sidebar's id perfectly on my page
    }
    function update($new_instance, $old_instance ) { }
    function form( $instance ) {
             echo self::$widget_id //here outputs nothing on my widget's form
    }
}

Maybe wordpress call form function first, before widget function, that's why I can't see anything on my form ....anyway, any ideas how to solve this problem?

How can I get $id variable in widget's form function? According to widget's structure I see that widget function have $args as a parameter, which will be extracted in function's body. In my case i want to use $id variable (from extracted $args) which contains current sidebar's id. How can I make 'visible' $id variable in form function? Any ideas?

class Ex_Widget_Example extends WP_Widget {
    //constructor goes here
    function widget($args, $instance) {extract($args);}
    function update($new_instance, $old_instance ) { }
    function form( $instance ) {}
}

For example we have sidebar with id 'my_sidebar'. I put this widget in it. For example my widget function, which we use to output something on page, looks like this:

function widget($args, $instance) {
         extract($args);
         echo $id; //or just echo $args['id'];
}

So on my page, in my 'my_sidebar' sidebar I will see a text 'my_sidebar'. That's great. But how can I get this $id or $args['id'] in widget's form function. In class example I see, that only widget function takes $args as argument. I'm not an expert in OOP pragramming, I tried to create a static variable (unsuccessfully), which would keep id value:

class Ex_Widget_Example extends WP_Widget {
    public static $widget_id;
    //constructor goes here
    function widget($args, $instance) {
             extract($args);
             self::$widget_id = $id;
             echo self::$widget_id //here outputs sidebar's id perfectly on my page
    }
    function update($new_instance, $old_instance ) { }
    function form( $instance ) {
             echo self::$widget_id //here outputs nothing on my widget's form
    }
}

Maybe wordpress call form function first, before widget function, that's why I can't see anything on my form ....anyway, any ideas how to solve this problem?

Share Improve this question edited Mar 14, 2012 at 18:36 user13250 asked Feb 20, 2012 at 20:12 user13250user13250 852 silver badges7 bronze badges 3
  • Just to make that shure: You need the sidebar ID/Name - where the Widget was placed in - inside your widget? – kaiser Commented Feb 20, 2012 at 22:53
  • Would you mind answering the question I asked ↑ here? Also: Please edit & improve your Q. So far there's a lot of confusion. :/ – kaiser Commented Mar 10, 2012 at 11:11
  • sorry, I'm a new guy here. Yes! I put this widget for example in 'my_sidebar'. In this widget's widget function I can just write something like: echo '$id'; and in my front-end I will see sidebar's id - 'my_sidebar'. but I need to use this $id variable in widget's form function, because I want to display in select box which widgets already have this 'my_sidebar'. – user13250 Commented Mar 14, 2012 at 17:19
Add a comment  | 

3 Answers 3

Reset to default 1

Do you search for $args['widget_id']? Just save it in a variable you can access in form() later.

Look at wp-includes/default-widgets.php to see how widgets work. Not the best code, but it should give you some hints.

As a follow up on @toscho answer, you maybe could get the current sidebar (that holds the widget) via the DB options table:

$current_sidebar = array_search( 
    "{$this->id_base}-{$this->number}", 
    get_option( 'sidebars_widgets' ), 
    true 
);

Not tested

The following worked for me inside the function for the form

public function form( $instance ) {
    $my_widget = $this;
    $widget_identifier = "{$this->id_base}-{$this->number}";
}

Edit: the following also works

public function form( $instance ) {
    $widget_identifier = $this->id;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933262a4601863.html

相关推荐

  • How can I get $id variable in widget's form function?

    How can I get $id variable in widget's form function?According to widget's structure I see that widget functi

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信