Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the commenting system to all users to add a private note that only they can see to posts and I am using the wordpress comments system to do it. I wrote a function to call the logged in user comments in a shortcode to display on their profile page. I can get this to work without passing it through a variable but it always shows up at the top of the page. I can't seem to call these variables inside the function. All I get is a blank page. Please take a look and tell me what I am doing wrong.

function notes_by_comments() {

global $wpdb;

$current_user = wp_get_current_user();

$note_content = '<h5 style="border-bottom: 1px solid #DADADA; padding-bottom: 7px;padding-top: 25px;"><strong>Notes</strong></h5>';

$commentQuery = "SELECT * from $wpdb->comments WHERE user_id=$current_user->ID ORDER BY   comment_date DESC LIMIT 0 ,5";

$comments = $wpdb->get_results($commentQuery);
foreach ($comments as $comment) {

$note_content. = '<li><b>Note for</b> <a href="'. get_permalink($comment- >comment_post_ID). '#comment-'.$comment->comment_ID .'">';
$note_content. = get_the_title($comment->comment_post_ID) . '</a> -'; 

 if(get_post_type( $comment->comment_post_ID ) === 'for_sale_listings'){
 $note_content. =  "<b>Sale Listing</b>";
} elseif (get_post_type( $comment->comment_post_ID ) === 'rental_listings'){
$note_content. = "<b>Rental Listing</b>";
} elseif (get_post_type( $comment->comment_post_ID ) === 'roomate_share'){
$note_content. = "<b>Roommate Share</b>";
}

$note_content. = '<p>' . $comment->comment_content . '</p></li>';

return $note_content;

} 
}

add_shortcode('notes', 'notes_by_comments'); 
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Could be because you are using the concatenating assignment operator incorrectly:

$note_content. =

should be

$note_content .=

http://php.net/manual/en/language.operators.string.php

share|improve this answer
    
I accepted this answers because it answered my question very simply put. I guess I needed more coffee but after I saw this I couldn't believe that I missed it. Thank you. –  Jacob Rambo Mar 1 '13 at 14:42
    
Man, if I had a penny for every time time I made a simple syntax error... :) –  jfacemyer Mar 4 '13 at 3:34
add comment

I reformat your code a bit. Please always use $wpdb->prepare() for SQL queries. The switch-case is a bit better solution than this if-elseif-elseif-elseif-else-tadada.

I have not tested the code, I did not have a copy of your database, but you put the return inside the foreach loop. Maybe this was the error.

<?php
function notes_by_comments() {

    global $wpdb;

    $current_user = wp_get_current_user();
    $note_content = '<h5 style="border-bottom: 1px solid #DADADA; padding-bottom: 7px;padding-top: 25px;"><strong>Notes</strong></h5>';

    $commentQuery = $wpdb->prepare(
        'SELECT * from $wpdb->comments WHERE user_id=%d ORDER BY %s DESC LIMIT 0 ,5',
        $current_user->ID,
        comment_date
    );

    $comments = $wpdb->get_results( $commentQuery );

    foreach ( $comments as $comment ) {
        $note_content = '';

        $note_content .= sprintf( '<li><b>Note for</b> <a href="%s#comment-%d">',
            get_permalink( $comment->comment_post_ID ),
            $comment->comment_ID
        ); 

        $note_content .= get_the_title( $comment->comment_post_ID ) . '</a> -'; 

        switch ( get_post_type( $comment->comment_post_ID ) ) {

            case 'for_sale_listings':
                $note_content .=  "<b>Sale Listing</b>";
                break;

            case 'rental_listings':
                $note_content .= "<b>Rental Listing</b>";
                break;

            case 'roomate_share':
                $note_content .= "<b>Roommate Share</b>";
                break;

            default:
                $note_content .= '';
        }

        $note_content .= sprintf( '<p>%s</p></li>', $comment->comment_content );

    }

    return $note_content;

}

add_shortcode( 'notes', 'notes_by_comments' ); 

A well formated code with indents is always usefull to find errors.

share|improve this answer
    
I understand that using $wpdb->prepare() is best practice but this is for displaying some comments so I don't think I'll be vulnerable to sql injection. Using switch vs. if statements is not the question here and according to the php manual there is no difference, can be left to preference and is a matter of opinion. But I +1 this for an attempt to give a more complex answer and works just the same. –  Jacob Rambo Mar 1 '13 at 14:41
    
"A bit better solution" not in the meaning of faster or more efficient, it's better in the meaning of well formatted code. The same with $wpdb-Prepare(), it's not only the security. Compare user_id=current_user-ID with user_id=$current_user->ID. The first one have two errors, but to find them could be very hard, especially in a very long SQL statement. If you will use prepare() PHP will throw a lot of errors and you can find them very fast. Try it out, you will see the difference. As you can see, I also use printf() / spritf() as often as I can. The same reason, finding errors faster –  Ralf912 Mar 1 '13 at 15:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.