• Hi All

    I am using a Plugin that inserts url links into a template. I would like the links to open in a new window. I can modify the core Plugin template to do this as follows (see last line of code below). However I know this will be lost if the Plugin updates. I would therefore like to add a filter in the functions.php file in my child theme to do this instead, but am not sure how to do it. Any advice appreciated. By the way, site is currently only on local host so no link to include.

    Thanks

    // Begin templating logic.
    			$tpl = '<div itemscope itemtype="https://schema.org/Person" class="%%CLASS%%">%%AVATAR%% %%TITLE%% <div id="team-member-%%ID%%"  class="team-member-text" itemprop="description">%%TEXT%% %%AUTHOR%%</div></div>';
    			$tpl = apply_filters( 'woothemes_our_team_item_template', $tpl, $args );
    
    			$count = 0;
    			foreach ( $query as $post ) { $count++;
    				$template = $tpl;
    
    				$css_class = apply_filters( 'woothemes_our_team_member_class', $css_class = 'team-member' );
    				if ( ( is_numeric( $args['per_row'] ) && ( 0 == ( $count - 1 ) % $args['per_row'] ) ) || 1 == $count ) { $css_class .= ' first'; }
    				if ( ( is_numeric( $args['per_row'] ) && ( 0 == $count % $args['per_row'] ) ) ) { $css_class .= ' last'; }
    
    				// Add a CSS class if no image is available.
    				if ( isset( $post->image ) && ( '' == $post->image ) ) {
    					$css_class .= ' no-image';
    				}
    
    				setup_postdata( $post );
    
    				$title 		= '';
    				$title_name = '';
    
    				// If we need to display the title, get the data - JOHNNYM added to line 115 target=_blank
    				if ( ( get_the_title( $post ) != '' ) && true == $args['display_author'] ) {
    					$title .= '<h3 itemprop="name" class="member">';
    
    					if ( true == $args['display_url'] && '' != $post->url && apply_filters( 'woothemes_our_team_member_url', true ) ) {
    						$title .= '<a href="' . esc_url( $post->url ) . '" target="_blank">' . "\n";
    					}
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry for the late reply. This is an important concept to grasp. While adding filters is a great way to sustainably alter how WP works, you cannot just filter anything you like. The plugin or core author needs to anticipate your need and insert an apply_filters() call for you to add a filter to. Without that, there is nothing you can do that is sustainable.

    For example, you can alter the value of $tpl at the start of the snippet because it’s value is assigned from the return of apply_filters() of which you can add a filter to. The line you actually want to change has no apply_filters() call, so you cannot add a filter to change this part of the code. (unless there is a filter farther down in code that was not included in the snippet)

    There is no point to add your own apply_filters() code because it too will be lost during an update. If you do that, you may as well directly hack the code, knowing it will be lost and need to be reapplied.

    When no opportunity to add a filter exists to change content, another possible approach is to use javascript or jQuery to alter the content once the page loads.

    Thread Starter johnnymorriswp

    (@johnnymorriswp)

    Thanks, bcworkz. I very much appreciate your advice. Further down in the template there appears to be an additional filter further down in the code. Does that look like it could help me?

    // Allow child themes/plugins to filter here.
    		$html = apply_filters( 'woothemes_our_team_html', $html, $query, $args );
    
    		if ( $args['echo'] != true ) { return $html; }
    
    		// Should only run is "echo" is set to true.
    		echo $html;
    
    		do_action( 'woothemes_our_team_after', $args ); // Only if "echo" is set to true.
    } // End woothemes_our_team()
    Moderator bcworkz

    (@bcworkz)

    If $html contains the link in which you need to add the target attribute, then yes, you can use this filter to achieve your goal! You would use standard PHP string manipulation functions to insert the attribute. Exactly how to do this would depend on the nature of the remaining HTML.

    For example, after finding the first <a, replace the next > with target="_blank"> could work. Try to coordinate the quote style of your PHP code (single vs. double) to work with that typically used by the HTML.

    Thread Starter johnnymorriswp

    (@johnnymorriswp)

    Thanks, bcworkz. That’s great!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help Adding Filter’ is closed to new replies.