admin管理员组文章数量:1326310
I have created a php script (generate.php) which is querying posts on set criteria and then writing result in custom xml file. I would like to execute the generate.php everytime the post Save/Update button is pressed. Do you have any clue on how to do this? May be with cron jobs every X minutes would be better?
I have created a php script (generate.php) which is querying posts on set criteria and then writing result in custom xml file. I would like to execute the generate.php everytime the post Save/Update button is pressed. Do you have any clue on how to do this? May be with cron jobs every X minutes would be better?
Share Improve this question asked Aug 7, 2020 at 8:12 jamjam 1732 gold badges3 silver badges15 bronze badges3 Answers
Reset to default 1Seems like you want the save_post
hook which is called when a new post is saved or existing post is updated.
Make sure your PHP file is loaded (e.g. require
or include
) and has a function you can call, and then cause that function to be run when a post is saved or updated with:
add_action('save_post', 'your_function_name', 10, 1);
Note the last parameter there is the number of parameters you want from the hook, see the docs page.
I believe you should look into status transitions: https://developer.wordpress/reference/hooks/transition_post_status/
There are a couple of examples below the explanation on top of the page. Here's also some example code of mine:
add_action( 'transition_post_status', 'nxt_create_news', 10, 3 );
// Automatically create a news when a wiki post has been published
function nxt_create_news($new_status, $old_status, $nxt_wiki_post) {
if('publish' === $new_status && 'publish' !== $old_status && $nxt_wiki_post->post_type === 'wiki') {
... do something here
}
In your case, the old_status should be 'publish' and the new status as well. Make sure to find a way to prevent an endless loop, but I believe that the examples on the WP documentary should prove useful. :-)
At the end, I solved with jQuery and ajax
jQuery( '.post-type-property #post' ).submit( function( e ) {
if(!(checkMetaFields() && checkCategories())){ //some validation functions
return false;
}else{
jQuery.ajax({
url:"https://demo.site/XML/6098123798/gen.php",
type:"post"
})
}
} );
本文标签: Execute php after post saveupdate
版权声明:本文标题:Execute php after post saveupdate 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742204841a2432623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论