PHP Developers Network
http://forums.devnetwork.net/

Wordpress Pagination Links - how do I modify this?
http://forums.devnetwork.net/viewtopic.php?f=2&t=144184
Page 2 of 8

Author:  simonmlewis [ Wed Sep 13, 2017 7:41 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?


Author:  Celauran [ Wed Sep 13, 2017 7:45 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Example here: viewtopic.php?f=2&t=144184#p709589

Author:  simonmlewis [ Wed Sep 13, 2017 7:58 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Syntax: [ Download ] [ Hide ]
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...?

Author:  Celauran [ Wed Sep 13, 2017 9:23 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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

Author:  simonmlewis [ Wed Sep 13, 2017 9:33 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

" You need to define a filter function and pass its name as the second argument to add_filter".
Sorry a little lost again.

Author:  Celauran [ Wed Sep 13, 2017 11:25 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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
Syntax: [ Download ] [ Hide ]
add_filter('wp_list_comments_args', 'filterOutEmptyComments');

Author:  simonmlewis [ Wed Sep 13, 2017 11:57 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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?

Syntax: [ Download ] [ Hide ]
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');

        }

Author:  simonmlewis [ Wed Sep 13, 2017 1:34 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Syntax: [ Download ] [ Hide ]
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');

        }


???

Author:  Celauran [ Wed Sep 13, 2017 2:58 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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?

Author:  simonmlewis [ Wed Sep 13, 2017 3:22 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Syntax: [ Download ] [ Hide ]
// 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.

Author:  Celauran [ Thu Sep 14, 2017 5:20 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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.

Syntax: [ Download ] [ Hide ]
<?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/referen ... p_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/referen ... _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/referen ... _comments/
wp_list_comments([], $filtered_comments);

You may need to tweak the arguments you pass to wp_list_comments (the currently empty array).

Author:  simonmlewis [ Thu Sep 14, 2017 5:21 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

I'll try that, but can you 'comment' it so i can understand what you have done there?

Author:  simonmlewis [ Thu Sep 14, 2017 5:26 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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.

Author:  Celauran [ Thu Sep 14, 2017 5:38 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

I've added some comments and some links to the code. What broke, specifically? What sorts of errors were you seeing?

Author:  simonmlewis [ Thu Sep 14, 2017 5:47 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

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.

Page 2 of 8 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/