Skip to:
Content
Pages
Categories
Search
Top
Bottom

Please can somebody for once explain how Filters ? Actions work ?


  • Shmoo
    Participant

    @macpresss

    I’ve look at it a few times in the WordPress Codex and it probably explains GREATLY how it works and what it does but maybe I just don’t get it.

    I understand the concept of taking a function that exists <—> edit it <—> return it to WP Core but how!

    Lets say I just simply want to replace the text inside a HTML “… class=”bbp-topic-spam-link”> ” of the spam link.

    First step I do is search for the function: bbp_topic_spam_link()

    Second step found it inside plugins/bbpress/includes/topics/template.php [ line 2832 ]

    Third step copy the entire function to my functions.php file or plugins functions file.

    Fourth step, edit the code that you want to edit but after that, how do you add the filter to this function to make it a new or your function.

    
    /**
     * Output the spam link of the topic
     * @uses bbp_get_topic_spam_link() Topic spam link
     */
    function bbp_topic_spam_link( $args = '' ) {
    	echo bbp_get_topic_spam_link( $args );
    }
    
    	/**
    	 * Return the spam link of the topic
    	 */
    	function bbp_get_topic_spam_link( $args = '' ) {
    
    		// Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'id'           => 0,
    			'link_before'  => '',
    			'link_after'   => '',
    			'sep'          => ' | ',
    			'spam_text'    => esc_html__( 'Spam',   'bbpress' ),
    			'unspam_text'  => esc_html__( 'Unspam', 'bbpress' )
    		), 'get_topic_spam_link' );
    
    		$topic = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
    
    		if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
    			return;
    
    		$display = bbp_is_topic_spam( $topic->ID ) ? $r['unspam_text'] : $r['spam_text'];
    		$uri     = add_query_arg( array( 'action' => 'bbp_toggle_topic_spam', 'topic_id' => $topic->ID ) );
    		$uri     = wp_nonce_url( $uri, 'spam-topic_' . $topic->ID );
    		$retval  = $r['link_before'] . '<a href="' . esc_url( $uri ) . '" class="bbp-topic-spam-link">' . $display . '</a>' . $r['link_after'];
    
    		return apply_filters( 'bbp_get_topic_spam_link', $retval, $r );
    	}
    

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

  • Robin W
    Moderator

    @robin-w

    best explanation I know is here :

    Filters Explained

    I plan to write some bbPress specific documentation for this when I get my head around it enough to pass it on (I am relatively new to wordpress as well).


    Shmoo
    Participant

    @macpresss

    Thanks, IF I knew how it worked I would also like to write about it but for some reason they keep this very secretly while 98% of all bbPress changes have to happen through filters.

    I’ve read the WP Codex about this a few times but I just don’t see it, I can’t connect the dots + all tutorials online are often talking about this in a global way, they make up ‘some’ function and add it to another ‘something’ function.
    Pippin often has easy to follow tutorials but I lost him in the second picture.

    A Quick Introduction to Using Filters

    Respect for your link!
    I will make some time for it and read it very slowly today, looks like a very good one because I saw a heading HOW to SPOT a filter!


    Robin W
    Moderator

    @robin-w

    shmoo,

    don’t think there’s any conspiracy to keep this stuff secret, rather that the clever people who write this stuff are generally the same type of people who hate documenting – they enjoy the code and getting it to do stuff, not the this is how I did it.

    I’d love to see a crib on the structure of the bbPress plugin – a list of the directories and files, and a description of what each file did – eg loop-single-forum.php does this, and content-single-topic does that etc. but unless I do one, don’t think it will happen.

    I’ll try to unravel the filter side of the plugin at some stage !


    Shmoo
    Participant

    @macpresss

    I know they don’t try to keep information from us but it can be frustrated at times if you wanna learn something but for X reasons you can’t find it or you just can’t understand it :S

    And it becomes extra frustrated if you know for sure something similar works 1000 times easier inside WordPress out of the box.

    The link you gave me explained it very well, I’ve read it twice and now I made my very first filter.

    bbPress

    
    // Adding extra CSS
    function shmoo_extra_css( $classes ) {
    		$classes[] = 'tab-row';
    		return $classes;
    }
    add_filter( 'bbp_get_topic_class', 'shmoo_extra_css' );
    

    Doing the same thing in WordPress

    
    <?php body_class( 'tab-row' ); ?>
    

    Because this simply doesn’t work.

    
    <?php bbp_topic_class( 'tab-row' ); ?>
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar