Search code examples
phparrayswordpressreturn

I need to return multiple value from a function and print them in my template in Wordpress


I created a function in my function.php to retrieve some variables. To be clear, at the beginning of my project, I put those variables in my template, and they worked fine, but I want to write better code, so I want to rewrite my code, put in the function.php file and then call the function to print the value in my template.

So now my approach is different, here's my example:

function programpagedata() {
    global $post;
    $url = $_SERVER['REQUEST_URI'];
    $path = parse_url($url, PHP_URL_PATH);
    $segments = explode('/', rtrim($path, '/'));
    $campus = $segments[1];

    $prog_parent = get_field('program_parent');
    $prog_code = get_field('program_code');
    $program_parent = $post->post_parent;
    $prog_par_details = get_field('program_code', $program_parent );

    if( $prog_parent == true ) {
        $program = $prog_code;
    } else {
        $program = $prog_par_details;
    }
    return array($program, $campus);
}

And in my template I call the function in this way:

<?php
  programpagedata();
  echo $program;
  echo $campus;
?>

So for example in my template I can call those function in this way:

<div class="form-group">
      <label for="program">
        <input type="hidden" class="form-control" id="program" aria-describedby="programHelp" placeholder="Program" value="<?php echo $program; ?>" name="program">
      </label>
    </div>
    <div class="form-group">
      <label for="campus">
        <input type="hidden" class="form-control" id="campus" aria-describedby="campusHelp" placeholder="Campus" value="<?php echo $campus; ?>" name="campus">
      </label>
    </div>

I'm trying to understand where my mistake is, because, as I said, if I put this PHP snippet in my template, the code works fine with an important difference:

PHP IN THE TEMPLATE

<?php
    global $post;
    $url = $_SERVER['REQUEST_URI'];
    $path = parse_url($url, PHP_URL_PATH);
    $segments = explode('/', rtrim($path, '/'));
    $campus = $segments[1];
    // $program = end($segments);

    $prog_parent = get_field('program_parent');
    $prog_code = get_field('program_code');
    $program_parent = $post->post_parent;
    $prog_par_details = get_field('program_code', $program_parent );

    if( $prog_parent == true ) {
      $program = $prog_code;
    } else {
      $program = $prog_par_details;
    }
?>
<form>
my template form
</form>
?>

I don't return anything. So I guess that I'm struggling with the return keyword on WP. Any thoughts?


Solution

  • I think your problem is that you call the function, but you don't save the return to a variable, then you try to echo variables which don't only exist within the scope of the function.

    When you return something from a function in PHP, you have to store that value inside a variable to use it, and variables created inside a function do not exist on the page the function was ran on.

    Try this:

    //Direct replacement of your template code, doesn't require modification to your function
    
    //store return from function
    $return = programpagedata();
    
    //arrays start at 0, and $program is stored at index 0 of $return
    echo $return[0];
    
    //$campus is stored at index 1 of $return
    echo $return[1];
    

    It would probably be better to index your return with that the value actually is, instead of numbered indexes.

    In your function, change your return to this:

    function programpagedata() {
        global $post;
    
        ...
    
        return array("program" => $program, "campus" => $campus);
    }
    

    Then, you can call it this way:

    //Template code if you modify `return` of function using snippet above 
    
    //store return from function
    $data = programpagedata();
    
    //$program is stored at index 'program' of $data
    echo $data['program'];
    
    //$campus is stored at index 'campus' of $data
    echo $data['campus'];