0

When someone logs into the wordpress dashboard I want the Pro version of my plugin to check for the latest version of itself on a remote (my) server. All it retrieves is the most recent version number.

function plugin_version_check() {
    global $latestversion
    $latestversion = (something);
    return $latestversion;
 } 
 do_action('wp_login', 'plugin_version_check');
 

so far so good, it works.

Then I would expect the value of $latestversion to be available elsewhere, but it isn't

function dk_plugin_meta_links( $links, $file ) {

    //use the value of $latestversion here      
}
add_filter( 'plugin_row_meta', 'dk_plugin_meta_links', 10, 2 );

Without success I have also tried

function dk_plugin_meta_links( $links, $file, $latestversion ) {

(including changing the accepted args to 3)

which I would expect to work since $latestversion is a global variable, right?

Looking for solutions I have gone in circles and gotten ridiculously confused. How do I do this?

2
  • You can't use global variables across multiple requests. When the user logs in you are defining the variable but when they visit the plugins page that variable is lost because you've loaded a new page. The proper solution is unclear as I can't tell what you're trying to do. What is this the latest version of? What do you need to do with this variable? Dec 4, 2022 at 15:36
  • @JacobPeattie I state "I want my plugin to check for the latest version", but I have clarified that. It does this successfully on the main plugin PHP page. I then want to use that information on the plugins page in the update plugins element.
    – Steve
    Dec 4, 2022 at 21:28

2 Answers 2

0

The solution for this was a compromise

I moved the code from the wp_login hook into my meta_links function and test to see if my value exists

function dk_plugin_meta_links( $links, $file ) {

    if($latestversion == "") {
      //get the latest version if we don't already have it
    }

    (do something with) $latestversion;      
}
add_filter( 'plugin_row_meta', 'dk_plugin_meta_links', 10, 2 );
-1

In your code, the $latestversion variable is defined as a global variable inside the plugin_version_check function. This means that the variable is only accessible inside this function, and it is not visible to other functions or code outside of this function.

In order to make the $latestversion variable available to other functions or code, you can use the global keyword to explicitly declare that you want to use the global version of this variable, rather than a local variable with the same name. Here is an example of how you could modify your code to do this:

function plugin_version_check() {
global $latestversion;
$latestversion = (something);
return $latestversion;
} 
do_action('wp_login', 'plugin_version_check');

function dk_plugin_meta_links( $links, $file ) {
global $latestversion;

// Use the value of $latestversion here      
}
add_filter( 'plugin_row_meta', 'dk_plugin_meta_links', 10, 2 );

By using the global keyword, you are telling PHP that you want to use the global version of the $latestversion variable inside the dk_plugin_meta_links function. This allows you to access the value of this variable that was set in the plugin_version_check function.

Alternatively, you could pass the $latestversion variable as an argument to the dk_plugin_meta_links function, like this:

function plugin_version_check() {
$latestversion = (something);
dk_plugin_meta_links(null, null, $latestversion);
} 
do_action('wp_login', 'plugin_version_check');

function dk_plugin_meta_links( $links, $file, $latestversion ) {
// Use the value of $latestversion here      
}
add_filter( 'plugin_row_meta', 'dk_plugin_meta_links', 10, 3 );

In this example, the $latestversion variable is passed as an argument to the dk_plugin_meta_links function, which allows you to access the value of this variable inside the function. This is a cleaner and more modular approach than using global variables, and it can help avoid potential conflicts or bugs in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.