最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

headers - Redirect not work

programmeradmin2浏览0评论

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
  • Take a look at the following answer wordpress.stackexchange/questions/48085/…. It explains how to call add_action with class methods. – czerspalace Commented Mar 22, 2019 at 23:27
  • I change to "add_action('do_redirect',array( $this, 'redirect' ));" and works but now i still headers error – Jaron Commented Mar 22, 2019 at 23:59
  • Maybe you take a look here. Btw error with headers? what kind of error, what does it say. Please don't expect us to guess what errors you get, show them by adding them to your question (please update also the code now with your correction) – Charles Commented Mar 23, 2019 at 0:34
  • At what point are you initiating the users class, or calling new users()? – czerspalace Commented Mar 23, 2019 at 0:53
  • I have warning: Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\wp-includes\class.wp-styles.php:242) in C:\Program Files\VertrigoServ\www\wp-includes\pluggable.php on line 1251 – Jaron Commented Mar 23, 2019 at 9:58
 |  Show 3 more comments

1 Answer 1

Reset to default 0

When 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();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论