• Resolved teejo

    (@teejo)


    I’m currently building a notification system for when an event is added/changed.
    For this I use the ’em_event_save’ hook. This works all fine and good but I have a problem with differentiating between a new event and a changed one.

    I was planning on using the ‘event_date_modified’ field in the ’em_events’ table. (if the field is NULL, it would be the first time published).

    But the problem here is that for a drafted event, then later published, this field
    (obviously) is not NULL. Is there a specific hook or perhaps a different field I can check to make sure an event is published for the first time vs a changed event?

    Hope I formulated my question clearly, if anything is unclear let me know!

    Kind regards,
    Teejo

    P.S.
    Love the plugin by the way, gerat functinallity and easy to use! Perfect for what I’m using it for ^^

    • This topic was modified 6 years, 9 months ago by teejo. Reason: show some love
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,
    EM Users here too.

    I would rather use the native WP action “post_updated”. That was designed for tracking post changes and gives you a better insight.
    https://codex.ww.wp.xz.cn/Plugin_API/Action_Reference/post_updated

    For example:
    if( $post_before->post_status != 'publish' ) // Was draft

    You can then also very easily hook into the correct EM_Event Object without having to (re)query the database:
    $EM_Event = new EM_Event( $post_ID, 'post_id' );

    Thread Starter teejo

    (@teejo)

    Ah that looks perfect for what I’m trying to achieve. Wasn’t aware of the existance of ‘post_updated’. Thanks!

    Will have a look into it and will post back when I get it working.

    Great!
    Here’s a little something to get you started:

    function em_custom_notify_new_events( $id, $after, $before ) {
    	if( $after->post_type != 'event' ) {
    		return; // This is not an EM Event, so we'll leave it alone.
    	}
    	
    	if( $before->post_status != 'publish' && $after->post_status === 'publish' ) {
    		// Do stuff for the newly published event.
    		$EM_Event = new EM_Event( $id, 'post_id' );
    	}
    }
    add_action( 'post_updated', 'em_custom_notify_new_events', 10, 3 );
    Thread Starter teejo

    (@teejo)

    Alright. Got it working now after some pulling my hair out.
    I used your snipped but encountered some strange behaviour that notifications wouldn’t be added to the database.

    In the end I figured out that bp_notifications_add_notification() has an ‘allow_duplicate’ argument which defaults to false.

    Learned something new today :’)

    Thank you for your help!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Hook for event publication’ is closed to new replies.