PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 25, 2017 1:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 115 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 8  Next
Author Message
PostPosted: Wed Sep 13, 2017 7:41 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
array_filter appears to be an array to 'wrap' the other array inside?? Trying to read about it, but not quite getting it.
Quote:
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:

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

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 7:45 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
Example here: viewtopic.php?f=2&t=144184#p709589

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 7:58 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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...?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 9:23 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
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

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 9:33 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
" You need to define a filter function and pass its name as the second argument to add_filter".
Sorry a little lost again.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 11:25 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
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');

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 11:57 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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');

        }

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 1:34 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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');

        }


???

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 2:58 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
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?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Sep 13, 2017 3:22 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Thu Sep 14, 2017 5:20 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
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).

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Sep 14, 2017 5:21 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
I'll try that, but can you 'comment' it so i can understand what you have done there?

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Thu Sep 14, 2017 5:26 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Thu Sep 14, 2017 5:38 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6349
Location: Montreal, Canada
I've added some comments and some links to the code. What broke, specifically? What sorts of errors were you seeing?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Thu Sep 14, 2017 5:47 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4335
Location: United Kingdom
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.

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 115 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 8  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group