plugins – Can’t use updated variables in handle function
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked
Viewed
3 times
I have some problem with the wp-background-processing that’s when I change the $url
in another file using $this->process_single->url('https://anotherexample.com');
the url won’t update in handle function :/ but when I echo it in dispatch, it shows the updated url
here is my class-example-request.php
file:
<?php
class WP_Example_Request extends WP_Async_Request {
/**
* @var string
*/
protected $action = 'example_request';
protected $url = "https://example.com";
public function url($url){
$this->url = $url;
return $this;
}
public function get_url(){
return $this->url;
}
public function handle() {
$response = wp_remote_get( esc_url_raw( $this->get_url() ) );//here, the url won't update!
}
public function dispatch() {
$url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
$args = $this->get_post_args();
echo $this->get_url(); // this echos the updated url
return wp_remote_post( esc_url_raw( $url ), $args );
}
}
any ideas?
Erfan Paslar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You need to change the method use for the variable $url
-
public – the property or method can be accessed from everywhere. This
is default -
protected – the property or method can be accessed within the class
and by classes derived from that class -
private – the property or method can ONLY be accessed within the
class
In your case your script is not allowed to modify a protected variable.
Change it to public.
lang-php
Leave an answer