php - Custom global variable not working in function

admin2025-01-08  5

I've been stumped on this for a while and cannot for the life of me figure out why my function isn't able to reference my global variable.

$query_obj = get_queried_object();

if ( is_a($query_obj, 'WP_Term') ) {
    $my_base_url = get_term_link($query_obj);
} elseif ( is_a($query_obj, 'WP_Post') ) {
    $my_base_url = get_permalink();
} else {
    $my_base_url = null;
}

function getDropdown($args, $items, $all=false) {
    global $my_base_url;

    if (isset($_GET[$args])) {
         $check = $_GET[$args];
    } else {
        $check = null;
    }
    $output = '<ul class="dropdown ' . $args . '">';
    if ($all == true) {
        $output .= '<li class="' . ($check == null ? 'active' : null) . '"><a href="' . remove_query_arg( $args, add_query_arg( null, null ) ) . '">All</a></li>';
    }
    foreach ($items as $key => $value) {
        $output .= '<li' . ($check == $value ? ' class="active"' : null) .'><a href="' . esc_url( add_query_arg( $args, $value ) ) . '">' . $key . '</a></li>';
    }
    $output .= '</ul>';
    return $output . '<p>' . $my_base_url . '</p>';
}

The return at the bottom just has a paragraph tacked out to return the variable but it shows up blank. The only way I've been able to use the base URL was by using it as a function argument and passing it that way. What am I missing?

I've been stumped on this for a while and cannot for the life of me figure out why my function isn't able to reference my global variable.

$query_obj = get_queried_object();

if ( is_a($query_obj, 'WP_Term') ) {
    $my_base_url = get_term_link($query_obj);
} elseif ( is_a($query_obj, 'WP_Post') ) {
    $my_base_url = get_permalink();
} else {
    $my_base_url = null;
}

function getDropdown($args, $items, $all=false) {
    global $my_base_url;

    if (isset($_GET[$args])) {
         $check = $_GET[$args];
    } else {
        $check = null;
    }
    $output = '<ul class="dropdown ' . $args . '">';
    if ($all == true) {
        $output .= '<li class="' . ($check == null ? 'active' : null) . '"><a href="' . remove_query_arg( $args, add_query_arg( null, null ) ) . '">All</a></li>';
    }
    foreach ($items as $key => $value) {
        $output .= '<li' . ($check == $value ? ' class="active"' : null) .'><a href="' . esc_url( add_query_arg( $args, $value ) ) . '">' . $key . '</a></li>';
    }
    $output .= '</ul>';
    return $output . '<p>' . $my_base_url . '</p>';
}

The return at the bottom just has a paragraph tacked out to return the variable but it shows up blank. The only way I've been able to use the base URL was by using it as a function argument and passing it that way. What am I missing?

Share Improve this question edited Nov 29, 2020 at 0:38 tylorreimer asked Nov 28, 2020 at 16:39 tylorreimertylorreimer 651 silver badge8 bronze badges 3
  • 1 Perhaps all you need to do is call the global both when defining/setting the variable as well as when reading its value, e.g. global $var; $var = 'foo'; in a function and global $var; echo $var; in another function. But your question is off-topic here and better suited at Stack Overflow.. – Sally CJ Commented Nov 28, 2020 at 18:03
  • @SallyCJ I think there was some confusion since I didn't include the one variable in my original code in the topic. $my_base_url is being defined and storing the URL correctly. I can echo it outside of the getDropdown function correctly but inside the function it doesn't show anything. It looks like defining global on the variable initially and in the function fixed it. Looking at other examples online it doesn't seem like you normally have to add global initially so it seems odd but at least it's working so thank you. – tylorreimer Commented Nov 29, 2020 at 3:17
  • 1 The "in a/another function" are just sample contexts. And you need to call global if you're not already in the global scope when setting the variable just as the sample contexts, i.e. two different functions. Alternatively, you can use the array notation: $GLOBALS['var'] = 'foo' and there'd be no need for the global call. But as I pointed, these "global" stuff are generic PHP programming and not WP-specific - WP does not do anything that would prevent one from reading/setting a global variable. And perhaps, it's better if you just pass the variable to the function? – Sally CJ Commented Nov 29, 2020 at 3:42
Add a comment  | 

1 Answer 1

Reset to default 0

I try to use this code and for the if statments I had to put a global $query_obj before the code, I do not know if is the case.

global $query_obj;
if ( is_a($query_obj, 'WP_Term') ) {
    $my_base_url = get_term_link($query_obj);
} elseif ( is_a($query_obj, 'WP_Post') ) {
    $my_base_url = get_permalink();
} else {
    $my_base_url = null;
}

this seems to be working properly this way. In my wordpress only returns null. but the global variable $my_base_url is working fine when whe call $global $query_obj before the if's statments.

global $query_obj;
if ( is_a($query_obj, 'WP_Term') ) {
    $my_base_url = get_term_link($query_obj);
} elseif ( is_a($query_obj, 'WP_Post') ) {
    $my_base_url = get_permalink();
} else {
    $my_base_url = 'its null';
}
function ti(){
  global $my_base_url;
  echo '<pre>';
  print_r($my_base_url);
  echo '</pre>';
}
ti();

Now to test the function I will need the $args , $items variables at least..

If you have the wp_debug set to true in wp_config.php this error should appear

define( 'WP_DEBUG', true );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269001a1318.html

最新回复(0)