dimanche 15 novembre 2015

triggered add_action one time

I want when a user add a comment, it will be add in the same time in another page. For that I use this code :

<?php
class PluginFonctionalities {

    public function __construct() {
        add_action( 'comment_post', array( $this, 'show_message_function' ), 10, 2 );
    }

    public function show_message_function( $comment_ID, $comment_approved ) {
        if( 1 === $comment_approved ){

            $comment= get_comment($comment_ID );
            $the_post_id = 7;

            $commentdata = array(
                'comment_post_ID' => $the_post_id,
                'comment_author' => $comment->comment_author,
                'comment_author_email' => $comment->comment_author_email,
                'comment_author_url' => $comment->comment_author_url,
                'comment_content' => $comment->comment_content,
                'comment_type' => $comment->comment_type,
                'comment_parent' => 0, 
                'user_id' => $comment->user_id, 
            );
            $comment_id_new = wp_new_comment( $commentdata );

        }
    }
}

$pf = new PluginFonctionalities();
?>

The Happy thing: The comment is added in two pages

The bad thing that's the comment added many time (loop) in the page 7, because when wp_new_comment executed, it triggered the action hook then ....

How can I resolve my problem !? any ideas ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire