How can I use an alternate header when a query var is present

ChatGPT provided a working solution:

function load_custom_header_for_campaign( $template ) {
    // Check if the 'campaign' query var is set
    if (isset($_GET['campaign'])) {
        // Load the header-lp.php file instead of the default header
        add_filter('get_header', function($header) {
            return 'lp';  // This will load header-lp.php
        });
    }
    return $template;
}
add_filter('template_include', 'load_custom_header_for_campaign');

It notes:

The issue you’re encountering is due to the fact that WordPress loads
the header before the query vars are processed and used in conditional
logic. By the time the page.php file executes, WordPress has already
decided which header to load.

To resolve this, you can modify the way the header is included.
Directly modify the template file loading process by using the
get_template_part function to control the output.

Explanation of the Code: template_include filter is triggered when
WordPress is about to determine the template for the page. The
get_header filter: This is used to hook into and override the default
header loading. If the query var campaign exists, it tells WordPress
to load header-lp.php.

CoPilot provided a different method, which did not work. (Theirs returned a 502 Bad Gateway error.):

function my_custom_header_template($template) {
    if (isset($_GET['campaign'])) {
        // Load the alternate header template
        add_action('get_header', function() {
            get_header('lp');
        });
    }
    return $template;
}
add_filter('template_include', 'my_custom_header_template');

CoPilot could not provide modified code to solve the problem.

If anyone is interested, ChatGPT took two attempts to get the right code. This is the first ChatGPT attempt, which did not work:

function load_alternate_header_for_campaign() {
    // Check if the 'campaign' query var is set
    if (isset($_GET['campaign'])) {
        // Set a custom header for the page if 'campaign' query var is present
        add_filter('get_header', function($header) {
            // Return 'lp' header when the campaign query var is present
            return 'lp';
        });
    }
}
add_action('template_redirect', 'load_alternate_header_for_campaign');

After providing the 2nd try code which worked, ChatGPT noted:

Why This Might Work Better: This should work because the
template_include hook gives us a chance to check for query variables
before the template (and the header) is included. The get_header
filter ensures that we are returning header-lp.php instead of the
default header for pages where campaign is set in the URL.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)