Elimina „category” Si „tag” din URL WordPress

2
573
Elimina „category” Si „tag” din URL WordPress
Ti-a placut acest articol? Acorda-i o nota

Iata cum poti trece la o noua performanta cu blogul tau, in cativa pasi:

1. Primul pas este sa iti configurezi Permalink-ul corespunzator. Mergi la Settings > Permalinks si alege structura Custom. Completeaza /%category%/%postname%/. Asta iti va asigura URL-ul cu structura : blogultau.ro/numele-categoriei/titlul-articolului. Poti elimina ultimul slash (/) si il poti inlocui cu .html sau .htm

2. Pasul urmator, te asigur ca la sectiunile Category base si Tag base nu este completat nimic. Salvezi setarile si treci la urmatorul pas.

3. Mergi la Appearance > Editor si editezi fisierul temei tale functions.php. Acum vei introduce scriptul pentru excluderea cuvantului “category” si a cuvantului “tag” din url. Codul de mai jos il intoduci inainte de ultima linie de cod (inainte de “?>“):

 

 

add_filter(‘category_link’, ‘no_category_base’,1000,2);

function no_category_base($catlink, $category_id) {
$category = &get_category( $category_id );
if ( is_wp_error( $category ) )
return $category;
$category_nicename = $category->slug;
if ( $category->parent == $category_id ) // recursive recursion
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, ‘/’, true ) . $category_nicename;
$catlink = trailingslashit(get_option( ‘home’ )) . user_trailingslashit( $category_nicename, ‘category’ );
return $catlink;
}
// Add our custom category rewrite rules
add_filter(‘category_rewrite_rules’, ‘no_category_base_rewrite_rules’);
function no_category_base_rewrite_rules($category_rewrite) {
//print_r($category_rewrite); // For Debugging
$category_rewrite=array();
$categories=get_categories(array(‘hide_empty’=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID ) // recursive recursion
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, ‘/’, true ) . $category_nicename;
$category_rewrite[‘(‘.$category_nicename.’)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?category_name=$matches[1]&feed=$matches[2]‘;
$category_rewrite[‘(‘.$category_nicename.’)/page/?([0-9]{1,})/?$’] = ‘index.php?category_name=$matches[1]&paged=$matches[2]‘;
$category_rewrite[‘(‘.$category_nicename.’)/?$’] = ‘index.php?category_name=$matches[1]‘;
}
// Redirect support from Old Category Base
global $wp_rewrite;
$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( ‘%category%’, ‘(.+)’, $old_base );
$old_base = trim($old_base, ‘/’);
$category_rewrite[$old_base.’$’] = ‘index.php?category_redirect=$matches[1]‘;
//print_r($category_rewrite); // For Debugging
return $category_rewrite;
}
// Add ‘category_redirect’ query variable
add_filter(‘query_vars’, ‘no_category_base_query_vars’);
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = ‘category_redirect’;
return $public_query_vars;
}
// Redirect if ‘category_redirect’ is set
add_filter(‘request’, ‘no_category_base_request’);
function no_category_base_request($query_vars) {
//print_r($query_vars); // For Debugging
if(isset($query_vars[‘category_redirect’])) {
$catlink = trailingslashit(get_option( ‘home’ )) . user_trailingslashit( $query_vars[‘category_redirect’], ‘category’ );
status_header(301);
header(“Location: $catlink”);
exit();
}
return $query_vars;
}
// Refresh rules on activation/deactivation/tag changes
register_activation_hook(__FILE__,’no_tag_base_refresh_rules’);
add_action(‘created_post_tag’,’no_tag_base_refresh_rules’);
add_action(‘edited_post_tag’,’no_tag_base_refresh_rules’);
add_action(‘delete_post_tag’,’no_tag_base_refresh_rules’);
function no_tag_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
register_deactivation_hook(__FILE__,’no_tag_base_deactivate’);
function no_tag_base_deactivate() {
remove_filter(‘tag_rewrite_rules’, ‘no_tag_base_rewrite_rules’); // We don’t want to insert our custom rules again
no_tag_base_refresh_rules();
}

// Remove tag base
add_filter(‘tag_link’, ‘no_tag_base’,1000,2);
function no_tag_base($taglink, $tag_id) {
$tag = &get_tag( $tag_id );
if ( is_wp_error( $tag ) )
return $tag;
$tag_nicename = $tag->slug;

if ( $tag->parent == $tag_id ) // recursive recursion
$tag->parent = 0;
elseif ($tag->parent != 0 )
$tag_nicename = get_tag_parents( $tag->parent, false, ‘/’, true ) . $tag_nicename;

$taglink = trailingslashit(get_option( ‘home’ )) . user_trailingslashit( $tag_nicename, ‘tag’ );
return $taglink;
}

