0

I'm developing my new theme using Hybrid core framework and trying to add my class item on hybrid_post_attributes generated class using filter with this code.

add_filter( 'hybrid_post_attributes', 'my_post_atributes');
function my_post_atributes($attributes){
return  $attributes['class'] = substr_replace($attributes['class'], ' item', -1);
}

I want my 'item' class to be added as the last class from 'hybrid_post_attributes' classes. Here's hybrid_post_attributes function.

function hybrid_post_attributes() {

    $attributes = array();
    $output     = '';

    $attributes['id']    = 'post-' . get_the_ID();
    $attributes['class'] = join( ' ', hybrid_get_post_class() );
    $attributes = apply_atomic( 'post_attributes', $attributes );

    foreach( $attributes as $attr => $value )
        $output .= " {$attr}='{$value}'";

    echo $output;
}

And here's hybrid_get_post_class function.

function hybrid_get_post_class( $class = '', $post_id = null ) {
    static $post_alt;

    $post = get_post( $post_id );

/* Make sure we have a real post first. */
if ( !empty( $post ) ) {

    $post_id = $post->ID;

    /* Add hentry for microformats compliance, the post type, and post status. */
    $classes = array( 'hentry', $post->post_type, $post->post_status );

    /* Post alt class. */
    $classes[] = 'post-' . ++$post_alt;
    $classes[] = ( $post_alt % 2 ) ? 'odd' : 'even alt';

    /* Author class. */
    $classes[] = 'author-' . sanitize_html_class( get_the_author_meta( 'user_nicename' ), get_the_author_meta( 'ID' ) );

    /* Sticky class (only on home/blog page). */
    if ( is_home() && is_sticky() && !is_paged() )
        $classes[] = 'sticky';

    /* Password-protected posts. */
    if ( post_password_required() )
        $classes[] = 'protected';

    /* Has excerpt. */
    if ( post_type_supports( $post->post_type, 'excerpt' ) && has_excerpt() )
        $classes[] = 'has-excerpt';

    /* Has <!--more--> link. */
    if ( !is_singular() && false !== strpos( $post->post_content, '<!--more-->' ) )
        $classes[] = 'has-more-link';

    /* Post format. */
    if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
        $post_format = get_post_format( $post_id );
        $classes[] = ( ( empty( $post_format ) || is_wp_error( $post_format ) ) ? 'format-standard' : "format-{$post_format}" );
    }

    /* Add category and post tag terms as classes. */
    if ( 'post' == $post->post_type ) {

        foreach ( array( 'category', 'post_tag' ) as $tax ) {

            foreach ( (array)get_the_terms( $post->ID, $tax ) as $term ) {
                if ( !empty( $term->slug ) )
                    $classes[] = $tax . '-' . sanitize_html_class( $term->slug, $term->term_id );
            }
}

Did my way of using filter wrong?
Thanks.

2 Answers 2

1

For your filter to work, the Hybrid Core code would have to run something like $abcd = apply_filters('hybrid_post_attributes',$something); and I don't see that in the code you posted. In fact, I don't see apply_filters anywhere at all. Based entirely on that posted code, you can't filter those items.

I don't think you understand how filters work. You cannot hook to a function. You need to hook to something specifically created with apply_filters or do_action (for action hooks). See:

Clarification on filters and hooks
http://codex.wordpress.org/Plugin_API

However, my guess, based on the name, is that hybrid_get_post_class is itself hooked to post_class. You should be able to hook your own function to that.

add_filter('post_class', 'my_post_attributes');
function my_post_attributes($classes){
  $classes[] = ' item';
  return $classes;
}

Here is your "one line" version, @ChipBennett.

add_filter('post_class', 'my_post_attributes');
function my_post_attributes($classes){
  return array_merge($classes,array('item'));
}
2
  • Note: $classes in this case is an array, not a string. You need to append an array key, then return the array. If there's a single-line way to do that, I'd love to see it. :) Commented Oct 20, 2013 at 14:57
  • Nicely done! :) Commented Oct 20, 2013 at 15:09
0

It looks like your callback is written wrong:

add_filter( 'hybrid_post_attributes', 'my_post_atributes');
function my_post_atributes($attributes){
    return  $attributes['class'] = substr_replace($attributes['class'], ' item', -1);
}

You pass it an array, and you need to return that array. As written, you're only returning one array key. Try modifying the array, then returning it:

add_filter( 'hybrid_post_attributes', 'my_post_atributes');
function my_post_atributes($attributes){
    // Modify
    $attributes['class'] = substr_replace($attributes['class'], ' item', -1);
    // Return
    return $attributes;
}

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.