I have class:
class users{
public function __construct(){
add_action('do_redirect', 'redirect');
}
public function redirect(){
$url = get_site_url(null, '/welcome', 'https');
wp_redirect( $url );
exit;
}
public function some_function(){
do_action('do_redirect');
}
}
I get error :
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'redirect' not found or invalid function name.
If I use only wp_redirect()
i get error for headers.
How i must change this, because i fell I do something wrong?
I have class:
class users{
public function __construct(){
add_action('do_redirect', 'redirect');
}
public function redirect(){
$url = get_site_url(null, '/welcome', 'https');
wp_redirect( $url );
exit;
}
public function some_function(){
do_action('do_redirect');
}
}
I get error :
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'redirect' not found or invalid function name.
If I use only wp_redirect()
i get error for headers.
How i must change this, because i fell I do something wrong?
Share Improve this question edited Jul 21, 2019 at 23:40 Jaron asked Mar 22, 2019 at 23:00 JaronJaron 458 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 0When you're inside a class, you need to pass a reference to WordPress for WordPress to call the function within the class. See the line that contains $this
.
class My_Users {
public function __construct() {
add_action( 'do_redirect', array( $this, 'redirect' ) );
}
public function redirect() {
$url = get_site_url( null, '/welcome', 'https' );
wp_safe_redirect( $url );
exit;
}
public function some_function() {
do_action( 'do_redirect' );
}
}
$custom_users = new My_Users();
$custom_users->some_function();
That being said, you do not need to use an action here. It would be simpler to just do the following:
class My_Users {
public function redirect() {
$url = get_site_url( null, '/welcome', 'https' );
wp_safe_redirect( $url );
exit;
}
}
$custom_users = new My_Users();
$custom_users->redirect();
users
class, or callingnew users()
? – czerspalace Commented Mar 23, 2019 at 0:53