Advanced Customization of "rep__shortcode_atts" Filter

This guide will help you customize shortcode attributes using the rep__shortcode_atts filter to modify the output of related elements based on specific conditions.

1. Access Your Theme’s Functions File

  • To begin, log in to your WordPress admin dashboard.
  • Next, navigate to the “Appearance” menu and select “Theme Editor.”
  • In the right-hand panel, you’ll see a list of theme files. Find and click on the “functions.php” file associated with your currently active theme.

2. Insert the Provided Code

  • Once you’ve opened the “functions.php” file, scroll to the bottom or a convenient location for adding custom code.
  • This can also be done if you have installed code snippets plugin in your WordPress site like WPCode and Code Snippets to insert the code.
  • Copy and paste the following code snippet into the “functions.php” file:
// Override related elements shortcode args
add_filter( 'rep__shortcode_atts', function( $atts, $name ){
	if( $name == 'rep_listings' && is_singular('rep_listing') ){
		$post_id = get_the_ID();
		$search_location = \Launchpad\lp__get_field_value( "search_location", $post_id );
		$search_category = \Launchpad\lp__get_field_value( "search_category", $post_id );
		$beds = \Launchpad\lp__get_field_value( 'bedrooms', $post_id );
		$labels = array(
			'sale' => 'for sale',
			'lease' => 'for rent',
			'sold' => 'sold',
			'leased' => 'leased'
		);
		$states = \REP_Malcolm\rep__extract_search_categories([$search_category],'listing_state');
		$category = \REP_Malcolm\rep__extract_search_categories([$search_category],'listing_category');
		$state_labels = [];
		foreach( $states as $state ){
			$state_labels[] = \Launchpad\lp__array_key_value( strtolower($state), $labels );
		}
		
		$state_labels = implode(" or ", $state_labels);
		$bedroom_title = "";
		
		if( in_array('Residential', $category) ){
			$bedroom_title = "{$beds} bedroom ";
		}
		
		$atts['header_title'] = "Other {$bedroom_title}properties {$state_labels} in {$search_location}";	
	}
	return $atts;
}, 10, 2 );
This code is designed to dynamically generate a custom header title for the ‘rep_listings’ shortcode when used on a single ‘rep_listing’ post.
The title incorporates information such as location, bedroom count, and state labels, making it highly adaptable for different scenarios on your WordPress site.

3. Understanding the Code

  • This code utilizes the rep__shortcode_atts filter to customize shortcode attributes when the [rep_listings] shortcode is used.
  • It checks if the current page is displaying a single ‘rep_listing’ post, as indicated by is_singular('rep_listing').
  • The code then retrieves various fields such as ‘search_location,’ ‘search_category,’ ‘bedrooms,’ and more associated with the current post.
  • It processes and formats this information to generate a custom header title for the related elements shortcode.

4. Save Changes

After adding the code, click the “Update File” button to save your changes to the “functions.php” file.

5. Testing

To see the code in action, use the [rep_listings] shortcode within the context of a ‘rep_listing’ post on your WordPress site. The code will automatically modify the output based on the post’s data.