user_trailingslashit

Retrieves a trailing-slashed string if the site is set for adding trailing slashes.

Description

(string) user_trailingslashit( (string) $string, (string) $type_of_url = '' ); 

Conditionally adds a trailing slash if the permalink structure has a trailing slash, strips the trailing slash if not. The string is passed through the filter. Will remove trailing slash from string, if site is not set to have them.

Returns (string)

The URL with the trailing slash appended or stripped.

Parameters (2)

0. $string (string)
URL with or without a trailing slash.
1. $type_of_url — Optional. (string) => ''
The type of URL being considered (e.g. single, category, etc) for use in the filter. Default empty string.

Usage

  1. if ( !function_exists( 'user_trailingslashit' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // URL with or without a trailing slash. 
  5. $string = ''; 
  6.  
  7. // Optional. The type of URL being considered (e.g. single, category, etc) 
  8. // for use in the filter. Default empty string. 
  9. $type_of_url = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = user_trailingslashit($string, $type_of_url); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function user_trailingslashit($string, $type_of_url = '') { 
  2. global $wp_rewrite; 
  3. if ( $wp_rewrite->use_trailing_slashes ) 
  4. $string = trailingslashit($string); 
  5. else 
  6. $string = untrailingslashit($string); 
  7.  
  8. /** 
  9. * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes. 
  10. * @since 2.2.0 
  11. * @param string $string URL with or without a trailing slash. 
  12. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',  
  13. * 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',  
  14. * 'category', 'page', 'year', 'month', 'day', 'post_type_archive'. 
  15. */ 
  16. return apply_filters( 'user_trailingslashit', $string, $type_of_url );