Insert the user name anywhere on the site

You have a website with subscribers and you want to give a personalized touch to your visitors when they register. Well, we are going to embed a php code to give a pro style to your page.

What we are going to achieve with this is to display the name of the user who registers or logs in and display it as a welcome message at the top of some page or in the widget area of your theme.

This would be as follows:

Hello, Antonio. How are you?

The code to include is:

function fransoler_top_usuario( $atts ) {
	global $current_user, $user_login;
      	wp_get_current_user();
	add_filter('widget_text', 'apply_shortcodes');
	if ($user_login)
		return 'YOUR HOME TEXT' . $current_user->user_login . 'YOUR FINAL TEXT';
	else
		return '<a href="' . wp_login_url() . ' ">OPTIONAL TEXT FOR WHEN YOU ARE NOT LOGGED IN</a>';
}
add_shortcode( 'top_usuario', 'fransoler_top_usuario' );

How do I insert it?

The easiest way is to use Pluginception or Code Snippets, learn how to create your own plugin without the need to enter the administration panel (CPanel or Plesk).

For example with Pluginception it would be inserted like this:

<?php
/*
Plugin Name: Mis Funciones
Plugin URI: https://tudominio.es/
Description: Plugin to run custom functions.
Version: 1.0
Author: Fran Soler
Author URI: https://tudominio.es
License: GPLv2
*/

function fransoler_top_usuario( $atts ) {
	global $current_user, $user_login;
      	wp_get_current_user();
	add_filter('widget_text', 'apply_shortcodes');
	if ($user_login)
		return 'YOUR HOME TEXT' . $current_user->user_login . 'YOUR FINAL TEXT';
	else
		return '<a href="' . wp_login_url() . ' ">OPTIONAL TEXT FOR WHEN YOU ARE NOT LOGGED IN</a>';
}
add_shortcode( 'top_usuario', 'fransoler_top_usuario' );

Once this is done, just apply it using the shortcode [top_usuario] and you will be able to put it on any page or widget, compatible with Elementor.

Rate this post
Suscribe
Notify
guest
0 Comments
Inline Feedbacks
View all comments