当您在类中时,需要传递对WordPress的引用,以便WordPress调用类中的函数。请参见包含$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();
也就是说,这里不需要使用动作。只需执行以下操作就会更简单:
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();