Help with Custom Shortcode to display user meta data on page

I have a shortcode that adds some extra fields for user meta data and allows me to display that info via a shortcode in pages in multisite. However, my shortcodes get the data based on the "page author" (code below). Sometimes the admin creates/updates pages in the subsites and the page author is changed so the shortcodes show the wrong info.

Would it be a simple process to update my shortcode so it would display the sites usermeta from the admin or admin email for the site?

I don't understand all the code or know how to write it correctly but I have other shortcodes that display the admins info no matter the page author:

$user_info = get_user_by( 'email', get_option('admin_email'));
$user_info = get_site_admin_id();

Here is the shortcode I have the is based on "Page Author" that I need to change so it always displays the sites admin's meta data no matter who authored page. Any suggestions?

add_shortcode('lmn','lmn_shortcode_func');

function lmn_shortcode_func($options){

	$extra_fields = array(
		'first_name' => 'First Name',
		'last_name' => 'Last Name',
		'address_1' => 'Address 1',
		'address_2' => 'Address 2',
		'city' => 'City',
		'state' => 'State',
		'zipcode' => 'Zipcode',
		'phone' => 'Phone'
	);
	$current_field = $options['field'];
	$blog_id = get_current_blog_id();
	$mail = get_blog_option($blog_id, 'admin_email');
	$user_from_email = get_user_by('email', $mail);
	$user_id = $user_from_email->ID;
	$value = get_user_meta($user_id, $current_field, true);
	ob_start();
	?>

	<?php
		$output = ob_get_clean();
		if($value){
			echo $extra_fields[$current_field].': '.$value.'<br />';
		}
		return $output;
}

// here are the shortcodes

add_shortcode( 'lmn_user_info' , 'z_get_current_info' );
function z_get_current_info(){
	$mail = get_the_author_meta('user_email');
	$user_display_name = get_the_author_meta('display_name');
	$user_fname = get_the_author_meta('user_firstname');
	$user_lname = get_the_author_meta('user_lastname');
	$user_phone = get_the_author_meta('phone');
	$user_zipcode = get_the_author_meta('zipcode');
	$user_address = get_the_author_meta('address_1');
	$user_city = get_the_author_meta('city');
	$user_state = get_the_author_meta('state');
	$user_sitename = get_site_url();
    return '<ul class="lmn_user_info lmn_user_info_list"><li>username : ' . $user_display_name . '</li><li> Name : ' . $user_fname . '<span> ' . $user_lname . '</span></li><li> Site : ' . $user_sitename . '</li><li> email : ' . $mail . '</li><li> phone : ' . $user_phone . '</li><li> address : ' . $user_address . '</li><li> city : ' . $user_city . '</li><li> state : ' . $user_state . '</li><li> zipcode : ' . $user_zipcode . '</li></ul>';
}

thanks!

  • Kasia Swiderska

    Hello George,

    If I understand correctly, this is shortcode you want to change so it displays the admin info only?

    add_shortcode( 'lmn_user_info' , 'z_get_current_info' );
    function z_get_current_info(){
    	$mail = get_the_author_meta('user_email');
    	$user_display_name = get_the_author_meta('display_name');
    	$user_fname = get_the_author_meta('user_firstname');
    	$user_lname = get_the_author_meta('user_lastname');
    	$user_phone = get_the_author_meta('phone');
    	$user_zipcode = get_the_author_meta('zipcode');
    	$user_address = get_the_author_meta('address_1');
    	$user_city = get_the_author_meta('city');
    	$user_state = get_the_author_meta('state');
    	$user_sitename = get_site_url();
        return '<ul class="lmn_user_info lmn_user_info_list"><li>username : ' . $user_display_name . '</li><li> Name : ' . $user_fname . '<span> ' . $user_lname . '</span></li><li> Site : ' . $user_sitename . '</li><li> email : ' . $mail . '</li><li> phone : ' . $user_phone . '</li><li> address : ' . $user_address . '</li><li> city : ' . $user_city . '</li><li> state : ' . $user_state . '</li><li> zipcode : ' . $user_zipcode . '</li></ul>';
    }

    so you would need to change like that:

    add_shortcode( 'lmn_user_info' , 'z_get_current_info' );
    function z_get_current_info(){
    
    	$blog_id = get_current_blog_id();
    	$mail = get_blog_option($blog_id, 'admin_email');
    	$user_from_email = get_user_by('email', $mail);
    	$user_id = $user_from_email->ID;
    
    	$user_display_name = get_user_meta($user_id , 'display_name');
    	$user_fname = get_user_meta($user_id , 'user_firstname');
    	$user_lname = get_user_meta($user_id , 'user_lastname');
    	$user_phone = get_user_meta($user_id, 'phone');
    	$user_zipcode = get_user_meta($user_id ,'zipcode');
    	$user_address = get_user_meta($user_id , 'address_1');
    	$user_city = get_user_meta($user_id , 'city');
    	$user_state = get_user_meta($user_id , 'state');
    	$user_sitename = get_site_url();
        return '<ul class="lmn_user_info lmn_user_info_list"><li>username : ' . $user_display_name . '</li><li> Name : ' . $user_fname . '<span> ' . $user_lname . '</span></li><li> Site : ' . $user_sitename . '</li><li> email : ' . $mail . '</li><li> phone : ' . $user_phone . '</li><li> address : ' . $user_address . '</li><li> city : ' . $user_city . '</li><li> state : ' . $user_state . '</li><li> zipcode : ' . $user_zipcode . '</li></ul>';
    }

    Let me know if this works for you.

    Kind regards,
    Kasia

  • Kasia Swiderska

    Hello George,

    Ups! Please try this one:

    add_shortcode( 'lmn_user_info' , 'z_get_current_info' );
    function z_get_current_info(){
    
    	$blog_id = get_current_blog_id();
    	$mail = get_blog_option($blog_id, 'admin_email');
    	$user_from_email = get_user_by('email', $mail);
    	$user_id = $user_from_email->ID;
    
    	$user_display_name = get_user_meta($user_id , 'display_name', true);
    	$user_fname = get_user_meta($user_id , 'user_firstname', true);
    	$user_lname = get_user_meta($user_id , 'user_lastname', true);
    	$user_phone = get_user_meta($user_id, 'phone', true);
    	$user_zipcode = get_user_meta($user_id ,'zipcode', true);
    	$user_address = get_user_meta($user_id , 'address_1', true);
    	$user_city = get_user_meta($user_id , 'city', true);
    	$user_state = get_user_meta($user_id , 'state', true);
    	$user_sitename = get_site_url();
        return '<ul class="lmn_user_info lmn_user_info_list"><li>username : ' . $user_display_name . '</li><li> Name : ' . $user_fname . '<span> ' . $user_lname . '</span></li><li> Site : ' . $user_sitename . '</li><li> email : ' . $mail . '</li><li> phone : ' . $user_phone . '</li><li> address : ' . $user_address . '</li><li> city : ' . $user_city . '</li><li> state : ' . $user_state . '</li><li> zipcode : ' . $user_zipcode . '</li></ul>';
    }

    this should work (I forgot to add one parameter to get_user_meta()).

    Kind regards,
    Kasia

  • George

    Separately I was able to do this for the username or sub-site name:

    function x_show_current_name(){
    	// Get the current admin user, using it's registered email as key
    	$user_info = get_user_by( 'email', get_option('admin_email'));
    
    $user_info = get_site_admin_id();
    
    	// Use the user_info found to distill the info wanted
    	$username = $user_info->user_login;
    	// Return the output to show
    	return '' . $username . '';
    }
    add_shortcode( 'sub_site_name', 'x_show_current_name' );
    ?>

    And this for admins first & last name:

    function z_show_current_name(){
    	// Get the current admin user, using it's registered email as key
    	$user_info = get_user_by( 'email', get_option('admin_email'));
    
    $user_info = get_site_admin_id();
    
    	// Use the user_info found to distill the info wanted
    	$user_fname = $user_info->first_name;
    	$user_lname = $user_info->last_name;
    	$text = 'TESTING: ';
    	// Return the output to show
    	return '<span class="user_fname">' . $user_fname . '</span><span class="user_lname"> ' . $user_lname . '</span>';
    }
    add_shortcode( 'admins_first_last', 'z_show_current_name' );

    So I can use [sub_site_name] and [admins_first_last] to display those 2 missing lines of info.
    Can you update my orig shortcode that lists them all? If not that is ok, you have helped alot and at least this will work :slight_smile:

    thanks!!!

  • Kasia Swiderska

    Hello George,

    Try this one:

    <?php
    add_shortcode( 'lmn_user_info' , 'z_get_current_info' );
    function z_get_current_info(){
    
    	$blog_id = get_current_blog_id();
    	$mail = get_blog_option($blog_id, 'admin_email');
    	$user_from_email = get_user_by('email', $mail);
    	$user_id = $user_from_email->ID;
    
    	$user_display_name = get_user_meta($user_id , 'user_login', true);
    	$user_fname = get_user_meta($user_id , 'first_name', true);
    	$user_lname = get_user_meta($user_id , 'last_name', true);
    	$user_phone = get_user_meta($user_id, 'phone', true);
    	$user_zipcode = get_user_meta($user_id ,'zipcode', true);
    	$user_address = get_user_meta($user_id , 'address_1', true);
    	$user_city = get_user_meta($user_id , 'city', true);
    	$user_state = get_user_meta($user_id , 'state', true);
    	$user_sitename = get_site_url();
        return '<ul class="lmn_user_info lmn_user_info_list"><li>username : ' . $user_display_name . '</li><li> Name : ' . $user_fname . '<span> ' . $user_lname . '</span></li><li> Site : ' . $user_sitename . '</li><li> email : ' . $mail . '</li><li> phone : ' . $user_phone . '</li><li> address : ' . $user_address . '</li><li> city : ' . $user_city . '</li><li> state : ' . $user_state . '</li><li> zipcode : ' . $user_zipcode . '</li></ul>';
    }

    hope this works this time :slight_smile:

    Kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.