0

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'); 

2 Answers 2

1

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

2
  • 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. Commented Mar 1, 2013 at 14:42
  • Man, if I had a penny for every time time I made a simple syntax error... :)
    – jfacemyer
    Commented Mar 4, 2013 at 3:34
1

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.

2
  • 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. Commented Mar 1, 2013 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
    Commented Mar 1, 2013 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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