Skip to content

Instantly share code, notes, and snippets.

@CruelDrool
Last active May 10, 2024 19:50
Show Gist options
  • Save CruelDrool/4cc70b819a33793396456c5ddb81781d to your computer and use it in GitHub Desktop.
Save CruelDrool/4cc70b819a33793396456c5ddb81781d to your computer and use it in GitHub Desktop.
Example of using "Update URI" (new in WordPress 5.8)
<?php
/*
Plugin Name: Testing Update URI
Author: Test Doe
Author URI: https://example.com
Update URI: https://example.com/current.json
Plugin URI: https://example.com
Description: This is a test plugin to test out the new plugin header field "Update URI".
Version: 1.0.0
*/
add_filter('update_plugins_example.com', function( $update, $plugin_data, $plugin_file, $locales){
/*
This filter is applied inside a foreach loop in wp_update_plugins().
So, if there a several plugins using the same hostname as Update URI, our function will be run for each of those other plugins.
Better check if the loop has reached *our* plugin until we do anything.
*/
if ($plugin_file == plugin_basename(__FILE__)) {
$request = wp_remote_get($plugin_data['UpdateURI']);
$request_body = wp_remote_retrieve_body( $request );
$update = json_decode( $request_body, true );
}
return $update;
},10, 4);
/*
Here's an example of a "current.json". This file will tell WordPress that the plugin has a new update available and where to
download it from. Can have an icon to be displayed too. Icons that can be used: "svg", "2x", "1x", "default".
Using "slug" to avoid getting an error from /wp-admin/update-core.php - "Undefined property: stdClass::$slug". This will also remove
"Visit plugin site" ("Plugin URI" is ignored) and add "View details".
{
"version": "1.0.1",
"slug": "update-test",
"tested": "6.3.2",
"icons": {
"svg": "https://example.com/icon.svg"
},
"package": "https://example.com/update-test-1.0.1.zip"
}
*/
/*
Replace "View details" link.
*/
add_filter('plugin_row_meta', function($plugin_meta, $plugin_file, $plugin_data) {
if ( $plugin_file == plugin_basename(__FILE__) && current_user_can( 'install_plugins' ) ) {
/*
Currently, "View details" is only visible to those that can install plugins.
Those that can't, they have the "Visit plugin site" already.
*/
$plugin_meta[2] = sprintf('<a href="%s" aria-label="%s">%s</a>',
$plugin_data['PluginURI'],
sprintf( __( 'Visit plugin site for %s' ), $plugin_data['Name'] ),
__( 'Visit plugin site' )
);
}
return $plugin_meta;
}, 10, 3 );
/*
If testing from a local IP then the filter below is required. WordPress uses wp_safe_remote_get() when downloading plugin packages.
wp_safe_remote_get() sets $args['reject_unsafe_urls'] to true which will reject local IPs.
*/
/*
add_filter('http_request_host_is_external', function($external, $host, $url) {
$external = $host == "example.com" ? true : $external;
return $external;
},10, 3);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment