diff --git a/functions.php b/functions.php index 7119b36..5a615b0 100644 --- a/functions.php +++ b/functions.php @@ -203,13 +203,23 @@ function quiztech_handle_credit_purchase_submission() { return; } + // Get the Manage Credits page URL reliably + $manage_credits_page = get_page_by_path('manage-credits'); // Assumes page slug is 'manage-credits' + if (!$manage_credits_page) { + // Fallback or error if page doesn't exist + wp_die(esc_html__('Manage Credits page not found. Cannot process purchase.', 'quiztech')); + exit; + } + $manage_credits_url = get_permalink($manage_credits_page->ID); + + // Verify nonce if ( ! isset( $_POST['quiztech_buy_credits_nonce'] ) || ! wp_verify_nonce( $_POST['quiztech_buy_credits_nonce'], 'quiztech_buy_credits_action' ) ) { // Nonce is invalid, redirect back with error $redirect_url = add_query_arg( array( 'purchase_status' => 'error', 'message' => urlencode( __( 'Security check failed. Please try again.', 'quiztech' ) ) - ), get_permalink() ); // Assumes this runs when get_permalink() refers to the Manage Credits page + ), $manage_credits_url ); // Use the stored URL wp_safe_redirect( $redirect_url ); exit; } @@ -219,7 +229,7 @@ function quiztech_handle_credit_purchase_submission() { $redirect_url = add_query_arg( array( 'purchase_status' => 'error', 'message' => urlencode( __( 'You do not have permission to purchase credits.', 'quiztech' ) ) - ), get_permalink() ); + ), $manage_credits_url ); wp_safe_redirect( $redirect_url ); exit; } @@ -235,7 +245,7 @@ function quiztech_handle_credit_purchase_submission() { $redirect_url = add_query_arg( array( 'purchase_status' => 'error', 'message' => urlencode( __( 'Invalid credit package selected.', 'quiztech' ) ) - ), get_permalink() ); + ), $manage_credits_url ); wp_safe_redirect( $redirect_url ); exit; } @@ -251,7 +261,7 @@ function quiztech_handle_credit_purchase_submission() { $redirect_url = add_query_arg( array( 'purchase_status' => 'error', 'message' => urlencode( $result->get_error_message() ) - ), get_permalink() ); + ), $manage_credits_url ); wp_safe_redirect( $redirect_url ); exit; }