WordPress Development with Git
WordPress Development with Git
How newer web developers can learn to incorporate Git into WordPress projects.
Join the DZone community and get the full member experience.Join For Free
Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.
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 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 Repositories
To separate code from an individual site project and into its own Git repository:
- Install Git.
- 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.
- Create a GitHub account or Login to an existing account.
- Create a new remote repository.
- 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. 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.
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.
This article originally appeared on the Pantheon blog by Rachel Whitton.
Published at DZone with permission of Emily Miller , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.