grappler / polylang-slug

A unique post slug within the language in Polylang. This allows for a page to have same/identical slug in multiple languages.
275 stars 84 forks source link

404 Error in combination with Polylang Translate Rewrite Slugs #26

Closed MALIK-0 closed 8 years ago

MALIK-0 commented 8 years ago

Hi and thanks for the great plugin!

I'm facing an issue while using your Plugin "Polylang-Slug" with the "Polylang Translate Rewrite Slugs" Plugin.

WordPress: 4.4.2 Polylang: 1.8.5 Polylang-Slug: 0.2.0 Polylang-Translate-Rewrite-Slugs: 0.3.6

This is what I want to archive, please notice that the custom-post-type-slug is translateable (wp-polylang-translate-rewrite-slugs/issues plugin should do this) and the custom-post-title is not unique (what your plugin does).

http://example.com/{custom-post-type-slug-translateable}/{custom-post-title-not-unique} (variables) http://example.com/references/customer-xyz (real example, default language english) http://example.com/de/referenzen/customer-xyz (real example, translated to german)

Both plugins are working perfect, if used without the other plugin, but if I activate both and refresh my permalinks i get the following behaviour:

In the backend the permalinks look like they should: http://example.com/references/customer-xyz (real example, default language english) http://example.com/de/referenzen/customer-xyz (real example, translated to german)

But if I visit those pages i get an 404 error. Could you please have a look into this?

There are many use cases for this structure like:

http://example.com/products/identic-product-name http://example.com/produkte/identic-product-name

and so on.

I'll also mention this on the plugin site of "Polylang Translate Rewrite Slugs".

If I can help you anyhow, just let me know, I'm willed to help.

grappler commented 8 years ago

Strange. I tested both plugins on a test site and it worked fine. The two URLS being : /de/produkt/test/ /en/product/test/

This is the code that I used to test it.

add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
    // Add translation for "product" post type.
    $post_type_translated_slugs = array(
        'product' => array(
            'de' => array(
                'has_archive' => true,
                'rewrite' => array(
                    'slug' => 'produkt',
                ),
            ),
            'en' => array(
                'has_archive' => true,
                'rewrite' => array(
                    'slug' => 'product',
                ),
            ),
        ),
    );
    return $post_type_translated_slugs;
});
// Register Custom Post Type
function products_post_type() {

    $labels = array(
        'name'                  => _x( 'Products', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Products', 'text_domain' ),
        'name_admin_bar'        => __( 'Product', 'text_domain' ),
        'archives'              => __( 'Item Archives', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Product:', 'text_domain' ),
        'all_items'             => __( 'All Products', 'text_domain' ),
        'add_new_item'          => __( 'Add New Product', 'text_domain' ),
        'add_new'               => __( 'New Product', 'text_domain' ),
        'new_item'              => __( 'New Item', 'text_domain' ),
        'edit_item'             => __( 'Edit Product', 'text_domain' ),
        'update_item'           => __( 'Update Product', 'text_domain' ),
        'view_item'             => __( 'View Product', 'text_domain' ),
        'search_items'          => __( 'Search products', 'text_domain' ),
        'not_found'             => __( 'No products found', 'text_domain' ),
        'not_found_in_trash'    => __( 'No products found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Product', 'text_domain' ),
        'description'           => __( 'Product information pages.', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'custom-fields', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'product', $args );

}
add_action( 'init', 'products_post_type' );
MALIK-0 commented 8 years ago

Thanks for testing this @grappler. I tried your code, but got the same result as before (404). But since your code is working for you, it should work for me too. I'll search further, maybe its another plugin that conflicts etc. I will keep you informed here.

grappler commented 8 years ago

Ok, it may be good to disabled all of your plugins except for the those related to Polylang and perhaps even switch to one of the default themes. Have you enabled WP_DEBUG?

MALIK-0 commented 8 years ago

Doing this right now, WP_DEBUG is set to true.

MALIK-0 commented 8 years ago

Wow that's strange! I deactivated all plugins except this 3:

I still get an 404 on my and even on your CPT. Couldn't try a default theme cause people are currently working on the content. Do you have any idea how I can narrow this down?

Here is the code i used for my post type (was generated by the plugin: Custom Post Type UI:)

add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
    $labels = array(
        'name' => 'Kunden',
        'singular_name' => 'Kunde',
        'menu_name' => 'Kunden',
        'all_items' => 'Alle Kunden',
        'add_new' => 'Neuen Kunden hinzufügen',
        'edit' => 'Bearbeiten',
        'edit_item' => 'Kunde bearbeiten',
        'new_item' => 'Neuer Kunde',
        'view' => 'Anzeigen',
        'view_item' => 'Kunde anzeigen',
        'search_items' => 'Kunden suchen',
        'not_found' => 'Keinen Kunden gefunden',
        'not_found_in_trash' => 'Keine Kunden im Papierkorb gefunden',
        'parent' => 'Übergeordnete Kunden',
        );

    $args = array(
        'labels' => $labels,
        'description' => 'Darstellung der Referenzen.',
        'public' => true,
        'show_ui' => true,
        'show_in_rest' => false,
        'has_archive' => false,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'referenzen', 'with_front' => false ),
        'query_var' => true,
        'menu_position' => 6,'menu_icon' => 'dashicons-groups',     
        'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'revisions', 'thumbnail', 'author', 'page-attributes', 'post-formats' ),        
        'taxonomies' => array( 'reference_category' ),      
    );
    register_post_type( 'customer', $args );
}

// Add translation for post slugs
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
    $post_type_translated_slugs = array(
        'customer' => array(
            'de' => array(
                'has_archive' => true,
                'rewrite' => array(
                    'slug' => 'referenzen',
                ),
            ),
            'en' => array(
                'has_archive' => true,
                'rewrite' => array(
                    'slug' => 'references',
                ),
            ),
        ),
    );
    return $post_type_translated_slugs;
});
MALIK-0 commented 8 years ago

