So for some context i have created a site using gatsby, that pulls data in from Wordpress' API. I figured out i can trigger a hook within wordpress to fire after a post is saved by adding this into my functions.ph p file.
add_action( 'save_post', 'fireFunctionOnSave' );
function fireFunctionOnSave($post_id)
{
if(wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ))
{
return;
}
$old_path = getcwd();
chdir('/Applications/MAMP/htdocs/elliot-wp-gatsby/wp-
content/themes/twentyseventeen/');
$output = shell_exec('./build.sh');
chdir($old_path);
$myfile = fopen("/Applications/MAMP/htdocs/elliot-wp-gatsby/wp-
content/themes/twentyseventeen/output.txt", "w") or die("Unable to open
file!");
fwrite($myfile, $output);
fclose($myfile);
}
so far it can open and run my shell script but it wont actually run gatsby build any one have any ideas? This is my shell script.
#!/bin/bash
cd /Users/elliotm/Dev-local/Projects/gatsby-wp
npm run build
echo "site built/deployed"
Thanks
I'm not sure shell_exec works well with bash scripts and changing directories. Please try with: exec('cd /Users/elliotm/Dev-local/Projects/gatsby-wp && npm run build')
.
The &&
will force the exec to only execute the second part if the first one is successful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.