1

I want to add an extra classname to my comment template (the div wrapper before the post itself) which is stated in functions.php. The classname in functions.php is an extra field in my users profile section and I want to get that string and add it as an extra classname in my comment template.

The thing I want to add: $cityofuser->user_city

The question is: how do I do that? If I use $classes[] = 'comment-author-' . sanitize_html_class(echo $cityofuser->user_city); in a template, it won't work. I get errors like syntax error, unexpected 'echo'

Edit:

In my register form, people can add info in a new field, stating the city they live in. I want to get this field's output, make it a new classname in the comment template and I want to change the background of the users post based on this classname. So the custom field echo $cityofuser->user_city that I have made (in functions.php) and added to my register pages, is something I want to use in my whole site.

I want to add the new class name in here:<div id="comment-5" class="comment byuser comment-author-admin even thread-odd thread-alt depth-1">

And after this is done, also on the posts. That must be something like get_post_author?=cityofuser=Montreal or something, but I don't have a clue how to do that.

  • It's not clear to me where you want to add this extra classname. For example you talk about some post and not a comment: "the div wrapper before the post itself". Can you show us an HTML example? – birgire Dec 6 '14 at 10:35
  • I edited my post. I hope it is clear now. – Siyah Dec 6 '14 at 10:44
3

You should consider hooking into the comment_class() and post_class() filters, if your theme supports it.

Using the comment_class filter:

We can add the following filter:

/**
 * Add a custom comment class, based on a given comment author's user meta field.
 *
 * @see http://wordpress.stackexchange.com/a/170443/26350
 */

add_filter( 'comment_class', function( $classes, $class, $comment_id, $post_id ) {

    // Custom user meta key:
    $key = 'city';           // <-- Edit this to your needs!

    // Fetch the comment object:
    $comment = get_comment( $comment_id );

    // Check if the comment author is a registered user:
    if ( ! is_null( $comment ) && $comment->user_id > 0 )
    {
        // Check for the custom user meta:
        if( '' !== ( $value = get_user_meta( $comment->user_id, $key, true ) ) )
            $classes[] = sanitize_html_class( $value );
    }

    return $classes;
}, 10, 4 );

Output example:

<li class="comment byuser comment-author-someuser bypostauthor 
           odd alt depth-2 reykjavik" id="li-comment-78">
    <article id="comment-78" class="comment">

where the reykjavik has been added as the city user-meta, for the given comment author.

Using the post_class filter:

Similarly for the post_class filter, we can use:

/**
 * Add a custom post class, based on a given post author's user meta field.
 *
 * @see http://wordpress.stackexchange.com/a/170443/26350
 */

add_filter( 'post_class', function( $classes, $class, $post_id ) {

    // Custom user meta key:
    $key = 'city';           // <-- Edit this to your needs!

    // Fetch the comment object:
    $post = get_post( $post_id );

    if( ! is_null( $post ) && $post->post_author > 0 )
    {
        // Check for the custom user meta:
        if( '' !== ( $value = get_user_meta( $post->post_author, $key, true ) ) )
            $classes[] = sanitize_html_class( $value );

    }

    return $classes;
}, 10, 3 );

Here's a shorter version that works within the loop:

/**
 * Add a custom post class, based on a given post author's user meta field.
 *
 * @see http://wordpress.stackexchange.com/a/170443/26350
 */

add_filter( 'post_class', function( $classes, $class, $post_id ) {

    // Custom user meta key:
    $key = 'city';           // <-- Edit this to your needs!

    // Check for the custom user meta:
    if( '' !== ( $value = get_the_author_meta( $key ) ) )
        $classes[] = sanitize_html_class( $value );

    return $classes;
});

Output example:

<article id="post-1" 
         class="post-1 post type-post status-publish format-standard 
                hentry category-uncategorized reykjavik">

where the reykjavik has been added as the last post class, based on our filter above.

  • Almost there! It now shoes me plain 'city' instead of the value that is put in by the users. How can I make that one dynamic? – Siyah Dec 6 '14 at 11:17
  • Please check out the updated answer but it's untested. – birgire Dec 6 '14 at 11:28
  • This works, but not for the postauthor. Can you get that working too? – Siyah Dec 6 '14 at 11:31
  • 1
    I can test it after 1 or 2 hours. – birgire Dec 6 '14 at 11:45
  • No problem. I will give you an upvote. You deserved this. PS: what do I have to change to get the post class also dynamically working? And can I use checkboxes / dropdowns / radio buttons too as the inputs? So instead of text, checkbox or whatsoever? – Siyah Dec 6 '14 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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