繁体   English   中英

如何在另一个 function 中调用 wp_redirect?

[英]How do I call wp_redirect within another function?

我想在这个 function 中使用 wp_redirect,但我目前遇到错误:无法修改 Header 信息。 感谢您提供任何帮助。

这是我的代码:

function add_listing_dashboard_content() {

    global $post;

    global $page_id_dashboard; 
    global $page_id_user_profile;
    global $page_id_submit_listing;  
    global $page_id_success;     
    
    $page_id_dashboard      = get_field( 'dashboard_page', 'option');
    $page_id_user_profile   = get_field( 'user_profile_page', 'option');
    $page_id_submit_listing = get_field( 'submit_listing_page', 'option');
    $page_id_success        = get_field( 'success_page', 'option');
    
    if ( is_page( $page_id_dashboard ) && is_user_logged_in() ) :   
        include( plugin_dir_path( __FILE__ ) . '../templates/partials/dashboard-dashboard.php');        
    elseif ( is_page( $page_id_success ) && is_user_logged_in() ) : 
        include( plugin_dir_path( __FILE__ ) . '../templates/partials/dashboard-dashboard.php');                
    elseif ( is_page( $page_id_user_profile ) && is_user_logged_in() ) :    
        include( plugin_dir_path( __FILE__ ) . '../templates/partials/dashboard-profile.php');      
    elseif ( is_page( $page_id_submit_listing ) && is_user_logged_in() ) :  
        include( plugin_dir_path( __FILE__ ) . '../templates/partials/dashboard-submission.php');           
    elseif ( is_page( $page_id_dashboard ) || is_page( $page_id_user_profile ) || is_page( $page_id_submit_listing ) && !is_user_logged_in() ) :        
        include( plugin_dir_path( __FILE__ ) . '../templates/partials/dashboard-loggedout.php');
    elseif ( is_page( $page_id_success ) && !is_user_logged_in() ) :
        wp_redirect( get_permalink($page_id_dashboard) );       
        exit;
    endif;  
    
}
add_action( 'astra_entry_content_after', 'add_listing_dashboard_content' );

就像我在评论中所说的那样, astra_entry_content_after为时已晚,无法执行重定向。 因此,您可以分别使用template_redirect钩子和astra_entry_content_after钩子,而不是“在彼此内部”。 functions.php文件中使用以下代码段:

add_action("template_redirect", "redirecting_users_on_page_id_success");

function redirecting_users_on_page_id_success()
{
    global $page_id_dashboard;
    global $page_id_success;

    $page_id_dashboard = get_field('dashboard_page', 'option');
    $page_id_success   = get_field('success_page', 'option');

    if ((is_page($page_id_success) && !is_user_logged_in())) 
    {
        wp_safe_redirect(get_permalink($page_id_dashboard));
        exit;
    }
};
add_action('astra_entry_content_after', 'add_listing_dashboard_content');

function add_listing_dashboard_content()
{

    global $page_id_dashboard;
    global $page_id_user_profile;
    global $page_id_submit_listing;
    global $page_id_success;

    $page_id_dashboard      = get_field('dashboard_page', 'option');
    $page_id_user_profile   = get_field('user_profile_page', 'option');
    $page_id_submit_listing = get_field('submit_listing_page', 'option');
    $page_id_success        = get_field('success_page', 'option');

    if (is_page($page_id_dashboard) && is_user_logged_in()) :
        include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
    elseif (is_page($page_id_success) && is_user_logged_in()) :
        include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
    elseif (is_page($page_id_user_profile) && is_user_logged_in()) :
        include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-profile.php');
    elseif (is_page($page_id_submit_listing) && is_user_logged_in()) :
        include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-submission.php');
    elseif (is_page($page_id_dashboard) || is_page($page_id_user_profile) || is_page($page_id_submit_listing) && !is_user_logged_in()) :
        include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-loggedout.php');
    endif;
}

让我知道你是否可以让它工作!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM