最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Execute php after post saveupdate

programmeradmin5浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 1

Seems 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"
            
            })
            
            
            
        }   
    
     } );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论