简体   繁体   中英

Find unknown post ID in Wordpress programmatically

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. :)

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM