Adding automatic theme updates from a GitHub repository is actually pretty simple. The following function will hook into WordPress's native update system and grab the latest release from the repo of your choosing (if the version number has increased).
Place the following in your functions.php
// Automatic theme updates from the GitHub repository
add_filter('pre_set_site_transient_update_themes', 'automatic_GitHub_updates', 100, 1);
function automatic_GitHub_updates($data) {
// Theme information
$theme = get_stylesheet(); // Folder name of the current theme
$current = wp_get_theme()->get('Version'); // Get the version of the current theme
// GitHub information
$user = 'YOUR USERNAME'; // The GitHub username hosting the repository
$repo = 'YOUR-REPO-NAME'; // Repository name as it appears in the URL
// Get the latest release tag from the repository. The User-Agent header must be sent, as per
// GitHub's API documentation: https://developer.github.com/v3/#user-agent-required
$file = @json_decode(@file_get_contents('https://api.github.com/repos/'.$user.'/'.$repo.'/releases/latest', false,
stream_context_create(['http' => ['header' => "User-Agent: ".$user."\r\n"]])
));
if($file) {
$update = filter_var($file->tag_name, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
// Only return a response if the new version number is higher than the current version
if($update > $current) {
$data->response[$theme] = array(
'theme' => $theme,
// Strip the version number of any non-alpha characters (excluding the period)
// This way you can still use tags like v1.1 or ver1.1 if desired
'new_version' => $update,
'url' => 'https://github.com/'.$user.'/'.$repo,
'package' => $file->assets[0]->browser_download_url,
);
}
}
return $data;
}
This is a pretty standard disclaimer, but make sure your releases don't contain any .git files. You'd hate for that to muck up someone's WordPress installation. If you're on a Mac you can use my Automator script to exclude .git files automagically.
Tested with WordPress 4.3
Hi @eduardo-marcolino , thanks for sharing this. Could I just ask where this code goes? As I'm struggling to get this to work on some of my work. I seem to be getting a fatal error to do with the amount of arguments parsed. It seems like that add_filter is not accepting the url that I'm trying to pass through, saying that it only received one argument when it expected two. Here's the exact error:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in /my-site/public/wp-includes/class-wp-hook.php on line 310 and exactly 3 expected in /my-site/public/wp-content/themes/custom-theme/updater.php:39
It would be great to get this working. But in it's current state it doesn't look like it does that. Would it also be possible for you to share what your full file looks like? Just so I can get a better idea of how you've achieved this. Thanks!