Documentation > Developers > Filter Examples > New Comment Notification

New Comment Notification

Available with picu 1.7.0.

Using the following snippet will send an email to the collection author every time a new comment is made for an individual image:

/**
 * Send new comment notification
 */
function my_picu_after_saving_selection( $save_data, $old_data, $post_id, $ident ) {

    $post = get_post( $post_id );
    $collection_link = get_the_permalink( $post_id );
    $changes = [];

    // Get index of the image
    $collection_images = get_post_meta( $post_id, '_picu_collection_gallery_ids', true );
    $images = explode( ',', $collection_images );

    if ( ! empty( $save_data['markers'] ) ) {
        // 1. Iterate through all markers/comments, compare on an image by image basis
        foreach( $save_data['markers'] as $image_id => $image_markers ) {

            // 2. Iterate through all the markers/comments per image
            foreach( $image_markers as $marker_id => $marker_value ) {

                // Make sure that the comment is new
                if ( empty( $old_data['markers'][$image_id][$marker_id] ) ) {

                    // Get image position and create direct link to the image
                    $index = array_search( substr( $image_id, 3 ), $images) + 1;
                    if ( $ident ) {
                        $link = $collection_link . '?ident=' . $ident . '#' . $index;
                    }
                    else {
                        $link = $collection_link . '#' . $index;
                    }
                    $image =  basename( get_attached_file( substr( $image_id, 3 ) ) );

                    // Save the comment into variable
                    $comment = $marker_value['comment'];
                }
            }
        }
    }

    // If there is a comment, send email
    if ( ! empty( $comment ) ) {

        $mail = new Picu_Emails( $post_id );

        $content = 'New comment in collection <strong>' . get_the_title( $post_id ) . '</strong> for image <strong>' . $image . '</strong>:<br /><br />';
        $content .= $comment;

        $mail_parts = [
            [
                'type' => 'text',
                'text' => $content
            ],
            [
                'type' => 'button',
                'text' => 'Go to image',
                'url' => $link,
            ]
        ];

        $to_address = get_the_author_meta( 'user_email', $post->post_author );

        $options = get_option( 'picu_settings' );
        if ( ! empty( $options['notification_email'] ) ) {
            $to_address = sanitize_email( $options['notification_email'] );
        }

        $mail->__set( 'mail_context', 'new_comment_notification' );
        $mail->__set( 'to_address', $to_address );
        $mail->__set( 'subject', 'New Comment' );
        $mail->__set( 'mail_parts', $mail_parts );

        $mail->send();
    }
}

add_action( 'picu_after_saving_selection', 'my_picu_after_saving_selection', 10, 4 );Code language: PHP (php)

Need help?

If you couldn’t find what you were looking for and need more assistance, please get in touch with us directly and we’re happy to help.