dousu8767 2019-03-08 09:20
浏览 48

以编程方式在Wordpress中查找未知的帖子ID

So, I'm building my first plugin for WordPress. It's a simple plugin that queries a SQL database, gets blog readers emails, and sends them an email whenever a post is updated or published.

The idea is to have an email which tells the reader if the post was updated from its older version or a new post is published. It is also supposed to provide a link to the post for quick reading.

I've gotten this far:

  add_action( 'publish_post', 'email_function' );

function email_function( $arg ) {
    $link = mysqli_connect("*********", "********", "********", "***********");

            if (mysqli_connect_error()) {

                die ("There was an error connecting to the database");

            } 

    $query = "SELECT `******` FROM `*******`";

    $msg = "There has been a new blog post at The White Road!";

    if ($result = mysqli_query($link, $query)){
         while ($row = mysqli_fetch_array($result)){

             mail($row['email'],"New Post!",$msg);

         }}else {
             //for testing
            echo "break";
         }
}

Now, the issue is that in this situation I want this function to be carried out automatically, but to be able to do that I'll have to be able to find a few unknowns programmatically. In particular, the post that is being updated, if the post is being updated or if it's new, the author of said post, and the link to its page.

I've had a look at the WordPress Codex, and all the low-hanging functions (which would be easy for a beginner to understand) require either the post ID or the ID of the author to make that happen. That, of course, does not fulfill the design.

Any help on how to find and use these things would be greatly appreciated. :)

  • 写回答

1条回答 默认 最新

  • dtjbcda841554 2019-03-08 10:47
    关注

    There is a post_updated hook in the Wordpress API that you could attach this function to with add_action.

    https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

    You could compare before and after, and if the values of a particular field differ, it would trigger an update. I suppose the easiest would be checking the datetime, alternatively, you could have it check the title if minor spelling/grammar changes don't merit an email.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)