Help using a variable in a function parameter
-
I have the following code in a plugin:
if( $var != '' && !is_user_logged_in() ) { if( is_child( $var ) ) { auth_redirect(); } }
$var is functioning as intended on the page and I use it elsewhere in the template, but it’s not working when used as a parameter for the is_child() function.
If I put the ID number manually as the parameter.. it works perfectly.
Can someone help me figure out why please?
-
This topic was modified 5 years, 9 months ago by
OthelloBloke.
-
This topic was modified 5 years, 9 months ago by
-
Start off with some standarddebigging. Something like this will let you see exactly what is going on with that code.
if( $var != '' && !is_user_logged_in() ) { echo "<p>var: '".$var."'</p>"; if( is_child( $var ) ) { auth_redirect(); } }
If that isn’t correct, wrk backwards until you find out where the problem is.
I already did that. It’s echoing the page ID number that it’s supposed to.
But it doesn’t work when I put the variable as the parameter ??
The you should add the echo line in s the first line of the is_child() function and see what that’s telling you that it’s getting. If that’s the same, then look further into that function.
is_child() isn’t part of the WordPress core, so there’s no way for anyone here to say how it works unless we can see the code for it ??
It’s not echoing anything ?? I’m stuck. Here’s the is_child function:
<?php if( !function_exists( "is_child" ) ) { function is_child( $ofParent, $doRecursive = true ) { global $wpdb; $allCats = array(); // Turn title or slug into ID if needed. if( !is_int( $ofParent ) ) { if( is_page() ) # Different handling for Pages $getID = $wpdb->get_results(" SELECT ID as cat_ID FROM {$wpdb->posts} WHERE post_title = '{$ofParent}' OR post_name = '{$ofParent}' LIMIT 0,1 "); else # Get catID $getID = $wpdb->get_results(" SELECT cat_ID FROM {$wpdb->terms} // FROM {$wpdb->categories} WHERE cat_name = '{$ofParent}' OR category_nicename = '{$ofParent}' LIMIT 0,1 "); if( !$getID ) # Not found. return false; else { # Found. $ofParent = $getID[0]->cat_ID; unset( $getID ); } } // Everyone's a sub zero. if( $ofParent == 0 && $doRecursive ) return true; // Now let's break it down to categories (or pages). if( is_page() ) { global $post; $allCats[] = $post->ID; } elseif( is_single() ) { $getCats = get_the_category(); foreach( $getCats as $getCat ) $allCats[] = $getCat->cat_ID; unset( $getCats ); } elseif( is_category ) { global $cat; $allCats[] = $cat; } // Already a match? Would save processing time. if( in_array( $ofParent, $allCats ) ) return true; // Post without recursive search ends here. if( ( is_single() ) && !$doRecursive ) return false; // Otherwise, let's do some genealogy. while( count( $allCats ) != 0 ) { if( in_array( $ofParent, $allCats ) ) return true; else $allCats = is_child_getParents( $allCats ); } // Still here? Then nothing has been found. return false; } } if( !function_exists( "is_child_getParents" ) ) { function is_child_getParents( $fromChilds ) { // As there's only get_category_parents which isn't useful // for fetching parental data, we'll have to query this // directly to the DB. global $wpdb; $fromChilds = implode( ", ", $fromChilds ); if( !$fromChilds ) return array(); $getParents = ( is_page() ) ? # Pages $wpdb->get_results(" SELECT post_parent AS category_parent FROM {$wpdb->posts} WHERE ID IN ({$fromChilds}) ") : # Posts / Categories $wpdb->get_results(" SELECT category_parent // FROM {$wpdb->categories} FROM {$wpdb->terms} WHERE cat_ID IN ({$fromChilds}) "); foreach( $getParents as $getParent ) if( $getParent->category_parent != 0 ) $allParents[] = $getParent->category_parent; return $allParents; } }
What does this do?
if( !function_exists( "is_child" ) ) { function is_child( $ofParent, $doRecursive = true ) { echo "<p>ofParent: '".$ofParent."'</p>"; global $wpdb; ..... } } else { echo "<p>is_child() already exists!</p>"; }
is_child already exists.
Then that’s your problem. Your version of is_child() is not being used because it’s already defined somewhere else before yours is loaded. You’ll need to find waht is already there, and see if it’s compatible. If not you can also change the name of your function to something unique that doesn’t interfere with anything else.
Yeah it’s being defined in the same plugin.
It can’t work like that. You can not ever have two functions called the same thing. You will need to change the name of one of them to something unqiue.
No I mean it’s only defined in one place in the plugin… and then I use the is_child function later on in the plugin in a difference place.
That’s not how it works. It may only be defined in one place in your plugin, but from the results of your own debugging it’s already been defined by something else before the system gets to your plugins code. That’s why the echo of “already exists” shows. If you remove the if(function_exists()) call you’ll see the error and where it is defined.
But, again… you only need to change the name of that function to soemthing unique it will work.
So why does it work if I replace my original code with:
if( !is_user_logged_in() && is_child(232) ) { auth_redirect(); }
But is that calling your is_child() function, or the other is_child() function? I’ll bet it’s the other one. And I can’t tell you that because I don’t know what the other is_chld() function does. Again, that’s added somewhere else.
I just went through the ENTIRE plugin, and the ONLY place is_child is used or mentioned is here:
<?php if( !function_exists( "is_child" ) ) { function is_child( $ofParent, $doRecursive = true ) { global $wpdb; $allCats = array(); // Turn title or slug into ID if needed. if( !is_int( $ofParent ) ) { if( is_page() ) # Different handling for Pages $getID = $wpdb->get_results(" SELECT ID as cat_ID FROM {$wpdb->posts} WHERE post_title = '{$ofParent}' OR post_name = '{$ofParent}' LIMIT 0,1 "); else # Get catID $getID = $wpdb->get_results(" SELECT cat_ID FROM {$wpdb->terms} // FROM {$wpdb->categories} WHERE cat_name = '{$ofParent}' OR category_nicename = '{$ofParent}' LIMIT 0,1 "); if( !$getID ) # Not found. return false; else { # Found. $ofParent = $getID[0]->cat_ID; unset( $getID ); } } // Everyone's a sub zero. if( $ofParent == 0 && $doRecursive ) return true; // Now let's break it down to categories (or pages). if( is_page() ) { global $post; $allCats[] = $post->ID; } elseif( is_single() ) { $getCats = get_the_category(); foreach( $getCats as $getCat ) $allCats[] = $getCat->cat_ID; unset( $getCats ); } elseif( is_category ) { global $cat; $allCats[] = $cat; } // Already a match? Would save processing time. if( in_array( $ofParent, $allCats ) ) return true; // Post without recursive search ends here. if( ( is_single() ) && !$doRecursive ) return false; // Otherwise, let's do some genealogy. while( count( $allCats ) != 0 ) { if( in_array( $ofParent, $allCats ) ) return true; else $allCats = is_child_getParents( $allCats ); } // Still here? Then nothing has been found. return false; } } if( !function_exists( "is_child_getParents" ) ) { function is_child_getParents( $fromChilds ) { // As there's only get_category_parents which isn't useful // for fetching parental data, we'll have to query this // directly to the DB. global $wpdb; $fromChilds = implode( ", ", $fromChilds ); if( !$fromChilds ) return array(); $getParents = ( is_page() ) ? # Pages $wpdb->get_results(" SELECT post_parent AS category_parent FROM {$wpdb->posts} WHERE ID IN ({$fromChilds}) ") : # Posts / Categories $wpdb->get_results(" SELECT category_parent // FROM {$wpdb->categories} FROM {$wpdb->terms} WHERE cat_ID IN ({$fromChilds}) "); foreach( $getParents as $getParent ) if( $getParent->category_parent != 0 ) $allParents[] = $getParent->category_parent; return $allParents; } }
and then when I try to use it. I’m really stuck.
Again, it doesn’t have to be in your plugin. It can be in any other plugin or code before your plugin is loaded. A plugin is not an encapsulated littel ball of code. It still has to interact and play nocely with everything else in the WordPress system including core, other plugins and themes.
Did you remove the if(function_exists()) call as I said? Do that with debugging on (WP_DEBUG in your wp_config.php file) and you’ll see where it’s defined, and it will be outside of your plugin.
- The topic ‘Help using a variable in a function parameter’ is closed to new replies.