1

I'm working on a WordPress plugin hosted locally on GitHub, and it includes several addons. When updating a specific add-on, I want to ensure that the main plugin is of a particular version before allowing the update. I have wrote the logic for this work but I am not able to test it because I don't have update option on my add on. To add update option I have used the code below but it didn't work.

function check_for_plugin_update($transient) {
   $github_username = 'username';
   $github_repo = 'repo_name';
   $access_token = 'access_token'; 
 
    if (empty($transient->checked)) {
        return $transient;
    }

    // Get the current version of your plugin
    $current_version = '1.3.1'; // Replace with your current version

    // Get the latest release from GitHub
    $url = "https://api.github.com/repos/{$github_username}/{$github_repo}/releases/latest";
    if ($access_token) {
        $url = add_query_arg('access_token', $access_token, $url);
    }

    $response = wp_remote_get($url);
    if (!is_wp_error($response)) {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body);

        // Compare versions
        if ($data && version_compare($current_version, $data->tag_name, '<')) {
            $transient->response[plugin_basename(__FILE__)] = (object) array(
                'new_version' => $data->tag_name,
                'package' => $data->zipball_url,
                'url' => $data->html_url
            );
        }
    }

    return $transient;
}
 
add_filter('pre_set_site_transient_update_plugins', 'check_for_plugin_update');

0

Your Answer

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

Browse other questions tagged or ask your own question.