Question

Create a WordPress Theme That Updates From a Git Repository

Posted July 21, 2021 99 views
WordPressGit

Problem

WordPress themes are typically submitted to the review team to be hosted and published on the WordPress Theme Directory. We are working on an internal theme that will not work outside of our use case. It is also developed and managed using git and hosted on our Digital Ocean droplet.

How can we use our self-hosted, git-managed WordPress theme and have it behave like a normal theme would? For example, WordPress detects when the theme needs to be updated and the theme is auto-updated. If auto-updating is disabled, then it should wait till the update link is clicked manually.

I have begun with creating a --bare repo on our droplet with ssh authentication. I have also attempted to create a php class in the theme that cross checks the active theme’s version with the git repo but have unsuccessful in doing so. I did find some articles that have some good info but always ends up using a service or plugin to handle the pushing. If at all possible, we would like to accomplish this without 3rd party services or plugins.

I have broken out what I think needs to happen in the following steps:

  1. Get current theme version
  2. Look out to git repo via ssh (main branch)
  3. Get theme version of git repo
  4. If outdated, push notification to WordPress

Code

I was able to get the current theme version by using wp_get_theme() and then retrieving the version property. I am hoping to use the same function to check the theme on in the git repo but I hit a wall trying to retrieve the git repo. I am not sure how to retrieve the git repo, and once it is retrieved, how would I decode it from the bare git data. Also to clarify, we are not using Github or any other service to manage our git repo. Our WordPress install is currently setup on my local environment (wp-env) as I have not yet deployed it to a server. We are planning on deploying it on a FreeBSD distribution with Nginx.

<?php

class Dnk_Webos_Version_Check {
    protected $file;
    protected $theme;
    protected $version;

    public function __construct( $file ) {
        $this->file = $file;
        $this->theme = wp_get_theme();
        $this->version = $this->theme->version;

        // Debug test
        add_action( 'admin_notices', array( $this, 'debug_notice' ) );
    }

    public function debug_notice() {
        $debug = esc_html( $this->theme );
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo $this->version; ?></p>
        </div>
        <?php
    }
}

?>

Specifications

Local Environment
WordPress 5.8
PHP 7.4.21
Apache/2.4.38 (Debian)
Linux 5.10.25-linuxkit x86_64

Git Server
FreeBSD 12.2 zfs x64

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

I believe that the most straightforward way to do this is by using this GitHub updater here:

https://github.com/afragen/git-updater

This plugin is designed to update any GitHub hosted WordPress plugin or theme. Your plugin or theme must contain a header in the style.css header or in the plugin’s header denoting the location on GitHub.

Regards,
Bobby

  • Hey Bobby,

    Thanks for the response. I did see the Git Updater plugin. Unfortunately it will not work for our setup as we are working with a bare git repo that is not hosted on Github, Gitlab, etc.

    Thank you,
    Bula