0

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

1
  • it's possible there's a plugin that's trying to implement its own update mechanism that's interfering due to not implementing the filters properly but without knowing which plugins are not updating and which plugins are installed it's entirely a guess. It could even be your own code causing the problem but I don't see a way for that to happen. It would be much easier to use WP CLI and cron or a dedicated package manager such as composer than to try to auto-update on admin_init, especially since there's no throttling/limits in place here. At a minimum your admin gets slower from HTTP requests
    – Tom J Nowell
    Commented Jul 3 at 9:12

0

Browse other questions tagged or ask your own question.