0

I am brand new to WordPress plugin development and have a few questions, mostly about best practices but also some specifics.

Essentially I just want to trigger an action (invoking a PHP class and calling a method) whenever the following happens:

  • A post is published
  • A page is published
  • A comment is added

This will be site-wide, so no need to key in anything specific just yet. I know in the past people had used the add_action() function but that is now deprecated so any insight on best practices and how to accomplish the above would be much appreciated. To get started I am using: https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate just to ensure I have proper structure, etc.

2

Below I've included two hooks you could use. There may be better hooks depending on what you're trying to accomplish exactly but this is what you've asked for. At the bottom I've listed some helpful resources:

Publish Post Hook

http://codex.wordpress.org/Plugin_API/Action_Reference/publish_post

function published_post( $ID, $post ) {
    if( $post->post_type == 'post' ) {
        ...
    }

    if( $post->post_type == 'page' ) {
        ...
    }
}
add_action( 'publish_post', 'published_post', 10, 2 );

Comment Post Hook

function maybe_published_comment( $ID, $approval_bool ) {
    ...
}
add_action( 'comment_post', maybe_published_comment, 10, 2 );

Resources

  1. Action Reference
  2. Developer Reference
    1. WP_Publish_Post()
    2. Comment Post Hook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.