WordPress.org

Ready to get started?Download WordPress

Forums

It's so close but far: comment meta handling in WordPress (1 post)

  1. fejanos
    Member
    Posted 2 years ago #

    Well I'm trying here very hard. I'm feeling that I'm so close to the solution but as always happens I can't figure out what's wrong.
    Let me detail everything here
    The custom comment form field where user submits label (etiqueta means label in Spanish) : comments.php
    ...<p><?php do_action( 'et_comment_form_etiqueta' ); ?></p>...
    Calls function hook in additional_functions.php and rest of the functions that concerns this particular custom comment field (label=etiqueta)
    ...

    <?php }
    }
    add_action('et_comment_form_etiqueta','et_choose_etiqueta');
    function et_choose_etiqueta(){
    	#adds 'Ponle Precio' to the comment form ( frontend )
    	if ( !is_page() ) {
    <p>Tu etiqueta:<input type="text" name="et_etiqueta" id="precio" value="aqui va tu etiqueta" size="22" tabindex="3" /></p>
    }
     }
    add_action('comment_post','et_add_etiqueta_commentmeta', 10, 2);
    function et_add_etiqueta_commentmeta( $comment_id, $comment_approved ){
    	#when user adds a comment, check if it's approved
    
    	$comment_etiqueta = ( isset($_POST['et_etiqueta']) ) ? $_POST['et_etiqueta'] : 0;
    	add_comment_meta($comment_id,'et_comment_etiqueta',$comment_etiqueta);
    	if ( $comment_approved == 1 ) {
    		$comment_info = get_comment($comment_id);
    		et_update_post_user_etiqueta( $comment_info->comment_post_ID );
    	}
    }
    add_action('comment_post','et_add_etiqueta_commentmeta', 10, 2);
    function et_add_etiqueta_commentmeta( $comment_id, $comment_approved ){
    	#when user adds a comment, check if it's approved
    
    	$comment_etiqueta = ( isset($_POST['et_etiqueta']) ) ? $_POST['et_etiqueta'] : 0;
    	add_comment_meta($comment_id,'et_comment_etiqueta',$comment_etiqueta);
    	if ( $comment_approved == 1 ) {
    		$comment_info = get_comment($comment_id);
    		et_update_post_user_etiqueta( $comment_info->comment_post_ID );
    	}
    }
    add_action('et-comment-meta-etiqueta','et_show_comment_etiqueta');
    function et_show_comment_etiqueta( $comment_id ){
    	#displays user comment etiqueta on single post page ( frontend )
    
    	$user_comment_etiqueta = get_comment_meta($comment_id,'et_comment_etiqueta',true) ? get_comment_meta($comment_id,'et_comment_etiqueta',true) : 0;
    	if ( $user_comment_etiqueta <> 0 ) { ?>
    
                                        <?php echo $user_comment_etiqueta; ?>
    
    	<?php }
    }
    function et_get_top_etiqueta($top_etiqueta) {
    	global $wpdb;
    	return $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) as countof FROM $wpdb->commentmeta WHERE meta_key = 'et_comment_etiqueta' ORDER BY countof DESC LIMIT 1", $post_id));
    }
    function et_get_post_user_etiqueta( $post_id ){
    	#gets under process user (comments) added etiqueta for the post
            #this function adds the most popular etiqueta (label) to the post. (it needs to be calculated)
    	$approved_comments = et_get_approved_comments( $post_id );
    	if ( empty($approved_comments) ) return 0;
    	$user_etiqueta = 0;
    	$approved_comments_number = count($approved_comments);
    	foreach ( $approved_comments as $comment ) {
    		$comment_etiqueta = get_comment_meta($comment->comment_ID,'et_comment_etiqueta',true) ? get_comment_meta($comment->comment_ID,'et_comment_etiqueta',true) : 0;
    		if ( $comment_etiqueta == 0 ) $approved_comments_number--;
    
    		$user_etiqueta += $comment_etiqueta;
    	}
            //we need a variable where the most times submitted label is stored.
            $result = et_get_top_etiqueta( $top_etiqueta );
    	# save user rating to the post meta
    
    	if ( !get_post_meta($post_id,'_et_inreview_comments_etiqueta',true) ) update_post_meta($post_id,'_et_inreview_comments_etiqueta',$result);
    
    	return $result;
    }
    function et_update_post_user_etiqueta( $post_id ){
    	#update user added etiqueta for the post
    	$new_comments_etiqueta = et_get_post_user_etiqueta( $post_id );
    
    	if ( get_post_meta($post_id,'_et_inreview_comments_etiqueta',true) <> $new_comments_etiqueta )
    		update_post_meta($post_id,'_et_inreview_comments_etiqueta',$new_comments_etiqueta);
    }
    function et_get_approved_comments($post_id) {
    	global $wpdb;
    	return $wpdb->get_results($wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
    }
    ?>

    And this is the where the label (etiqueta) is supposed to appear in the comment box next to each comment submitted by users but IT DOESN'T. The label submitted by each user gets inserted in the database correctly but it just doesn't get displayed next to the comment in the comment box.
    <b>Etiqueta:</b> <?php do_action('et-comment-meta-etiqueta', get_comment_ID());?>
    In order to display the most frequently submitted label (etiqueta) next to the post I used this function which can also be found in additional_functions.php above IT DOESNT WORK EITHER!!
    <?php add_action('et-comment-meta-etiqueta','et_show_comment_etiqueta'); function et_show_comment_etiqueta( $comment_id ){ #displays user comment etiqueta on single post page ( frontend ) $user_comment_etiqueta = get_comment_meta($comment_id,'et_comment_etiqueta',true) ? get_comment_meta($comment_id,'et_comment_etiqueta',true) : 0; if ( $user_comment_etiqueta <> 0 ) { echo $user_comment_etiqueta; } } function et_get_top_etiqueta($top_etiqueta) { global $wpdb; return $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) as countof FROM $wpdb->commentmeta WHERE meta_key = 'et_comment_etiqueta' ORDER BY countof DESC LIMIT 1", $post_id)); }?>
    These are the tables that uses the theme:
    -wp_commentmeta which has 4 columns: meta_id; comment_id; meta_key; meta_value
    -wp_postmeta which has 4 columns: meta_id; post_id; meta_key; meta_value

Topic Closed

This topic has been closed to new replies.

About this Topic