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.

3
  • 1
    add_action is most definitely not deprecated and your best bet to accomplish what you need.
    – Howdy_McGee
    Commented Dec 23, 2014 at 20:35
  • @Howdy_McGee I must have read something wrong then, glad to hear it isn't deprecated. Any input on writing a simple plugin that can hook into those three actions I mention in the post?
    – niczak
    Commented Dec 23, 2014 at 20:37
  • Ahh and see notes here as to why the deprecation was confusing, I must have linked to one of the other resources: codex.wordpress.org/Plugin_API/Action_Reference/publish_post
    – niczak
    Commented Dec 23, 2014 at 20:46

1 Answer 1

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 and acknowledge you have read our privacy policy.

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