-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] How does Gatsby know about new content when using gatsby-source-wordpress? #2753
Comments
Yeah — you'll need to trigger a new build when the WordPress content is updated. I believe @sebastienfi has a wordpress plugin he's using with Netlify to automate this. |
@KyleAMathews you mean sebastienfi/gatsby-wordpress-netlify-starter? I've glanced into the source but don't see where he's automating it. Is there something sensible that can be done so it runs automatically? Because in current form it's not very useful. |
He mentioned there's a wordpress plugin he uses that can fire a webhook when content is added or updated. |
@selrond you can add the following to your functions.php file (or create a plugin)
Hope that helps |
You should be able to use HookPress too. This will allow you to define webhooks for particular actions/filters in WordPress. Personally I prefer adding a button to the toolbar that says "publish/deploy" which triggers the webhook. |
@garytokyo @crgeary good idea, but I still have to host my static site on a nodejs server in order to rebuild it there right? |
@selrond not necessarily host it on a nodejs server - you would just have to have node and npm installed on your server (or a server). You could fire off a post request to a php script for instance that then triggers the gatsby build process (I'm pretty sure that is possible to run shell commands from PHP). I haven't set it up yet but I am also fairly sure you can use thirdparty services like DeployBot - set up a webhook that is triggered on wordpress save which runs your deploy commands. |
@selrond If you're hosting your Gatsby site on Netlify (if not, you probably should cos it's awesome), then they include a CI environment that supports Node JS for builds & deployments. :) |
@garytokyo right, either way though - you have to have some kind of a private server, instead of shared hosting (which is enough for WordPress alone). It makes it a bit pricier |
Hi guys, I know the topic is closed, but when I was looking at this it didn't provide me with the answer (maybe it's just me). I have done a little blog post on how to trigger Gatsby build on new Wordpress post. gatsby-wordpress-netlify-auto-update. Disclaimer: I am still a newbie so if there is anything wrong with the post let me know. |
hey @zetdotcom - I tried following the example. I have a Netlify webhook ready and added this to the end of the
After adding/deleting/updating a post, it does not trigger any builds - what am I missing? Thanks |
Hi. Have you set up your deploy settings in Netlify? I.e. give your repository url, ,set build command as 'gatsby build', public directory 'public/', and production branch to 'master'. |
@b0gd4n I finally solve this problem by using code below.
Reference: https://deliciousbrains.com/php-curl-how-wordpress-makes-http-requests/ |
@zetdotcom Absolutely yes!! |
@gapgag55 so replace $response = curl_exec( $curl ); with $response = Requests::post( NETLIFY_BUILD_HOOK ); ? |
@b0gd4n Do like this,
|
@gapgag55 damn - that worked. Thank you! |
hey @gapgag55 - is there a way of not triggering a build when a user logs in? - the code above seems to trigger the hook as soon as I log in, as well as when I bin a draft post and if I save a draft of a post. Would there be a way of only triggering the hook when you bin a published post, when you change a published post to un-published, and when you publish a post? Thanks |
@b0gd4n Change the WP hook to fit your solution |
I'll probably make a WordPress plugin for this in the near future. I'll make it work for anything that uses webhooks, but with extra bits specifically for Netlify (as they have an API I can access). I was thinking..
Does anyone want/need anything else? :) |
TLDR... Here's a plugin I wrote to do it for you: crgeary/wp-jamstack-deployments. @b0gd4n the Just to make things more annoying, WordPress uses posts for menus & menu items (among other things). Therefore saving menus will trigger a build per menu item + the menu itself. So basically a navigation menu with 10 items will fire 11 This is probably why that code runs when you login to the site. WordPress is probably saving some kind of a post in the background, which usually only happens when someone is actively using the site due to how WP-Cron works. @b0gd4n & @zetdotcom I see you're using With this in mind, I've built a plugin that has a manual deploy button as part of the admin bar, and also automatically deploys whenever you change a post type ( I have some additional features to add, but the core work is done around triggering builds. The future roadmap is to add better integration with Netlify, and decrease the number of requests made by deferring them. If you ran a script that updated 10 posts, currently it would fire 10 requests. However, I'd like to make it so that 10 updates happen, and then 1 request is made after the 10th. But this is a future todo. For now, here is the plugin: crgeary/wp-jamstack-deployments I'll look at getting this onto the WordPress repository, but for now (at least while it's BETA) it's going to be Github only. |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
Hey, just for the sake of reference, if anyone gets here. I've set up a basic prototype using the following stack. Features
What's missing
Total cost
Gatsby repo is available here. It's very simple, can probably be optimized. In Wordpress enabled theme // functions.php
function preview_link_fix( $preview_link, $post ) {
$id = $post->ID;
$latest_revision_id = '';
if($post->post_status !== 'draft') {
$revisions = wp_get_post_revisions($id);
$last_revision = array_shift($revisions);
$latest_revision_id = $last_revision ? $last_revision->ID : '';
}
$nonce = wp_create_nonce( 'wp_rest' );
$parsedUrl = parse_url( $preview_link );
$path = $parsedUrl['path'];
if ($path === '/') $path = "/preview";
$scheme = $parsedUrl['scheme'];
$host = $parsedUrl['host'];
$query = $parsedUrl['query'];
return "$scheme://$host$path?$query&revision_id=$latest_revision_id&_wpnonce=$nonce&status=$post->post_status";
}
add_filter( 'preview_post_link', 'preview_link_fix', 10, 2); Happy to help. |
Hey, I have setup a docker with gatsby and wordpress but for some reason I would like to keep everything together and not use netlify to host my gatsby site. What I would like to do is to host the whole app with wordpress, db and gatsby on Digital Ocean. Is there a way to trigger the gatsby site build on wordpress update button click with this setup? hopefully someone can help me with this, :) |
@jimmysafe to the best of my knowledge, for the time being, when you use gatsby and probably |
This might be something to mention, you can host on "Firebase Webhosting" for free, it also has cloud functions for the node backend, you can host there, do your backend on prismic.io ( also free and has graphql ) and prismic.io has webhooks available. Now you can have everything hosting and scripted in JS for free 100% with all your needs filled. You will not get to use wordpress headless, but really, prismic is 1000 times better and more efficient + its a CDN and its 100% freemium up to some crazy limits like firebase is. |
I just find this project, seems promising: |
If you use Netlify, on your Netlify account then you will have hook looks like this copy that and use it on Wordpress It worked for me 🎉 |
Do I have to manually regenerate the whole site? How does it work?
The text was updated successfully, but these errors were encountered: