4

I'm having these two WordPress functions:

$wpb_set_post_views = function($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
};

add_action( 'wp_head', function ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;
    }
    $wpb_set_post_views($post_id);
});

But the page return Notice: Undefined variable: wpb_set_post_views for the last line.

5
  • 4
    After function ($post_id) you need to add use (wpb_set_post_views). The variable is out of scope. Commented Sep 7, 2015 at 14:48
  • @Andrew Did you mean use ($wpb_set_post_views) ? Commented Sep 7, 2015 at 14:50
  • Yes, my bad, forgot the $. Commented Sep 7, 2015 at 14:54
  • Well do you really need this to be an anonymous function? Commented Sep 7, 2015 at 14:58
  • Have you found the answer to your question yet? Commented Oct 27, 2015 at 13:48

2 Answers 2

10

When dealing with Closures in PHP you need to ensure that any out of scope variable is put into the Closure scope. This is unlike JavaScript where closures have access to variables declared in a PHP scope.

Your anonymous function should be as follows

function() use ($variableNeeded) { }

You will then have access to that variable.

It is important to keep in mind that this is a pass by value scenario, so any changes to that variable will not be reflected outside the closure, so you would need to pass by reference to make changes.

function() use (&$variableNeeded) { }
Sign up to request clarification or add additional context in comments.

2 Comments

There's really no need to pass it in by reference.
In this example, maybe not, but if you need the value change to be reflected outside the closure, you need to pass by reference. Calling closures is the same as calling any other function, so you will pass by value.
0

Use global keyword to access the outside variables in a function.

So your code will be

add_action( 'wp_head', function ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;
    }
    global $wpb_set_post_views;
    $wpb_set_post_views($post_id);
});

Or

add_action( 'wp_head', function ($post_id) {
        if ( !is_single() ) return;
        if ( empty ( $post_id) ) {
            global $post;
            $post_id = $post->ID;
        }
        $wpb_set_post_views = $GLOBALS['wpb_set_post_views'];
        $wpb_set_post_views($post_id);
    });

Please refer http://php.net/manual/en/language.variables.scope.php

2 Comments

Global is really no the answer here.
Global is so 2008, do not use

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.