Page 2 of 8

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 7:41 am
by simonmlewis
array_filter appears to be an array to 'wrap' the other array inside?? Trying to read about it, but not quite getting it.
Go back and look at my array_filter example
Not sure I saw an example. Only a link to a page, but doesn't quite make sense to me.

I have found this in the comment-template.php file:

Code: Select all

		 * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
		 * perform a separate comment query and allow Walker_Comment to paginate.
		 */
		if ( $r['page'] || $r['per_page'] ) {
			$current_cpage = get_query_var( 'cpage' );
			if ( ! $current_cpage ) {
				$current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
			}

			$current_per_page = get_query_var( 'comments_per_page' );
			if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
				$comment_args = array(
					'post_id' => get_the_ID(),
					'orderby' => 'comment_date_gmt',
					'order' => 'ASC',
					'status' => 'approve',
				);
Do I need to tap into this somehow?

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 7:45 am
by Celauran

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 7:58 am
by simonmlewis

Code: Select all

if(has_filter('wp_list_comments_args')) {
  $args = apply_filters('wp_list_comments_args', $args);
  function wp_list_comments_remove_blanks($args) {
  // the $args parameter is an array of arguments for  wp_list_comments_args() function
  $array[
        ['comment_content' => '', 'condition' => false],
        ];
  $filtered = array_filter($array, function($wp_list_comments_args) {
    return $item['condition'] === true;
});
 
  return $wp_list_comments_args;
}
add_filter('wp_list_comments_args');

        }
Closer...?

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 9:23 am
by Celauran
Getting closer. You don't need to call apply_filter as that's already called for you. You need to define a filter function and pass its name as the second argument to add_filter

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 9:33 am
by simonmlewis
" You need to define a filter function and pass its name as the second argument to add_filter".
Sorry a little lost again.

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 11:25 am
by Celauran
You're clear on how to write the filtering function? When you call add_filter, just pass in its name. Say you call your function filterOutEmptyComments, then you'd call add_filter as

Code: Select all

add_filter('wp_list_comments_args', 'filterOutEmptyComments');

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 11:57 am
by simonmlewis
It's a fine example of me thinking I have grasped something, to realise that I haven't.

Do I need to literally add this in here, or have i written something wrong?

Code: Select all

if(has_filter('wp_list_comments_args')) {
  $args = apply_filters('wp_list_comments_args', $args);
  function wp_list_comments_remove_blanks($args) {
  // the $args parameter is an array of arguments for  wp_list_comments_args() function
  $array[
        ['comment_content' => '', 'condition' => false],
        ];
  $filtered = array_filter($array, function($wp_list_comments_args) {
    return $item['condition'] === true;
});
 
  return $wp_list_comments_args;
}
add_filter('wp_list_comments_args');

        }

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 1:34 pm
by simonmlewis

Code: Select all

if(has_filter('wp_list_comments_args')) {
  $args = apply_filters('wp_list_comments_args', $args);
  function wp_list_comments_remove_blanks($args) {
  // the $args parameter is an array of arguments for  wp_list_comments_args() function
  $array[
        ['comment_content' => '', 'condition' => false],
        ];
  $filtered = array_filter($array, function($wp_list_comments_args) {
    return $item['condition'] === true;
});
 
  return $wp_list_comments_args;
}
add_filter('wp_list_comments_args','wp_list_comments_remove_blanks');

        }
???

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 2:58 pm
by Celauran
The add_filter line looks right. The rest of it, not so much. Can you walk me through the code line by line and explain what each line is doing?

Re: Wordpress Pagination Links - how do I modify this?

Posted: Wed Sep 13, 2017 3:22 pm
by simonmlewis

Code: Select all

// checks that there is a filter called wp_list_comments_args - tho I know there is... do I need this?
if(has_filter('wp_list_comments_args')) {
// puts the arguments of the filter into $args.
  $args = apply_filters('wp_list_comments_args', $args);
// creates a function called wp_list_comments_remove_blanks, using $args values
  function wp_list_comments_remove_blanks($args) {
  // the $args parameter is an array of arguments for  wp_list_comments_args() function
// adds to the $args array, that comment_content must be empty - tho not sure if this is right at all
  $array[
        ['comment_content' => '', 'condition' => false],
        ];
// adds a filter to the original function, with my array
  $filtered = array_filter($array, function($wp_list_comments_args) {
// returns it .... should that be $item???
    return $item['condition'] === true;
});
 // returns it to the original function
  return $wp_list_comments_args;
}
Applies the function of ....remove_blanks to the original function.
add_filter('wp_list_comments_args','wp_list_comments_remove_blanks');

        }
That's my theory, but I can see there are issues. But not sure why they are issues. Or in fact, if I am thinking about it all in the right way.

I know I need to take the original function, then apply a filter. To do that I need to write a function that adds a filter to say that comment_content must not be empty.
Then apply that filter back to the original function within Core WP.

Re: Wordpress Pagination Links - how do I modify this?

Posted: Thu Sep 14, 2017 5:20 am
by Celauran
Looking back at it last night, it seems like not having used WP in a while came back to bite me and I was mistaken about the use of wp_list_comments_args. Fortunately, that may not change much. What still looks like a workable approach is to call get_comments, then array_filter the results, and then pass that back into wp_list_comments as the second argument. I just whipped this together quickly, but I think this is the general approach you want to take.

Code: Select all

<?php

/**
 * Filters down an array of comments to those where comment_content is not an empty string
 *
 * @param array An array of WP_Comment objects (https://developer.wordpress.org/reference/classes/wp_comment/)
 *
 * @return array An array of WP_Comment objects
 */
function filter_empty_comments($comments) {
    // http://php.net/manual/en/function.array-filter.php
    return array_filter($comments, function($comment) {
        return !empty($comment->comment_content);
    });
}

/**
 * Retrieve comments from the database
 * Some arguments may be required here to pull only comments related to the current post
 * See https://developer.wordpress.org/reference/functions/get_comments/
 */
$unfiltered_comments = get_comments();

// Filter out empty comments, see above
$filtered_comments = filter_empty_comments($unfiltered_comments);

// Used in comments template to list comments
// See https://developer.wordpress.org/reference/functions/wp_list_comments/
wp_list_comments([], $filtered_comments);
You may need to tweak the arguments you pass to wp_list_comments (the currently empty array).

Re: Wordpress Pagination Links - how do I modify this?

Posted: Thu Sep 14, 2017 5:21 am
by simonmlewis
I'll try that, but can you 'comment' it so i can understand what you have done there?

Re: Wordpress Pagination Links - how do I modify this?

Posted: Thu Sep 14, 2017 5:26 am
by simonmlewis
I did the silly thing of just trying it in the child's staging function it is broke it. OBviously it does need some tweaking, butnot sure where.

If you could comment it, I might be able to see where I need to add arguments etc.

Re: Wordpress Pagination Links - how do I modify this?

Posted: Thu Sep 14, 2017 5:38 am
by Celauran
I've added some comments and some links to the code. What broke, specifically? What sorts of errors were you seeing?

Re: Wordpress Pagination Links - how do I modify this?

Posted: Thu Sep 14, 2017 5:47 am
by simonmlewis
The page loaded up empty. White. And the Error Log showed no errors, bizarrely. So I cannot even tell you the cause.
"You may need to tweak the arguments you pass to wp_list_comments".
I don't see wp_list_comment or any of the functions or things from the live version in there. Is that why it's not working?
this is all in staging, so it's ok to break it. lol.