WordPress.org

Ready to get started?Download WordPress

Forums

using the_author_meta in a global variable (5 posts)

  1. Manish
    Member
    Posted 3 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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');
  3. Manish
    Member
    Posted 3 years ago #

    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.

  4. Manish
    Member
    Posted 3 years ago #

    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.

  5. 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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.