WordPress: rendere gli URL relativi

Possiamo rendere gli URL di WordPress relativi con molta semplicità.

WordPress di Gabriele Romanato

Possiamo rendere gli URL di WordPress relativi con molta semplicità.

Il seguente codice, da inserire in functions.php, fa uso delle funzioni anonime di PHP (5.3 e superiori):


add_action( 'template_redirect', 'my_relative_url' );

function my_relative_url() {
  $filters = array(
	 
	 // Post e categorie 
	  
    'bloginfo_url',
    'the_permalink',
    'wp_list_pages',
    'wp_list_categories',
    'the_content_more_link',
    'the_tags',
    'the_author_posts_link',
    'post_link',       
    'post_type_link',  
    'page_link',       
    'attachment_link', 
    'get_shortlink', 
    'post_type_archive_link',
    'get_pagenum_link',          
    'get_comments_pagenum_link',
    'term_link',   
    'search_link',
    'day_link',   
    'month_link',
    'year_link',

    // URL dell'installazione
    'option_siteurl',
    'blog_option_siteurl',
    'option_home',
    'admin_url',
    'get_admin_url',
    'get_site_url',
    'network_admin_url',
    'home_url',
    'includes_url',
    'site_url',
    'site_option_siteurl',
    'network_home_url',
    'network_site_url',

    // Debug
    'get_the_author_url',
    'get_comment_link',
    'wp_get_attachment_image_src',
    'wp_get_attachment_thumb_url',
    'wp_get_attachment_url',
    'wp_login_url',
    'wp_logout_url',
    'wp_lostpassword_url',
    'get_stylesheet_uri',
    'get_stylesheet_directory_uri',
    'plugins_url',
    'plugin_dir_url',
    'stylesheet_directory_uri',
    'get_template_directory_uri',
    'template_directory_uri',
    'get_locale_stylesheet_uri',
    'script_loader_src', 
    'style_loader_src',
    'get_theme_root_uri'
  );
  
  $home_url = home_url();
  
  $filter_fn = function( $link ) use ( $home_url ) {
    if ( !is_array( $link ) && strpos( $link, $home_url ) === 0 ) {
      return wp_make_link_relative( $link );
    } else {
      return $link;
    }
  };

  foreach ( $filters as $filter ) {
    add_filter( $filter, $filter_fn );
  }
}


Commenti