1

I'm trying to make the update feature for my plugin but I don't manage to the the "View Version Details" or the "Update Now" features working.

It keeps or returning "Plugin not found."

I'm returning this 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
}

I only managed to get "View Version Details" working when I removed the slug parameter on $plugin_info. Which loads the details page directly instead of something like this:

https://example.com/wp-admin/plugin-install.php?tab=plugin-information&plugin=test&section=changelog&TB_iframe=true&width=772&height=542

My main file test/test.php also contains the 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? I can't find any good documentation on pre_set_site_transient_update_plugins anywhere on WordPress, so I'm a bit lost here. How do WP developers know how to do this kind of stuff without proper docs?

enter image description here

1 Answer 1

1

Generally, if you are planing to release your plugin into a WordPress repo, you do not need to manually handle the update process. WordPress will handle this for you.

When your plugin will be approved, you will receive SVN access. When you will push new version there, all WordPresses will know that there is new version and they will handle update process. You do not need to do anything on your side to have this information covered.

If you want to store plugin files somewhere else (on your server for example) It is better to use this filter:

add_filter( 'site_transient_update_plugins', array( $this, 'check_for_new_version' ) );

/**
 * Checks if there is new update, and prepare WordPress transient.
 *
 * @param stdClass $transient - array of available updates.
 * @return array
 */
public function check_for_new_version( $transient ) {

    if ( empty( $transient->checked ) ) {
        return $transient;
    }

    $remote = $this->get_plugin_info();

    if ( $remote && version_compare( $this->current_version, $remote->version, '<' ) && version_compare( $remote->requires, get_bloginfo( 'version' ), '<=' ) && version_compare( $remote->requires_php, PHP_VERSION, '<' ) ) {
        $res              = new \stdClass();
        $res->slug        = self::PLUGIN_SLUG;
        $res->plugin      = self::PLUGIN_NAME;
        $res->new_version = $remote->version;
        $res->tested      = $remote->tested;
        $res->package     = $remote->download_url;

        $transient->response[ $res->plugin ] = $res;
    } elseif ( $remote ) {
        $res              = new \stdClass();
        $res->slug        = self::PLUGIN_SLUG;
        $res->plugin      = self::PLUGIN_NAME;
        $res->new_version = $remote->version;
        $res->tested      = $remote->tested;
        $res->package     = $remote->download_url;

        $transient->no_update[ $res->plugin ] = $res;
    }

    return $transient;
}

Where:

get_plugin_info() - function that will return basic information about your plugin current version (for example from external file)
self::PLUGIN_SLUG = 'test'
slef::PLUGIN_NAME = 'test/test.php'
$current_version = '1.0.0' - current version of the plugin

Also, to be sure you are displaying proper details about new plugin version on plugins page you can use this filter:

add_filter( 'plugins_api', array( $this, 'prefill_plugin_details' ), 20, 3 );

/**
 * Function to prefill plugin details on Plugins page.
 *
 * @param stdClass $res - empty at this step.
 * @param string   $action - 'plugin_information'.
 * @param stdClass $args - stdClass Object ( [slug] => test [is_ssl] => [fields] => Array ( [banners] => 1 [reviews] => 1 [downloaded] => [active_installs] => 1 ) [per_page] => 24 [locale] => en_US ).
 * @return stdClass
 */
public function prefill_plugin_details( $res, $action, $args ) {

    // do nothing if this is not about getting plugin information.
    if ( 'plugin_information' !== $action ) {
        return $res;
    }

    // do nothing if it is not our plugin.
    if ( self::PLUGIN_SLUG !== $args->slug ) {
        return $res;
    }

    $remote = $this->get_plugin_info();

    $res                 = new \stdClass();
    $res->name           = $remote->name;
    $res->slug           = $remote->slug;
    $res->author         = $remote->author;
    $res->author_profile = $remote->author_profile;
    $res->version        = $remote->version;
    $res->tested         = $remote->tested;
    $res->requires       = $remote->requires;
    $res->requires_php   = $remote->requires_php;
    $res->download_link  = $remote->download_url;
    $res->trunk          = $remote->download_url;
    $res->last_updated   = $remote->last_updated;
    $res->sections       = array(
        'description'  => $remote->sections->description,
        'installation' => $remote->sections->installation,
        'changelog'    => $remote->sections->changelog,
    );

    // in case you want the screenshots tab, use the following HTML format for its content:
    // <ol><li><a href="IMG_URL" target="_blank"><img src="IMG_URL" alt="CAPTION" /></a><p>CAPTION</p></li></ol>.
    if ( ! empty( $remote->sections->screenshots ) ) {
        $res->sections['screenshots'] = $remote->sections->screenshots;
    }

    $res->banners = array(
        'low'  => $remote->banners->low,
        'high' => $remote->banners->high,
    );

    return $res;
}

It relays on the same get_plugin_info function, that returns details about your newest version of the plugin.

Code above is from one of my old plugins I didn't test it right now, but should work.

plugins_api filter docs: https://developer.wordpress.org/reference/functions/plugins_api/

But in generall if possible, I would not use this methods, and relay on SVN you will receive.

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.