将侧面wordpress小部件类中的变量和实例传递给新的自定义函数

时间:2019-06-06 10:03:24

标签: php wordpress

我想在侧面wordpress小部件类中创建自定义ajax函数,但无法将小部件方法中的实例和一些变量传递给新的自定义ajax方法...

   class Emailer_Widget extends WP_Widget
      {

        public function __construct()
      {
      $options = array(
      'description' => __('Emailer Widget','onepage'),
      'classname'   => 'emailer-widget',
      );
      parent::__construct(
      'op_emailer_widget', //Base ID
     __('OP Emailer Widget','onopage'), //Name
      $options
  );
    $this->adder();
   }
   public function adder() {
    add_action('wp_ajax_mailerformValidation', array($this,       'emailer_form_sender')); // Call when user logged in
           add_action('wp_ajax_nopriv_mailerformValidation', array($this,   'emailer_form_sender')); // Call when user in not logged in
       add_filter( 'some_event_action',array($this, 'emailer_form_sender') );
        }
     public function widget($args, $instance) {
    //some form here
     }
   public function form($instance) {
      $title                 = apply_filters('widget_title',  empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base );
      $message               = ! empty( $instance['message'] ) ? $instance['message'] : '';
      $to_email              = ! empty( $instance['to_email'] ) ? $instance['to_email'] : '';
      $subject               = ! empty( $instance['subject'] ) ? $instance['subject'] : '';
?>
<!-- Widget Title -->
<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'onepage' ); ?></label>
  <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>

<!-- Text -->
<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'message' )); ?>"><?php _e( 'Input Text:', 'onepage' ) ?></label>
  <textarea id="<?php echo esc_attr( $this->get_field_id( 'message' )); ?>"  name="<?php echo esc_attr( $this->get_field_name( 'message' )); ?>" style="min-height: 150px;"><?php echo esc_attr( $message ); ?></textarea>
</p>

<!-- email subject -->
<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'subject' ) ); ?>"><?php _e( 'Email Subject:', 'onepage' ); ?></label>
  <input id="<?php echo esc_attr( $this->get_field_id( 'subject' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'subject' ) ); ?>" type="text" value="<?php echo esc_attr( $subject ); ?>" />
</p>

<!-- email destination -->
<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'to_email' ) ); ?>"><?php _e( 'Email Destination:', 'onepage' ); ?></label>
  <input id="<?php echo esc_attr( $this->get_field_id( 'to_email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'to_email' ) ); ?>" type="text" value="<?php echo esc_attr( $to_email ); ?>" />
</p>



    }
    public function update($new_instance, $old_instance ){
   }
    public function emailer_form_validation() {
     // I wanna pass the instance values to here and the form values to     here 
     }

唯一的问题是emailer_form_validation()方法无法访问其他方法的实例和变量...

1 个答案:

答案 0 :(得分:0)

尝试通过引用传递类:

public function adder() {
    add_action('wp_ajax_mailerformValidation', array(&$this,       'emailer_form_sender')); // Call when user logged in
    add_action('wp_ajax_nopriv_mailerformValidation', array(&$this,   'emailer_form_sender')); // Call when user in not logged in
    add_filter( 'some_event_action',array(&$this, 'emailer_form_sender') );
}

所以不是$this而是&$this more info

还请记住传递您需要将其分配给类的变量。因此,如果要从构造函数访问$options。您还应该将它们设置为类,因此在表格

的顶部
public function form( $instance ) {
    $title    = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
    $message  = ! empty( $instance['message'] ) ? $instance['message'] : '';
    $to_email = ! empty( $instance['to_email'] ) ? $instance['to_email'] : '';
    $subject  = ! empty( $instance['subject'] ) ? $instance['subject'] : '';

    $this->title = $title;     
    $this->message = $message;   
    $this->to_email = $to_email;  
    $this->subject = $subject;

    // the rest of the form
}

现在,您可以使用emailer_form_validation()$this->message方法访问变量。 要查看类中所有设置的变量,只需var_dump($this)并检查输出。