Skip to main content
edited tags
Link
Tom J Nowell
  • 59.9k
  • 7
  • 75
  • 146
Source Link

Wp Plugins Update

Good morning everyone, I have a problem that I've been struggling with for days but you can't solve it. in my WordPress installation it doesn't detect some plugin updates while others do. I did it in debug to check if there are updates and manually the system finds them. Using this functions hooks

function clear_update_transients() {
    delete_site_transient('update_plugins');
    delete_transient('update_plugins');
    wp_cache_flush();
}
add_action('admin_init', 'clear_update_transients');
function check_plugin_updates_manually() {
    // Include the $wp_version
    require ABSPATH . WPINC . '/version.php';
    // Ensure we have the Plugin_Upgrader class
    if ( ! class_exists( 'Plugin_Upgrader' ) ) {
        require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    }
    // Get all installed plugins
    $plugins = get_plugins();
    $to_send = array(
        'plugins' => array(),
        'active' => array(),
    );
    foreach ($plugins as $plugin_file => $plugin_data) {
        $to_send['plugins'][$plugin_file] = array(
            'slug' => dirname($plugin_file),
            'version' => $plugin_data['Version']
        );
    }
    $options = array(
        'body'       => array(
            'plugins'      => wp_json_encode($to_send),
            'locale'       => wp_json_encode(get_locale()),
        ),
        'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url('/'),
    );
    $url = 'https://api.wordpress.org/plugins/update-check/1.1/';
    $response = wp_remote_post($url, $options);
    if (is_wp_error($response)) {
        var_dump("Error in update request: ", $response->get_error_message());
        return;
    }
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    if (isset($data['plugins'])) {
        foreach ($data['plugins'] as $plugin_slug => $plugin_update) {
            var_dump("Manual update available for $plugin_slug: ", $plugin_update);
          
            $upgrader = new Plugin_Upgrader();
            $result = $upgrader->upgrade($plugin_update['plugin']);
            if (is_wp_error($result)) {
                error_log("Error updating plugin $plugin_slug: " . $result->get_error_message());
            } else {
                error_log("Plugin updated successfully: $plugin_slug");
            }
        }
    } else {
        var_dump("No manual updates available for plugins");
    }
}
add_action('admin_init', 'check_plugin_updates_manually');

in my manual function it detects updates But there is no way to make it work automatically, it's the first time this has happened to me, do any of you have any advice to give me? thanks