Forum Replies Created

Viewing 15 replies - 46 through 60 (of 63 total)
  • Thread Starter edow

    (@edow)

    Thanks, I had to change it to $score = count($likes) - count($unlikes); and now it works great!

    Later I have to figure out how to show the score on the category page, tag page and homepage, but for now it works great. Again, thanks a lot!

    The code which is on top of the page is now:

    <?php
    		$comments = get_comments( array('post_id' => get_the_ID(),) );
    			$likes = array();
    			$unlikes = array();
    			foreach( $comments as $comment ) {
    				if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    					else $likes[] = $comment;
    			}
    			$score = count($likes) - count($unlikes);
    			$totalvotes = count($likes) + count($unlikes);
    			if ($score > 0 )
    				{
    				echo '<span class="plusscore">+'.$score.'</span><br>';
    				}
    			elseif ($score == 0 )
    				{
    				echo '<span class="zeroscore">'.$score.'</span><br>';
    				}
    			elseif ($score < 0 )
    				{
    				echo '<span class="minusscore">'.$score.'</span><br>';
    				}
    			if ($score == 1 )
    				{
    				echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stem</a></span>';
    				}
    			elseif ($score == -1 )
    				{
    				echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stem</a></span>';
    				}
    			elseif ($score == 0 )
    				{
    				echo '<span class="totalvotes"><a href="#comments">Nu stemmen!</a></span>';
    				}
    			else
    				{
    				echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stemmen</a></span>';
    				}
    		?>

    Thread Starter edow

    (@edow)

    I have a question about moving the votes. Instead of moving I would like to duplicate the counter to the top of the page. So, I want the counter as where it is now and want another on top of the page. I used this code:

    <?php
    			foreach( $comments as $comment ) {
    				if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    					else $likes[] = $comment;
    			}
    			echo '<h3>'.count($likes).' people like this</h3>';
    			echo '<h3>'.count($unlikes).' people don't like this</h3>';
    			?>

    But that isn’t working. This is the first loop you’re talking about right? This code displays 0 people like this, even when there are likes.

    Which code do I have to use to have another counter on the same page. It would be better if the top counter calculates the difference. So if there’re 10 likes and 5 dislikes the counter say +5 and if there’re 2 likes and 4 dislikes is says -2.

    Thread Starter edow

    (@edow)

    Hi Ashok,

    Thank you for your reply. If I deactivate the “Simple Local Avatars” plugin then it only shows the default avatar for every user (this one: http://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=50).

    If I activate the plugin all the avatars have the same local avatar as me (the admin). Even the not logged in users who give a comment. But this is only when the admin gave a comment. If not, the default avatars shows.

    The only code besides my already mentioned code is this in my function file:

    <?php
    /* Prevent more than one %like */
    add_filter('pre_comment_approved','aigoo_one_vote',10 ,2);
    function aigoo_one_vote($approved, $commentdata) {
    	global $wpdb;
    	extract($commentdata, EXTR_SKIP);
    	// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
    	$dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", wp_unslash( $comment_post_ID ), wp_unslash( $comment_parent ), wp_unslash( $comment_author ) );
    	if ( $comment_author_email )
    		$dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $comment_author_email ) );
    	$dupe .= $wpdb->prepare( ") AND comment_content LIKE %s LIMIT 1", '%like' );
    	if ( $wpdb->get_var($dupe) ) {
    		if ( defined('DOING_AJAX') )
    			die( __('Je hebt hier al op gestemd') );
    		wp_die( __('Je hebt hier al op gestemd') );
    	}
    }
    ?>

    But I don’t think this has something to do with it.

    As far as I know I don’t have any filter applied. Better WP Security, Simple Local Avatars and Theme Check are all the plugins I use. I made the theme myself and with almost the exact same code it worked on another domain.

    If I only use <?php echo get_avatar( $id_or_email, $size, $default, $alt ); ?> it also only shows the default avatar instead of the local avatar. The reason why I use get_avatar_url id that I want to add a class and a title tag to the image.

    Thread Starter edow

    (@edow)

    Unfortunately that isn’t working either. It also gives all the voters the same avatar after a logged in user voted.

    This is the corresponding function code:

    function get_avatar_url($get_avatar){
        preg_match("/src='(.*?)'/i", $get_avatar, $matches);
        return $matches[1];
    }

    Maybe there is some error in the function code. I’m happy to open a new topic if you’re done helping me 😉

    Thread Starter edow

    (@edow)

    Thanks for your explanation. Soon I’ll look after moving the counters.

    I’ve a small problem and I don’t know if it is because of the code or something else. I use this line of code to show the images:
    <img src=\"".get_avatar_url(get_avatar( $comment, 50 ))."\" class=\"tooltip\" title=\"$like->comment_author\">

    Normally $comment should be $id_or_email, but that does nothing. If I use $comment to get the comment author avatar (I also use the plugin Local Avatar or something) every author gets the same avatar (the one who voted when he was logged in), also when they’re not logged in. Perhaps I’m doing something wrong. When there’s no uploaded avatar from someone he shows the standard avatar as it should be.

    I’ll post my final code here, because maybe it’s useful for others.

    Code to get the comment form and show the avatars:

    <div class="post-commentform">
    			<?php comments_template(); ?>
    		</div>
    
    		<div class="post-comments">
    			<div class="upvoters" id="votes">
    			<?php $comments = get_comments( array('post_id' => get_the_ID(),) );
    			$likes = array();
    			$unlikes = array();
    			foreach( $comments as $comment ) {
    				if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    					else $likes[] = $comment;
    			}
    			echo '<h3>'.count($likes).' bezoekers vinden dit leuk</h3>';
    			echo '<div class="upvoter-img">';
    			foreach( $likes as $like )
    				echo("<img src=\"".get_avatar_url(get_avatar( $comment, 50 ))."\" class=\"tooltip\" title=\"$like->comment_author\">");
    			echo '</div>';
    			?>
    			</div>
    
    			<div class="downvoters">
    			<?php
    			echo '<h3>'.count($unlikes).' bezoekers vinden dit niet leuk</h3>';
    			echo '<div class="downvoter-img">';
    			foreach( $unlikes as $unlike )
    				echo("<img src=\"".get_avatar_url(get_avatar( $comment, 50 ))."\" class=\"tooltip\" title=\"$unlike->comment_author\">");
    			echo '</div>';
    			?>
    			</div>
    
    			<div class="clearBoth"></div>
    		</div>

    Code in comments.php:

    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
    			<fieldset>
    				<div class="comment">
    					<span class="radiobuttonleft"><input type="radio" name="comment" id="comment" value="like" data-related-item="commentinput">Ik vindt dit leuk!</span>
    					<span class="radiobuttonright"><input type="radio" name="comment" id="comment" value="unlike" data-related-item="commentinput">Ik vindt dit niet zo leuk...</span>
    				</div>
    				<div class="fields">
    					<?php $current_user = wp_get_current_user(); ?>
    					<?php
    					if ( is_user_logged_in() ) {
    						echo '';
    					} else {
    						echo '<input type="text" class="commentinput" name="author" id="author" placeholder="Naam of schuilnaam :)" value="'.$current_user->user_login.'" />
    							  <input type="text" class="commentinput" name="email" id="email" placeholder="E-mail" value="'.$current_user->user_email.'" />
    							  <input type="submit" class="commentsubmit" value="Geef je mening!" />
    							  <div class="labelemail">We willen even weten of je geen robot bent</div>';
    					}
    					?>
    				</div>
    
    				<?php comment_id_fields(); ?>
    				<?php do_action('comment_form', $post->ID); ?>
    			</fieldset>
    		</form>

    Then it looks like this, beware, it’s Dutch 😉
    Without replies: http://oi59.tinypic.com/2nkt9g7.jpg
    With replies: http://oi61.tinypic.com/2ev9q49.jpg
    When you give a comment: http://oi62.tinypic.com/1zx39j8.jpg

    So there’s only the little problem with the avatars. Thanks again for your help! I’m super happy with it!

    Thread Starter edow

    (@edow)

    And what do I have to do to show the counters outside the code? I’m thinking of showing them also at the top of the page as well as how I’m showing them now.

    Thread Starter edow

    (@edow)

    Great! Now I’ve the code like this:

    <div class="upvoters" id="votes">
    		<?php $comments = get_comments( array('post_id' => get_the_ID(),) );
    		$likes = array();
    		$unlikes = array();
    		foreach( $comments as $comment ) {
    			if('unlike' == $comment->comment_content ) $unlikes[] = $comment;
    				else $likes[] = $comment;
    		}
    		echo '<h2>'.count($likes).' bezoekers vinden dit leuk</h2>';
    		echo '<div class="upvoter-img">';
    		foreach( $likes as $like )
    			echo("<img src=\"/wp-content/themes/aigooNL/images/voter.png\" class=\"tooltip\" title=\"$like->comment_author\">");
    		echo '</div>';
    		?>
    		</div>
    
    		<div class="downvoters">
    		<?php
    		echo '<h2>'.count($unlikes).' bezoekers vinden dit niet leuk</h2>';
    		echo '<div class="downvoter-img">';
    		foreach( $unlikes as $unlike )
    			echo("<img src=\"/wp-content/themes/aigooNL/images/voter.png\" class=\"tooltip\" title=\"$unlike->comment_author\">");
    		echo '</div>';
    		?>
    		</div>

    It’s working great. This is the screenshot (in Dutch) how it looks like: http://oi57.tinypic.com/30hv7g6.jpg
    I guess the code is still correct with my changes?

    I have to design it now, but the functions are working great! Thanks again.

    WordPress has it build in that you may not comment the same text twice. I was forgotten to mention it, but now visitors can comment “like” and “unlike”. Is there a simple solution that prevents visitors from voting like and unlike on the same post? WordPress prevents from like the same post twice or unlike it twice.

    Thread Starter edow

    (@edow)

    Super awesome! Works great immediately. I hardly dare to ask, but is there with this code the possibility to show a counter for the upvotes and downvotes? If not, it doesn’t matter. This is super awesome already! Thank you so much!

    Thread Starter edow

    (@edow)

    Yes you’re right, the entire comment text is “like” or “unlike”. There is no text field, only the radio buttons and the input field for your name and email address.

    I want a list with people who voted “like” and a seperate list of people who voted “unlike”. I have two lists like this:

    `<div class=”upvoters” id=”votes”>
    <h2>We like this!</h2>
    <div class=”voter-img”>
    <a href=”#respond”><img src=”/wp-content/themes/aigooNL/images/upvote.png” class=”upvote”></a>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”John”>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”Edo”>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”Peter”>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”Joey”>
    </div>
    </div>

    <div class=”downvoters”>
    <h2>We, not so much…</h2>
    <div class=”voter-img”>
    <a href=”#respond”><img src=”/wp-content/themes/aigooNL/images/downvote.png” class=”downvote”></a>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”Chris”>
    <img src=”/wp-content/themes/aigooNL/images/voter.png” class=”tooltip” title=”Michael”>
    </div>
    </div>`

    There are two classes: upvoters and downvoters. These lists reflects the votes.

    I have a small website. Sure, I want the site to grow, but right now I’m thrilled if someone gave a review 😉
    Hopefully the site will grow and I get dozens “likes/unlikes” on each post, but certainly not thousands.

    I understand if you don’t have the time to help me with this, but I would really appreciate it if you or someone else could help me a little bit more. I understand how to design and know about html, but I don’t know much about PHP or other coding languages.

    I’m not in a hurry. If you need more information please let me know. Thanks already for any help you can give.

    Thread Starter edow

    (@edow)

    Hi bcworkz,

    No problem and thanks for your reply!

    These are the input fields for the like/dislike buttons in the comment section:

    <div class="comment">
    					<label class="commentformtitle">Do you like <?php the_title(); ?>?</label>
    					<span class="radiobuttonleft"><input type="radio" name="comment" id="comment" value="like">I like this!</span>
    					<span class="radiobuttonright"><input type="radio" name="comment" id="comment" value="unlike">I don't like this...</span>
    					<input type="submit" class="commentsubmit" value="Submit review" />
    				</div>

    My coding abilities aren’t so well. I’m not sure how to achieve this. Is it possible that you help me a little bit further with this?

    The comment form itself works good. Guests have to input their name, email and choose like or unlike. After submit the comment text is “like” or “unlike” so that works great.

    I realy don’t know how to handle this. In the database the comment text only saves “like” or “unlike”, so perhaps with PHP I can sort the likes and unlikes based on the comment-text? Hopefully you can help me a little bit further.

    Thread Starter edow

    (@edow)

    I’ve still my question. Is it possible to sort the comments in two lists? Comments can only contain the word “like” or “unlike”. Is it possible, based on these words, to sort these comments in two seperate lists? One list with the “likes” and the other one with the “unlike”. In this list only the avatar should be shown. Hopefully anyone can help me with this.

    Thread Starter edow

    (@edow)

    Now it works. In the WordPress settings I changed the option that the user must have a previous approved review. It looks like it works well.

    The next problem is that I want to sort the people in two lists. One list with people who choose “LIke” and a list of people (avatars shown only) who choose “Unlike”.

    Thread Starter edow

    (@edow)

    I appreciate your quick replies.

    I’ve managed to remove the text field, because (as you said) it’s hardcoded.

    This is my code now:

    <div class="comment">
    					<label>Review: (verplicht)</label>
    					<input type="radio" name="comment" id="comment" value="like">Like<br>
    					<input type="radio" name="comment" id="comment" value="unlike">Unlike
    					<br />
    					<input type="submit" class="commentsubmit" value="Review plaatsen" />
    					<?php if ( is_user_logged_in() ) { echo ''; } else { echo '<span> - Het is niet verplicht, maar je kan een <a href="http://aigoo.nl/wp-login.php?action=register">gratis profiel aanmaken</a> of <a href="'.wp_login_url( get_permalink() ).'">inloggen</a></span>'; };?>
    				</div>

    I also made radio buttons as you can see. The problem now is that it says “Your comment is awaiting moderation.” and I can only place one review. Maybe because it’s from the same IP.

    Thread Starter edow

    (@edow)

    My comment_form tag looks like this:

    <?php do_action('comment_form', $post->ID); ?>

    I’m not sure how to use your line of code, because the form doesn’t show anymore after I use your code within my code. I think I’m doing something wrong.

    If it will work, doesn’t the database get full of code of tags (?) with whitespace? I’m not sure how this works, but I suppose the space from the textfield is saved in the database?

    Sorry for my bad English.

    Thread Starter edow

    (@edow)

    Thank you for your response. Tomorrow I’ll take a look at this. Thanks in advance!

Viewing 15 replies - 46 through 60 (of 63 total)