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'm not sure how to use this filter. Could someone show me an example? I'm trying to change ('0', '1', '%') to ('0 Comments', '1 Comment', '% Comments').

I'm using this function to get the comments number.

    function lpe_get_comments_number( $anchor='#comments' ) {
        $return = '<a class="comments-number-link" href="' . get_permalink() . $anchor . '">';
        $return .= get_comments_number(); // '0', '1', '%'
        $return .= '</a>';
        return $return;
    }

I know I could just set the arguments here and call it a day, but I was hoping to store my custom functions away in a different file and handle their settings from the themes functions.php.

share|improve this question
    
You can use comments_number function to acheve this. codex.wordpress.org/Function_Reference/comments_number –  Vinod Dalvi Mar 4 '13 at 6:02
    
I'm trying to set the $args in the functions file, so that all comments_number() functions have the same value. –  marcup Mar 4 '13 at 6:42

2 Answers 2

If you put functions like get_permalink() or get_comments_number() in a function, then you have to make the $comments and $post vars accessible. Read more about the scope in PHP.

Normally you will do it with a global keyword. An easy example: retriving the permalink

function my_personal_permalink( $anchor = '' ) {
  global $post;
  $return = get_permalink( $post->ID );
  $return .= $anchor;

  return $return;
}

This is a very simple example. get_permalink() need some information from which post it should generate the permalink. So we have to make the 'post' accessible inside the function. This will be done with global $post;, it will make the actual post data accessible. get_permalink() need the post ID, we pass it with $post->ID

If you use template functions inside a function, then you have to explore which data the template functions need (post, comment, database connection ($wpdb), etc.) and pass them as argument.

share|improve this answer
    
The problem is not with the function, the function works. I'm looking for an example on how to use a filter with it. –  marcup Mar 5 '13 at 0:08
    
You mean apply_filters() and add_filter()? –  Ralf912 Mar 5 '13 at 8:50

From your comment on my first answer, I think you wish to apply filters.

function lpe_get_comments_number( $anchor='#comments' ) {
  $return = '<a class="comments-number-link" href="' . get_permalink() . $anchor . '">';

  $args = array(
    'zero' => 'Zero comments',
    'one' => 'One comment',
    'more' => '% Comments'
  );

  $filtered_args = apply_filters( 'choose_your_hook_name', $args );

  // be sure all keys in the filtered args are present and have a valid value
  // this is optional but usefull
  $args = wp_parse_args( $filtered_args, $args );

  $return .= get_comments_number( $args['zero'], $args['one'], $args['more'] ); // '0', '1', '%'

  $return .= '</a>';
  return $return;
}

add_filter( 'choose_your_hook_name', 'your_filter_callback', 1, 1 );

function your_filter_callback( $args ) {
  return array( 'zero' => 'Foo Comments', 'one' => 'Bar Comments' );
}

You can also pass additional argument to apply_filters() if you want to decide things depending on the passed arguments:

$zero = 'Zero Comments';

if ( $today == 'sunday' ) {
  $foo = 'Sunday, yeah!';
} else {
  $foo = '';
  $bar = 'No sunday';
}

$zero = apply_filters( 'choose_your_hook_name', $zero, $foo, $bar );

get_comments_number( $zero, $one, $more );

[more code]

add_filter( 'choose_your_hook_name', 'your_filter_callback', 1, 3 );

function your_filter_callback( $zero, $foo = '', $bar = '' ) {
  if ( ! empty( $foo ) )
    $zero = 'No comments on sunday!';
  elseif( ! empty( $bar ) )
    $zero = $bar . ', here are % comments';

  return $zero;
}
share|improve this answer

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.