Add Filter Template Functions
-
Hello,
I would like to know the correct method to add a filter in a child theme for a template function.
For this example lets use ‘bbp_get_user_subscribe_link()’
I would like to insert
<icon class="icon-user"></i>
into the following before the %s
<span id="subscribe-%d" %s>%s
$html = sprintf( '%s<span id="subscribe-%d" %s>%s</span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
of this function bbp_get_user_subscribe_link()
function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { if ( !bbp_is_subscriptions_active() ) return; // Parse arguments against default values $r = bbp_parse_args( $args, array( 'subscribe' => __( 'Subscribe', 'bbpress' ), 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ), 'user_id' => 0, 'topic_id' => 0, 'before' => ' | ', 'after' => '' ), 'get_user_subscribe_link' ); // Validate user and topic ID's $user_id = bbp_get_user_id( $r['user_id'], true, true ); $topic_id = bbp_get_topic_id( $r['topic_id'] ); if ( empty( $user_id ) || empty( $topic_id ) ) { return false; } // No link if you can't edit yourself if ( !current_user_can( 'edit_user', (int) $user_id ) ) { return false; } // Decide which link to show $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id ); if ( !empty( $is_subscribed ) ) { $text = $r['unsubscribe']; $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); } else { $text = $r['subscribe']; $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); } // Create the link based where the user is and if the user is // subscribed already if ( bbp_is_subscriptions() ) { $permalink = bbp_get_subscriptions_permalink( $user_id ); } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { $permalink = bbp_get_topic_permalink( $topic_id ); } else { $permalink = get_permalink(); } $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); $sub = $is_subscribed ? ' class="is-subscribed"' : ''; $html = sprintf( '%s<span id="subscribe-%d" %s>%s</span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); // Initial output is wrapped in a span, ajax output is hooked to this if ( !empty( $wrap ) ) { $html = '<span id="subscription-toggle">' . $html . '</span>'; } // Return the link return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id ); }
So how would I construct my_custom_bbp_get_user_subscribe_link() function to add_filter( ‘bbp_get_user_subscribe_link’, ‘my_custom_bbp_get_user_subscribe_link’ ); ?
I hope the answer will serve as a general example to correctly add filters to many other template functions.
Thanks for your assistance.
- You must be logged in to reply to this topic.