• Manish

    (@financetrain)


    I have defined an additional field ‘client’ for user profiles.

    I am calling this value in a page using <?php set_client(); ?>

    In the functions.php I have the following function

    //first a global variable is set to which I am assigning the client value
    $client = the_author_meta(‘client’);

    function set_client(){
    global $client;
    echo $client;
    }

    The above function echos empty. It doesn’t return the required value

    If I use the same function without using global variable, it works fine, as shown below:

    function set_client(){
    $client = the_author_meta(‘client’);
    echo $client;
    }

    This works fine.

    For my program, it is important to set the value in a global variable as I am going to be using it in multiple places in my page. Can someone please tell me what exactly I am doing wrong.

    Please help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Since you are creating the variable in functions.php, I suspect it is inside a function. You need to declare it as a global there, or else it is local to the function.

    //first a global variable is set to which I am assigning the client value
    global $client;
    $client = the_author_meta('client');
    Thread Starter Manish

    (@financetrain)

    This is exactly how I have don, but when I echo the $client value, it displays empty.

    On the other hand, it works fine, if it is just a local variable.

    Thread Starter Manish

    (@financetrain)

    I am sorry.

    Actually to define a variable with a global scope, I don’t think we have to declare it global. Just the following is sufficient:

    $client = the_author_meta(‘client’);

    This will automatically have global scope.

    Then when you use it within a function, you declare it as global as follows:

    function set_client(){
    global $client;
    echo $client;
    }

    My problem is that in the global scope, the function the_author_meta(‘client’) does not return any value.

    the_author_meta() will echo the value out
    get_the_author_meta() will return the value to be processed.

    So you need to switch to using the get_ version.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘using the_author_meta in a global variable’ is closed to new replies.