KLicheR/wp-polylang-translate-rewrite-slugs

Preview button does not work in combination with this plugin

Opened this issue · 2 comments

Hey KLicheR,

First of: amazing work on adding the possibility for archive slugs! it works great!

I have a question: when using your plugin, i am not able to preview a post.

When i create a news post with slug "test-page" and i click Preview i am redirected to http://testsite.com/nieuws/test-page?preview=true and i get an 404 page. This should be http://testsite.com/nl/nieuws/test-preview?preview=true (because my default language is NL).

I tried looking for a clue but didnt find one yet. Can you help?

Thanks!

I got the same issue and I found the problematic function. It's the post_type_link_filter here:
https://github.com/KLicheR/wp-polylang-translate-rewrite-slugs/blob/master/polylang-translate-rewrite-slugs.php#L158

When the post is a draft it looks like the $post-post_name is not defined so I think it might be good to take the same condition as the one in worpdress > wp-includes/link-template.php > get_post_permalink:

$draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );

and then do somethink like this:

// Check if the post type is handle.
if (( !$draft_or_pending || $sample ) && isset($this->post_types[$post->post_type])) {
  // Build URL. Lang prefix is already handle.
  return home_url('/'.$this->post_types[$post->post_type]->translated_slugs[$lang]->rewrite['slug'].'/'.($leavename?"%$post->post_type%":$post->post_name));
}

This change fix the issue for me but since I don't understand why this function exist and the usage of the $leavename and $sample parameters this might as well break other things.

If this looks right and doesn't break everything I can do a pull request : )

edit: I found one issue with the proposed solution. When you try to access the draft preview as anonymous there is a redirect loop... (the url looks like mywordpress/?post_type=project&p=15426 ). Without the modification I'm redirected to the archive page.

Nice find @idflood !