// Add our custom tag rewrite rules
add_filter(‘tag_rewrite_rules’, ‘no_tag_base_rewrite_rules’);
function no_tag_base_rewrite_rules($tag_rewrite) {
//print_r($tag_rewrite); // For Debugging

$tag_rewrite=array();
$tags=get_tags(array(‘hide_empty’=>false));
foreach($tags as $tag) {
$tag_nicename = $tag->slug;
if ( $tag->parent == $tag->tag_ID ) // recursive recursion
$tag->parent = 0;
elseif ($tag->parent != 0 )
$tag_nicename = get_tag_parents( $tag->parent, false, ‘/’, true ) . $tag_nicename;
$tag_rewrite[‘(‘.$tag_nicename.’)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$’] = ‘index.php?tag=$matches[1]&feed=$matches[2]‘;
$tag_rewrite[‘(‘.$tag_nicename.’)/page/?([0-9]{1,})/?$’] = ‘index.php?tag=$matches[1]&paged=$matches[2]‘;
$tag_rewrite[‘(‘.$tag_nicename.’)/?$’] = ‘index.php?tag=$matches[1]‘;
}
// Redirect support from Old tag Base
global $wp_rewrite;
$old_base = $wp_rewrite->get_tag_permastruct();
$old_base = str_replace( ‘%tag%’, ‘(.+)’, $old_base );
$old_base = trim($old_base, ‘/’);
$tag_rewrite[$old_base.’$’] = ‘index.php?tag_redirect=$matches[1]‘;

//print_r($tag_rewrite); // For Debugging
return $tag_rewrite;
}

// Add ‘tag_redirect’ query variable
add_filter(‘query_vars’, ‘no_tag_base_query_vars’);
function no_tag_base_query_vars($public_query_vars) {
$public_query_vars[] = ‘tag_redirect’;
return $public_query_vars;
}
// Redirect if ‘tag_redirect’ is set
add_filter(‘request’, ‘no_tag_base_request’);
function no_tag_base_request($query_vars) {
//print_r($query_vars); // For Debugging
if(isset($query_vars[‘tag_redirect’])) {
$taglink = trailingslashit(get_option( ‘home’ )) . user_trailingslashit( $query_vars[‘tag_redirect’], ‘tag’ );
status_header(301);
header(“Location: $taglink”);
exit();
}
return $query_vars;
}

4. “Update files” adica salvezi modificarile si mergi din nou la Settings > Permalinks. Mai salvezi o data in acea fereastra pentru a te asigura ca wordpress isi modifica structura generala.

2 COMENTARII

  1. Pasul 0: poposește într-un editor de text și înlocuiește ghilimelele cu unele drepte.

    Pasul 1: aruncă bucățile redundante de cod și pe ălea care au sens doar în plugin

    Pasul 2: poate vrei să scapi și de „author”.

    Uite strictul necesar:


    // remove category base
    add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
    function no_category_base_rewrite_rules($category_rewrite) {
    $category_rewrite=array();
    $categories=get_categories(array('hide_empty'=>false));
    foreach($categories as $category) {
    $category_nicename = $category->slug;
    if ( $category->parent == $category->cat_ID )
    $category->parent = 0;
    elseif ($category->parent != 0 )
    $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
    $category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
    $category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
    $category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
    }
    global $wp_rewrite;
    $old_base = $wp_rewrite->get_category_permastruct();
    $old_base = str_replace( '%category%', '(.+)', $old_base );
    $old_base = trim($old_base, '/');
    $category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
    return $category_rewrite;
    }

    // remove tag base
    add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
    function no_tag_base_rewrite_rules($tag_rewrite) {
    $tag_rewrite=array();
    $tags=get_tags(array('hide_empty'=>false));
    foreach($tags as $tag) {
    $tag_nicename = $tag->slug;
    if ( $tag->parent == $tag->tag_ID ) // recursive recursion
    $tag->parent = 0;
    elseif ($tag->parent != 0 )
    $tag_nicename = get_tag_parents( $tag->parent, false, '/', true ) . $tag_nicename;
    $tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
    $tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
    $tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
    }
    global $wp_rewrite;
    $old_base = $wp_rewrite->get_tag_permastruct();
    $old_base = str_replace( '%tag%', '(.+)', $old_base );
    $old_base = trim($old_base, '/');
    $tag_rewrite[$old_base.'$'] = 'index.php?tag_redirect=$matches[1]';
    return $tag_rewrite;
    }

    // remove author base
    add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
    function no_author_base_rewrite_rules($author_rewrite) {
    global $wpdb;
    $author_rewrite = array();
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");
    foreach($authors as $author) {
    $author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
    $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
    $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
    } return $author_rewrite;}
    add_filter('author_link', 'no_author_base', 1000, 2);
    function no_author_base($link, $author_id) {
    $link_base = trailingslashit(get_option('home'));
    $link = preg_replace("|^{$link_base}author/|", '', $link);
    return $link_base . $link;
    }

LĂSAȚI UN MESAJ