Where is the least memory consuming place to put a variable store for get_currentuserinfo()

Currently, I have a code block that checks to see if the user is logged in. If the user is logged in, it says "Welcome _". In the blank spot, there is a variable that reflects an if statement that checks to see if the logged in user has a first name stored in their account, then saves it. If the user doesn't then it, then it saves their user name.

I have the following in my header.php file, but Im wondering if its unnecessary to continually running this script in every page.

global $current_user;
get_currentuserinfo();
$atriangleUsername = $current_user->user_login;
$atriangleFirst =  $current_user->user_firstname;
$atriangleOutputName = ($atriangleFirst) ? $atriangleFirst : $atriangleUsername;
$urlLogin = site_url('/login/', 'http');
echo '<div id="topSpecialNav" class="text-right row right special-top-right-nav">'; 
echo '<div class="d-inline" style="font-size: 11px;">'; 
if ( is_user_logged_in() ) {
        echo 'Welcome, '. $atriangleOutputName .'!';
        } else {
            echo '<a href="'. $urlLogin .'">Login</a>';
        }?>
echo '</div>';

Where is the best place these blocks, so I am making the least number of calls to my database, but still staying secure?

Solutions

You should set your user variables (or simply just call them) once you're in your is_user_logged_in() block... There shouldn't be any additional strain, and the way you provided is the best way to pull user details from Wordpress.. These properties are set directly from the table columns in the users table and loaded automatically by Wordpress.

Similar questions

MySQL keeps running out of memory with Wordpress, how much memory do I need?
I have been experiencing MySQL crashing recently and really need to figure out what I need to do to get this to stop. I have a 2GB Digital Ocean server running the following: I also have 2GB of swap. The last time MySQL crashed this was in my error log http://laravel.io/bin/E304E The important part seems (to me) to be this I am getting about 2000 p...
PHP Memory Limit on creation of CSV from db - How to reduce memory that PHP uses?
Am running a db query here, and using mysqli with MYSQLI_USE_RESULT in order to execute the query faster. But there is php manipulation of data needed here, so it's not as simple as just writing to a CSV file on each row returned. I know that the database query is finishing (even though it is taking a long time), but the db query does finish, so th...
Consuming an external API in WordPress?
I have a client who has a WordPress website, and he want to extend it to add new functionalities. These functionalities are included in a website I have already done (a kind of search engine over a database, with some filters), and which is under new enhancements. What I'm planning to do is to create a json api of my website (with all search funtio...
Consuming an external API to publish/post to wordpress
Is there a solution or a way to consume an external web service API (could be JSON or SOAP) to be able populate/update a wordpress blog ? For example using wikipedia api to pull content that wikipedia.com has on the topic the post is about. I tried searching this topic alot online and all i found was how to create a API out of already exisitng post...
API
Is WP-Cron / wp_schedule_event suitable for time-consuming actions?
I want to schedule an action to be run daily. This action can take up to a few seconds. Question is: is WP-Cron the suitable mechanism (compared to, say, classic Unix cron) for such action? The reason why I'm asking is that, as far as I understand, all WordPress hooks are triggered by HTTP requests, probably coming from regular visitors or administ...
.htaccess and proxy settings for routing a decoupled REST API consuming theme
Developing a decoupled front-end consuming the REST API... I'm using create-react-app so can enter a proxy field corresponding to WP in my package.json and write my calls like fetch('/wp-json/v2/pages/...') if I run yarn start from the front-end directory... I'd love to actually have this theme in my wp-content/themes/my-theme directory though I'm ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.