A WordPress Plugin To Trigger Netlify Builds
January 2, 2022
This site is built as a headless site, using WordPress as a backend for content, and Next.js hosted on Netlify for the front end. As most pages of this site use static props for page content, sometimes triggering a new Next.js build on Netlify is required.
While this could be accomplished by visiting the Netlify dashboard, I wanted a way to trigger a build from my WordPress admin area. So I built a small plugin for this.
It is worth noting that others have built tools for this. I just wanted to take my own approach.
Inside of the Netlify dashboard, you are able to quickly create an endpoint that you can POST to and trigger a build. My plugin adds a sub menu page under the “Dashboard” page and has a button to “Trigger Build”. Instead of storing the POST endpoint in an option in the dashboard, my plugin requires that a constant is added to the wp-config.php file.
I kept the code simple and straightforward. Since this is such a small, utilitarian plugin, I did not use object-oriented PHP. It also uses procedural jQuery logic to handle AJAX requests and the related DOM manipulation.
Other Details:
- Uses aria live regions to announce to screen readers the status of the AJAX request
- Includes an optional field to add a “Trigger title” for describing the trigger in the Netlify dashboard
- Uses techniques to make translating the page’s text more logical (placeholders, context, etc.)
Future Development
I would like to add some hooks and filters to make this plugin more extensible. I would also like the ability to automatically trigger a build when a particular action occurs (updating a post or option, for example). Finding a balance between features and simplicity will be key.