Update: Tested my theme with the Theme Check Plugin and coulnd't find any warnings or errors that could cause this strange behaviour.

grappler commented 8 years ago

The Theme Check plugin won't tell you much. For a previous user it did not work as the query was not set correctly.

I could perhaps test your theme on my test site.

Query Monitor should also be help to find out where the issue is when loading the page. https://wordpress.org/plugins/query-monitor/

MALIK-0 commented 8 years ago

I've installed the query-monitor plugin on WordPress TwentySixteen theme with only 4 Plugins (Query-Monitor and the three Polylang plugins) and your code (so i should have the same or similar setup). Can you tell me for what I should look in the Queries? I tried to search the query output for the post id, but got no result.

May I kindly ask you too look at this (Updated Screenshot and removed black rectangle) screenshot? It shows the whole report. (Updated Screenshot, removed black rectangle).

MALIK-0 commented 8 years ago

@grappler Looks like I found out what creates this issue:

In my settings -> permalinks I got the following settings:

Custom: /news/%postname%/

If I remove "/news/" the url is working. Do you have any idea, how I can keep the "/news/" for my blog posts? Btw I wanted to thank you for taking time to help me!

MALIK-0 commented 8 years ago

Okay, I solved that. So for everyone else who is facing similar issues. If you use "Polylang Translate Rewrite Slug" and don't want the front (in my case /news/) added to your translated CPT-Slugs then configure your translation as follows.

// Add translation for post slugs
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
    $post_type_translated_slugs = array(
        'customer' => array(
            'de' => array(
                'has_archive' => false,
                'rewrite' => array(
                    'slug' => 'referenzen',
                    'with_front' => false,
                ),
            ),
            'en' => array(
                'has_archive' => false,
                'rewrite' => array(
                    'slug' => 'references',
                    'with_front' => false,
                ),
            ),
        ),
    );
    return $post_type_translated_slugs;
});

The important part is "with_front => false". I did not know that the plugin overwrites the settings i did while registering the cpt. But thats the case, hope that some finds this useful.

And again a big thanks to @grappler who helped me out. I'll close this :)

grappler commented 8 years ago

Ah, great. Pleased it worked out :smile: