PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 13, 2022

[FIXED] Wordpress Astra theme how do I use wp_redirect

 January 13, 2022     custom-wordpress-pages, php, redirect, wordpress, wordpress-theming     No comments   

Issue

I want to use wp_redirect inside this function but I am currently getting an error:

Cannot Modify Header Information. Any help is appreciated thank you.

Here is my code:

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' );

Solution

Like I said in the comments, astra_entry_content_after is too late for performing a redirect. So, you could use both template_redirect hook and astra_entry_content_after hook separately not "inside each other". Use the following snippets in the functions.php file:

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;
}

Let me know if you could get it to work!



Answered By - Ruvee
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing