PHP Multisite WordPress, need 2 functions to work without breaking website

I’m completely new and not a programmer, I’m just experimenting with the functions.php file (in a child theme) to see if I can get a function to work.

My goal: I am using a multisite wordpress setup for a website on models. When admin creates a profile for each model, their name is used on SITE 1 for their unique url AND SITE 2 to say ‘welcome model name’

Read More

SITE 1: public site for everyone to see
SITE 2: private models only site to view and update personal details

On SITE 1, I have a complete list of all the models on a page for the public to browse through. Each model has their own unique profile URL that contains their first and last name. E.g. http://www.domain.com/models/samantha-rebecca

On SITE 2, when the models log in to the site, I need the homepage to show 2 name uses. One is their display name using a shortcode so I can say ‘Welcome Samantha Rebecca’ (which I have got working using this code).

/* username in page content using [current_user] shortcode */

function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->display_name;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');

The other is for a button on the same private page they can press to go straight to their ‘public’ profile page. This is what I need help with please.

In an ideal world, I would like to take their first and last name (all profiles are admin created only) and put a dash in between so I can use http://www.domain.com/models/[url_user] for which I did try and use the following:

/* username in url using [url_user] shortcode */

function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_firstname . '-' . $current_user->user_lastname;
$output = ob_get_clean();
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func');

But I can’t seem to get them to both work at the same time in the functions file without an error coming up such as this

Fatal error: Cannot redeclare custom_shortcode_func() (previously declared in /home/domain/public_html/wp-content/themes/child-theme/functions.php:47) in /home/domain/public_html/wp-content/themes/child-theme/functions.php on line 65

Please can someone help me get this to work as one efficient function. Thank you.

Related posts

Leave a Reply

1 comment

  1. i got your issue is in both short code you have used same function name please change your second function name like below code.

    /* username in url using [url_user] shortcode */
    
    
    
     function custom_shortcode_func_url_username() {
        ob_start();
        $current_user = wp_get_current_user();
       $output = $current_user->user_firstname . '-' . $current_user->user_lastname;
    
        return $output;
        }
        add_shortcode('url_user', 'custom_shortcode_func_url_username');
    

    change your second function name like above code your problem will solved. you are getting error because for both shortcode your function name is same. function name should be unique. same name multiple time is not allowed.
    more about used define function name rules. More about used define functions