I want to usewp_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' );
Like I said in the comments,astra_entry_content_after
is too late for performing a redirect. So, you could use bothtemplate_redirect
hook andastra_entry_content_after
hook separately not "inside each other". Use the following snippets in thefunctions.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!
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.