How to display a value stored in a function in functions.php in WP template

Hi

There is a value in a PHP variable that I am using in a function in functions.php in a custom PHP theme

Now I wish to display this value- either on screen or in console or log(just to check its value)

How do I do this? Since the value is used in functions.php I cant simply use javascript to display it in a web page.

Yours sincerely,
Arvind.

  • Jorgen Juel
    • Site Builder, Child of Zeus

    Hi @Arvind
    I'm not pro on PHP myself yet, but I think this approach would do the task;

    First create a variable in your functions.php, outside your function scope.
    Then reference the variable inside your function, using global variable.
    Then save your value within the function to the variable.
    Now the value is stored in the variable and can be sent to the user in any way you wish.
    For instance, you can create it as a shortcode and use the shortcode somewere else to display it.

    Here is my code example:

    <?php
    $variable;
    
    function this
    {
        global $variable;
    
        $variable = yourvalue
    } 
    
    //create shortcode
    function my_shortcode_func( $atts ){
     global $variable;
     return $variable;
    }
    add_shortcode( 'my_shortcode', 'my_shortcode_func' );
    ?>

    I think this should work.

    edit: I suppose the other solution was alot simpler :slight_smile:

    --
    Jørgen Juel

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.