WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Can *anything* be filtered? (15 posts)

  1. Doodlebee
    Member
    Posted 5 years ago #

    Just wondering - can *anything* in the WP core files be filtered?

    I'm trying to replace a piece of WP core code with my functions.php file, and the filter isn't taking. I'm not really "up" on this part of WordPress coding, so I'm trying to mess around and see how I can make this work - I don't want to edit core files.

    Basically, I need to add some code to the start_lvl() and end_lvl() functions in the comments-template.php file. I've copied both functions into my own functions.php file, renamed them, added the code I need and applied the filter, but it's not taking. So now I'm wondering if these items can even be edited at all without hacking the core.

    if they *can* be edited via functions.php - can someone provide a link to something that can tell me *how*? I've searched for a week, and either it can't be done, or I don't know what to call what I'm searching for. not looking for the code to solve my issue - just a little bit of direction on where to look.

    Thanks!

  2. Basically, I need to add some code to the start_lvl() and end_lvl() functions in the comments-template.php file.

    Doodlebee,

    That sounds a bit complicated. What are you trying to do functionally? I mean, are you trying to modify your comments output to achieve a look, feel, etc?

    I would refer you to http://clark-technet.com/2008/11/wordpress-27-comment-callback-function but I see that you've done that already :)

  3. Doodlebee
    Member
    Posted 5 years ago #

    LOL - yeah - he says it's even out of his league - so what chance to little peons like me have? :) (But I do so love a challenge!)

    Yes, I am trying to achieve a "look" :)

    Basically, I need to add a div to enclose comment replies. I want my comments to be displayed - but if there's a reply to a comment (or a bunch of replies to a specific comment) I want the replies to be inside a grouped outer containing element.

    I actually *did* pull it off, but I had to hack the core to make it happen. I'd rather not leave it this way, for obvious reasons. But the *reason* for it is so I can use jQuery to expand the replies. So my comments layout would be such that only the topmost level of comments will show, and if said comment has a reply, then a link will appear to "click to open and view replies".

    I found *where* I need to insert said piece of code - but like I said, the only way I've managed to pull it off right now is by hacking core. If I could just figure out a way to add in a line to that start_lvl() and end_lvl() functions in the Walker class, then I'd be set. But basically, I just need to add a line so they look like so:

    function start_lvl(&$output, $depth, $args) {
    		$GLOBALS['comment_depth'] = $depth + 1;
    
    		switch ( $args['style'] ) {
    			case 'div':
    				echo "<div id="children">\n";
    				echo "<div>\n";
    				break;
    			case 'ol':
    				echo "<ol class='children'>\n";
    				break;
    			default:
    			case 'ul':
    				echo "<ul class='children'>\n";
    				break;
    		}
    	}

    You can see, I only need to add one line. So I wasn't sure if this was even possible to do, because it seems fairly deeply embedded into the core files. I'm thinking I may just have to write a plugin to pull off the effect, but that seems like overkill for just the addition of a single line of code. So if it's not possible, that's fine (and may the plugin writing commence! LOL) but if it's possible to "inject" a line in that section, then I think it would be more simplistic to do so, rather than take the long road around.

    Does that make more sense?

  4. That sounds interesting and you got it working by modifying the start_lvl and end_lvl code...

    So regular linear comments go as usual but you want replies to be grouped within a div like so?

    Comment-1
    Comment-2
    Comment-3
      div replies-to-Comment-3
        reply-3.1
        reply-3.2
      /div
    Comment-4

    Should the replies to a reply get grouped in a new div also?

  5. Doodlebee
    Member
    Posted 5 years ago #

    Well, with editing the start_lvl and end_lvl ii does do that. So if there's a sub-reply, those are also grouped.

    I've managed to make a little progress with it - but I keep running into the same issue: because wp_list_comments (and using the callback function) is it's own little loop, anything you add using said function is also looped. So instead of calling in the outer div a single time for each depth change, it's being called in for every single comment.

    But my progress is by using the return callback function, and calling in the comment_depth global:

    $GLOBALS['comment_depth'] = $depth + 1;
       extract($args, EXTR_SKIP);
       if ( 'div' == $args['style'] ) {
         switch ( $args['style'] ) {
    	   case 'div':
           echo '<div class="children">' . "\n";
    	   break;
         }
       }

    this is actually working - it's placing the div exactly as I need it - but like I said, because it's in it's "own little loop" it's placing it for *every* comment - not grouping the depths.

  6. Doodlebee
    Member
    Posted 5 years ago #

    Okay - slightly more progress:

    $GLOBALS['comment_depth'] = $depth;
       if($depth != '1') {
         extract($args, EXTR_SKIP);
         if ( 'div' == $args['style'] ) {
           switch ( $args['style'] ) {
    	     case 'div':
    		 echo "<div class='replylink'>Show/Hide Replies</div>\n";
             echo '<div class="children">' . "\n";
    	     break;
           }
         }
       }

    so now we have the topmost level of comments showing, and if there are replies, then there's a Show/Hide link showing - but same issue: every reply comment is being wrapped, not grouped. i think this is where I got stumped the last time - I guess I need some sort of "pause" in the Loop or something, so if there's more than one reply, it'll just open at the first and close at the last.

  7. sojweb
    Member
    Posted 5 years ago #

    Could you post some HTML of how it should look? Is there a reason you can't do this in the theme itself?

  8. Doodlebee
    Member
    Posted 5 years ago #

    I *am* trying to do it in the theme - through the functions.php file. I can't place the div outside the wp_list_comments call, because all that does is wrap ALL comments in a div - that's not what I'm looking to do.

    By default, the comments display (HTML output) like so:

    <div class="comments" id="comment-ID">
    <!--/comment stuff here-->
    <!--/comments-->
    </div>

    if it's threaded, the comments-template.php file nests it like so:

    <div class="comments" id="comment-ID">
    <!--/comment stuff here-->
    
    <div class="comments" id="comment-ID">
    <!--/this would be a comment reply->
    <!--/comments-->
    </div>
    
    <div class="comments" id="comment-ID">
    <!--/this would be a second comment reply->
    <!--/comments-->
    </div>
    
    <!--/comments-->
    </div>

    Which is fine. but what *I* need is nested, with an outer div:

    <div class="comments" id="comment-ID">
    <!--/comment stuff here-->
    
    <div class="children">
    
    <div class="comments" id="comment-ID">
    <!--/this would be a comment reply->
    <!--/comments-->
    </div>
    
    <div class="comments" id="comment-ID">
    <!--/this would be a second comment reply->
    <!--/comments-->
    </div>
    
    <!--/children-->
    </div>
    
    <!--/comments-->
    </div>

    I suppose I could pull this off using the OL/UL version rather than the DIV, since those are already supplied with the class I need, but I prefer DIVs, because my comments are styled in such a way that if I use OL/LI, then my HTML becomes invalid.

  9. sojweb
    Member
    Posted 5 years ago #

    Sorry, I meant just in a theme template file, and not through functions. And I see what you mean now, thanks.

  10. Doodlebee
    Member
    Posted 5 years ago #

    No problem :) The more people that "get" what I'm trying to do, the better :)

  11. sojweb
    Member
    Posted 5 years ago #

    What about doing it in JavaScript? Or would that not work for you?

  12. Doodlebee
    Member
    Posted 5 years ago #

    Well, I'm already using jQuery (that's actually what started all of this) - but I don't know how I'd insert the needed wrapping div with javascript...

    I've actually written some PHP code (about 3 days ago) that would insert the opening div tag in the right place, but the issue was the closing one. i couldn't get a proper loop going that would could the replies and insert the closing div tag at the last reply. So I keep coming *really* close, and then I hit a wall I can't seem to get over.

  13. sojweb
    Member
    Posted 5 years ago #

    Well, with JavaScript, you could loop through all the div's in the comments section, and for each one, collect child div's into a container div. It'd be pretty simple, but might look strange as it ran on the page.

  14. Doodlebee
    Member
    Posted 5 years ago #

    Thanks soj -

    I got it :)

    Basically I had to rewrite the bottom half of the comments-template.php file, and do a class extension. Seems like a little "overkill" for just changing a few select lines of code, but it's better than hacking core!

  15. Doodlebee
    Member
    Posted 5 years ago #

    If it helps anyone, I turned it into a plugin. Feel free to use it.

    jQuery Expandable Comment Replies

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.