• Hi

    I’m trying to save the authenticated user in a header, but this is not done for all requests; it will be only done for the rendered html pages, but not for the images, css, and other type of media.

    I have tried several filters and functions, but the only one that is partially working is this one:

    
    add_action('init', 'add_auth_user_header');
    function add_auth_user_header() {
      $auth_user = wp_get_current_user();
      $auth_user_login = 'Anonymous';
      if ($auth_user->ID != 0) {
        $auth_user_login = $auth_user->user_login;
      }
      header( 'X-WPUser: ' . $auth_user_login);
    }
    

    First I put that action into a “Code Snippet” (WordPress plugin); the header was as I said partially set. So, I tried to directly put the code into the functions.php file of the WordPress theme, but the results were the same. Please also note that I completely disabled all the cache mechanism; I heard that catching may be an issue.

    I now that there are also places for actions and filters for setting headers: send_headers and wp_headers, but none of them worked for me. The header wasn’t set.

    Does anybody have an idea?

    Thanks in advanced

    Best regards
    Josef

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator t-p

    (@t-p)

    You need to identify exactly which theme you are using and then post in that theme’s dedicated forum via its page in the Theme Repository.

    Thread Starter jmeile

    (@jmeile)

    I’m afraid this is more a core issue. I decided to put the code inside:

    wp-includes/functions.php

    and the problem still happens. It is happening in the backoffice, so, there is no theme involved at all.

    I will try with the default theme and see if this still happens

    Moderator bcworkz

    (@bcworkz)

    Please, never alter core code like wp-includes/functions.php. All custom code should reside in a child theme or your own plugin.

    Media requests for the most part do not go through WP, so you would need to add header data through other means, like .htaccess.

    Thread Starter jmeile

    (@jmeile)

    Dear bcworkz

    Yes, I know that I shouldn’t modify the core. I just did it in order to proof that the Theme doesn’t have anything to do with the failure while setting the header through the WordPress hooks. This seems to be a problem in the core of WordPress.

    The problem with the .htaccess approach is that I can’t access the current logged user in WordPress from there. That’s why I need to do it somewhere inside WordPress where I can call the function: wp_get_current_user.

    My problem is that the header I’m setting will work only for php files, but not for images, js, and css files. I even disabled the cache for the Backoffice. What happens in the Frontoffice is less important to me.

    Best regards
    Josef

    • This reply was modified 6 years, 5 months ago by jmeile.
    Moderator bcworkz

    (@bcworkz)

    To get WP to handle media requests, the requested media URL must not lead to a valid file. There does need to be something in the URL that tells WP what file to send. You’d need a custom handler routine to decipher the request, then respond with appropriate headers, followed by the data stream. WP is essentially mimicking the normal server response that occurs with a media file request.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Setting a custom header doesn’t work for all requests’ is closed to new replies.