0

When I click on "Update now" the plugin gets installed, but I'm getting the following error:

The plugin "test/test.php" has been deactivated due to an error: Plugin file does not exist.

Also, I can see on the file structure the plugin has been installed using a random suffix (test-wmAX6O) instead of just using test for the folder name.

I'm returning the following on check_for_plugin_update:

$plugin_info = array(
    'slug'        => 'test',
    'id'          => 'test/test.php',
    'plugin'      => 'test/test.php',
    'new_version' => $remote_version->version,
    'url'         => $remote_version->info_url,
    'package'     => $remote_version->download_url,
    'requires_php'  => '',
    'compatibility' => new stdClass(),
);

$checked_data->response['test/test.php'] = (object) $plugin_info;

return $checked_data;

Which gets called in the main file test/test.php in the constructor:

public function __construct() {
         ....
    $this->force_plugin_update_check();

    add_action( 'pre_set_site_transient_update_plugins',  array($this, 'check_for_plugin_update') );
}

function force_plugin_update_check(){
  delete_site_transient('update_plugins');
  wp_update_plugins();  // Force a fresh update check
}

The plugin's main file test/test.php also contains the following header comments:

<?php
/**
* Plugin Name: Test
* Plugin URI: https://test.io
* Description: A brief description of the plugin.
* Text Domain: test
* Version: 1.1.1
* Author: XXXX
* Author URI: https://test.io

* License: GPL2
* @package test
**/

Am I missing something?

It looks like there's a problem with the "slug" but I can't see why.

I can't find any good documentation on pre_set_site_transient_update_plugins anywhere on WordPress, so I'm a bit lost here.

3
  • You can't find it because that's an old hack created before the official API was added. There's a dedicated API for custom plugin updates that was added in WP 5.8, you don't need to manually modify transients, read make.wordpress.org/core/2021/06/29/… and look up the Update URI: field for the plugin header and the update_plugins_{$hostname} filters. Ignoring that, wouldn't the transient be set when the plugin updates are checked, so your filter would be added after rather than before? Order matters!
    – Tom J Nowell
    Commented Aug 21 at 12:48
  • So... Am I doing something wrong? Isn't this suppose to work even through it's a "hack"? I didn't understand your last sentence. Regarding update_plugins_ or the Update URI I've read the article multiple times but I still don't understand how that helps with the updates. Any place where I can see a code example?
    – Alvaro
    Commented Aug 21 at 16:12
  • @TomJNowell I've replaced pre_set_site_transient_update_plugins for site_transient_update_plugins but still not working. Same problem.
    – Alvaro
    Commented Aug 21 at 17:17

1 Answer 1

0

I tried the hook update_plugins_{$hostname} following the fresher informations I found in the link quoted by Tom J Nowell :
https://make.wordpress.org/core/2021/06/29/introducing-update-uri-plugin-header-in-wordpress-5-8/

here is a example of plugin with self managed update. it's a base for update by the Updates page and the Plugins page and this example can be complete to manage more details, e.g. in the plugin information page. then don't hesitate to edit to complete.

<?php
/*
Plugin Name: MY_PLUGIN
Version: 128
Plugin URI: https://my_domain.com/my_plugin
Update URI: https://my_domain.com/my_plugin
*/

if (!function_exists("add_action")) {
    echo "plugin";
    exit();
}

// the part "my_domain.com" in the hook is the domain of the url in the header "Update URI"
add_filter("update_plugins_my_domain.com", function ($update, $plugin_data, $plugin_file, $locales) {
    
    // if you manage more plugins on the same domain,
    //     test here if it's the current plugin
    
    
    if (TRUE) { // if there is a new version for this plugin
        $update = [
            "slug" => "MY_PLUGIN",
            "version" => 137, // new version
            "package" => "https://my_domain.com/plugins-updates/my_plugin-v137.zip",
        ];
    }
    
    
    return $update;
    
}, 10, 4);


// filter to manage the information page of the plugin
add_filter("plugins_api", function ($res, $action, $args) {
    
    if (    ("plugin_information" === $action)
        &&  ("MY_PLUGIN" === $args->slug)
    ) {
        
        $res = (object) [
            "slug" => $args->slug,
            "name" => "Name of the plugin",
            "sections" => [
                "description" => "<strong>plugin information</strong>",
                "changelog" => "<ul><li>details of the change log</li></ul>",
            ],
            "banners" => [
                "low" => "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/8_Poultry.jpg/1024px-8_Poultry.jpg",
            ],
            "download_link" => ".", // just any single character is enough
            // to enable update button on information page
        ];
        
    }
    
    
    return $res;
    
}, 10, 3);
1
  • update_plugins_{$hostname} filter doesnt seem to run in my page. I dont get anything inside add_filter("update_plugins_my_domain.com", function ($update, $plugin_data, $plugin_file, $locales) { var_dump($plugin_file);
    – Toniq
    Commented Sep 16 at 17:39

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.