0
votes

While typing this I'm being told there are many similarly phrased questions with answers to this in Stackoverflow - but having read many of the other queries I'm still no nearer to solving the issue I seem to be having regarding an error message about an undefined variable.

To explain . . .

I'm using a code snippet in my child theme functions file to test for sub-Pages which I've taken from the following Wordpress.org reference page: (snippet 4 in https://developer.wordpress.org/reference/functions/is_page/).

I've added 2 lines below the code to A) define what $pid is and B) an if statement that defines which sub pages I want to call up (see last 2 lines in the code below):

 * Check whether we are on this page or a sub page
 *
 * @param int $pid Page ID to check against.
 * @return bool True if we are on this page or a sub page of this page.
 */
function wpdocs_is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
    $post = get_post();               // load details about this page
 
    $is_tree = false;
    if ( is_page( $pid ) ) {
        $is_tree = true;            // we're at the page or at a sub page
    }
 
    $anc = get_post_ancestors( $post->ID );
    foreach ( $anc as $ancestor ) {
        if ( is_page() && $ancestor == $pid ) {
            $is_tree = true;
        }
    }
    return $is_tree;  // we arn't at the page, and the page is not an ancestor
}
?>

<?php $pid = is_page(array('my-newcomen','the-piston-engine-revolution')) ?>
<?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?> 

The code has worked OK for a while but the website is now becoming very slow. Having installed the 'Query Monitor' plugin I can see various php errors that I want to clear up/debug - and this is one of them.

The error message says that the variable $is_tree on line 579 (which is the last line in the above code <?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?>) is undefined.

This area of php is beyond my current understanding - but for me the $is_tree variable is defined as either $is_tree = false; or $is_tree = true; . . . but obviously this is not the case!

I'd very much appreciate if someone could look at the above code and let me know

A) why the variable is being seen as undefined and B) how I go about successfully defining it in order to resolve the error message

Many thanks

Phil

1
$is_tree is not defined because that variable is not in the GLOBAL scope. That variable only exists within that function. You can try this is_page( wpdocs_is_tree( $pid ) ) as that function will return TRUE or FALSE. - disinfor
Been reading up about a variable's scope so now understand that because $is_tree is inside the function, it'll be undefined when used outside of it. However still don't get why it's undefined tbh - is it because it's boolean (true/false) - how do you define that? Meanwhile tried your suggestion of using is_page( wpdocs_is_tree( $pid ) ) in the line <?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?> but it's still saying is_tree is undefined. Any ideas? - Phil Legg
How did you use it? Post the exact line you tried. $is_tree gets defined when you pass the $pid and any of the if statements are met. - disinfor
I used this as the last line in the code I posted in my original question <?php if (is_page( wpdocs_is_tree( $pid ) ) && !is_paged() && !is_page('society-business','journal-archive')) {?> Is that not what you meant? - Phil Legg

1 Answers

0
votes

So, there are a couple things here (based on the last comment):

  1. is_page() doesn't accept a bool value, it needs an id or a page name string (or an array of either.
  2. $is_tree gets defined when you pass in the pid of a page. The function returns true or false. $is_tree isn't used outside of that function - it's simply a holder for the bool value.
  3. wpdocs_is_tree() takes a single post id as a parameter, but you are passing an array.
  4. You are assigning $pid to is_page() which returns a true or false, not a post id, which is what wpdocs_is_tree() is expecting.
  5. I also updated the wpdocs_is_tree() to use global $post on the first line instead of $post = get_post() since there's already a global variable available.

Here's what the code should be:

/* Check whether we are on this page or a sub page
*
* @param int $pid Page ID to check against.
* @return bool True if we are on this page or a sub page of this page.
*/
function wpdocs_is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
    global $post;               // load details about this page

    $is_tree = FALSE;

    if ( is_page( $pid ) ) {
        $is_tree = TRUE;            // we're at the page or at a sub page
    }

    $anc = get_post_ancestors( $post->ID );
    foreach ( $anc as $ancestor ) {
        if ( is_page() && $ancestor === $pid ) {
            $is_tree = TRUE;
        }
    }

    return $is_tree;  // we aren't at the page, and the page is not an ancestor
}

?>

<?php
/* In plain english (in order): if the current page is my-newcomen or the-piston-engine-revolution 
 * AND wpdocs_is_tree returns true by passing the current page id, then continue 
 * AND is NOT society-business or journal-archive
 */
if ( is_page( ['my-newcomen', 'the-piston-engine-revolution'] ) && wpdocs_is_tree( get_queried_object_id() ) && ! is_page('society-business','journal-archive') ) { ?>