The approachability of WordPress empowers users of highly varied technical backgrounds and expertise to develop sites with relative ease, but knowing when or how to level-up workflows can be tough for some. This has been especially true for newer developers looking to incorporate a Version Control System, such as Git, within their new and existing WordPress projects.
Learn WordPress Git Basics on Pantheon
Pantheon’s integrated git-based workflow greatly reduces the often steep learning curves associated with Git. Once modifications have been committed to the site, users can immediately track changes with minimal effort while also seeing who authored a particular change and why. The entire codebase is version controlled as a single entity with this integration, including WordPress core, plugins, themes, and respective stylesheets.
Without version control, WordPress developers find themselves relying on their memory or messy file naming conventions (e.g. functions-v6-stable-2015-11-16.php) to track code changes. These methods negatively impact collaboration and project visibility on teams while also making it a nightmare to undo changes after an afternoon of tweaking code.
Separate Custom Plugins or Themes
While the Git integration provided by Pantheon fosters a smooth introduction to version control, it’s important to consider implementations that extend beyond entire website projects. Arguably the greatest value of adopting Git into your WordPress development workflow comes from the practice of separating custom code (e.g. plugins, themes, etc.) into individual repositories to optimize reusability. A repository is essentially a storage unit for code which can exist both locally on your machine and/or remotely on Git hosts such as GitHub.
Repositories establish a dedicated location for the development and maintenance of code. It’s impossible to efficiently reuse custom code across projects without a centralized distribution strategy, and Git solves this problem wonderfully.
Create Local and Remote Git Repositories
To separate code from an individual site project and into its own Git repository:
Create a new directory on your local machine: mkdir custom-code-project-name
Note: This directory will be used to house custom code and needs to be separate from all WordPress installations and/or site projects.
Run git init to initiate the folder as a Git repository.
Move custom plugin or theme code out of the individual project and into the new repository.
Stage and commit your code: git commit -am “Initial commit for custom WordPress plugin/theme”
Note: Edit the text within the quotes as needed to provide a detailed commit message.
Add the new GitHub repository as a remote destination for your local repository.
Run git push [remote-name] master to update GitHub with your local repository.
Create separate repositories and repeat this process for all custom plugins or themes developed for your WordPress projects—even if you don’t plan to reuse it in the near future. Each of these repositories should be thought of as its own entity, containing only the code required for a single custom plugin or theme to operate.
Once custom code exists exclusively within its respective repository and hosted remotely with a service like GitHub, you can reintroduce the code to your projects one at a time from a single source. For sites running on Pantheon, download the source code for your plugin or theme from GitHub as a zip file and use the familiar method of uploading new plugins or themes via the WordPress Dashboard. Pantheon does not support Git submodules (putting another Git repository as a subdirectory of your site’s repository) so make sure to add it as a zip file. Maintenance and updates to custom code should be done within the remote repository, since it is used as the installation source. This will make updates easier to manage over time.
Plan an Update Strategy
Plugins hosted within the official WordPress plugin repository automatically notify users when an update is available. If you think others could benefit from using your code, consider submitting the plugin for review once you understand and adhere to the requirements. However, if the code is intended for an individual client or commercial use, it may not be the best candidate for the official WordPress repositories. Not to worry, it’s possible to send update notifications for your custom code without hosting them on the official WordPress repos.
More experienced developers can hook into the following WordPress core filters to trigger update notifications for custom plugins: pre_set_site_transient_update_plugins, plugins_api, and upgrader_post_install. Newer developers may find this process complicated or tricky, but like most things in WordPress: there’s a plugin for that. The GitHub Updater plugin (actively maintained by Andy Fragen) is a great alternative to using filters to trigger notifications and updates for custom code hosted on GitHub. WP Pusher provides a similar method for managing custom code. For details, see our related docs.
Now that you’ve nailed down a solid WordPress development workflow, review the following resources to become a Git ninja:
Ready for even more WordPress developer level-ups? Explore using Composer within your workflow to install and track updates for all packages commonly used in your projects, including your own custom plugins and themes. If you'd prefer to avoid the command line, check out our doc Managing Custom Code for WordPress with Plugins.
Update: For a great additional resource, see Morten Rand-Hendriksen's session GitHub for the Rest of Us from WordCamp US.Topics: Development, WordPress