Wordpress - Execute Command on New Post

2019-08-28 17:52发布

I have a localhost installed version of the latest WordPress on my server, and have begun designing my own plug, but am trying to learn a bit more about how WordPress works first.

I'm currently looking for a method to execute a terminal command (or just an application that runs on the server) every time a new post has been created. Even better if possible, to run a different command depending on what category the post was made in. Is something like this possible? Thanks in advance!

2条回答
孤傲高冷的网名
2楼-- · 2019-08-28 18:39

Try writing the function and using the action hook with publish_post as described here http://codex.wordpress.org/Plugin_API/Action_Reference

So it would be like this:

<?php
    function my_custom_function(){
        //stuff to do on new post
    }
    add_action('publish_post', 'my_custom_function');
?>

Hope this helps. I am not sure about the terminal execution but @thenetimp seems to have a handle on that.

查看更多
爷、活的狠高调
3楼-- · 2019-08-28 18:39

You'd use the php command exec() or shell_exec() they are both slightly different please read the docs on both.

http://php.net/manual/en/function.shell-exec.php

查看更多
登录 后发表回答