Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 13:00 9:00 – 13:00 9:00 – 13:00 8:00 – 12:00 8:00 – 12:00 -
- 14:00 – 17:00 14:00 – 18:00 14:00 – 18:00 13:00 – 17:00 13:00 – 17:00 -

Supporter timezone: Europe/Zagreb (GMT+02:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Bruno Kos 1 year, 3 months ago.

Assisted by: Bruno Kos.

Author Posts
January 4, 2024 at 1:35 pm #15150724

Raúl

Categories slug is manually removed from WP URLs using the custom code below,

When installing WPML, the category slug is back again as if this code is ignored :

function remove_category_base( $category_link, $category ) {
if ( 'category' === $category->taxonomy ) {
$category_link = str_replace( '/' . $category->taxonomy . '/', '/', $category_link );
}
return $category_link;
}

add_filter( 'term_link', 'remove_category_base', 10, 2 );

function rewrite_rules_array_remove_category_base( $rules ) {
$new_rules = array();
foreach ( $rules as $regex => $rule ) {
if ( strpos( $rule, 'category/' ) !== false ) {
$new_rules[ $regex ] = str_replace( 'category/', '', $rule );
} else {
$new_rules[ $regex ] = $rule;
}
}
return $new_rules;
}

add_filter( 'rewrite_rules_array', 'rewrite_rules_array_remove_category_base' );

function change_category_base() {
if ( 'category' === get_option( 'category_base' ) ) {
update_option( 'category_base', '/' );
}
}

add_action( 'init', 'change_category_base' );

Do we have a similar code that we can apply for WPML to ignore the categories slug?

I Want to remove the slug the beginning slug for categories in WordPress

For example in English it would be

hidden link

I Want to turn it into

hidden link

In Spanish it would be

hidden link

I want it to be

hidden link

January 5, 2024 at 12:41 pm #15154432

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+02:00)

What if instead of using such filters you change the rewrite parameter on the taxonomy directly, given that it is a custom one?

So something like:

function register_custom_taxonomy() {
    $labels = array(
        'name' => _x('Custom Categories', 'taxonomy general name'),
        // ... other labels ...
    );

    $args = array(
        'hierarchical' => true, // Set to true if you want taxonomy to be hierarchical like categories, set to false for tags-like behavior
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => '/', 'with_front' => false), // This removes the base slug from URL
    );

    register_taxonomy('custom_category', array('post'), $args);
}
add_action('init', 'register_custom_taxonomy');

The topic ‘[Closed] Remove category slug from WP URLs when using WPML’ is closed to new replies.