Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yoast resets slug of custom post when permalink is customized using post_type_link #7800

Open
wprosrb opened this issue Aug 30, 2017 · 5 comments

Comments

@wprosrb
Copy link

wprosrb commented Aug 30, 2017

Please give us a description of what happened.

Yoast does not recognize a custom post type's permalink customization. If you "Edit snippet" using Yoast, the slug is blank and will reset the slug the user previously set.

Please describe what you expected to happen and why.

Yoast should not reset the slug, the custom slug should be prepopulating into Yoast's slug field, and the permalink Yoast shows should show the proper URL.

How can we reproduce this behavior?

  1. Start with default WordPress, Yoast SEO
  2. Create a basic custom post type and customize that type's URL (see "for functions.php" code below)
  3. Create a post of this type, customize the slug, save.
  4. Edit page title via Yoast--notice Yoast's slug field is blank--and save.
  5. Your custom slug was lost.

For functions.php:

add_action('init', 'cptCreate' );
add_action('init', 'cptRewrite');
add_filter( 'post_type_link', 'cptLink' , 10, 2 );
function cptCreate() {
	register_post_type('cpt', array(
		'label'=>"cpt",
		'public'=>true,
		'hierarchical' => true));
}
function cptRewrite() {
	add_rewrite_rule('^prefix/([^/]+)/?', 'index.php?post_type=cpt&pagename=/$matches[1]', 'top');
}
function cptLink( $post_link, $id = 0 ) {
	$post = get_post($id);
	if(is_wp_error($post) || 'cpt' != $post->post_type || empty($post->post_name)){
		return $post_link;
	}
	return home_url(user_trailingslashit( "prefix/{$post->post_name}" ));
}

Technical info

  • WordPress version: 4.8.1
  • Yoast SEO version: 5.3.3
  • Relevant plugins in case of a bug: none
@s8m2s
Copy link

s8m2s commented Aug 30, 2017

For my site I'm adding custom fields into the permalink using the same method as above. If I try to hook into post type link and return a value. It basically hoses my permalink if I ever edit yoast meta data.

@s8m2s
Copy link

s8m2s commented Aug 31, 2017

I'm currently doing this as a workaround. Added to the functions.php file

This is grabbing the current slug, and adding it to yoast slug field when the yoast panel is clicked.

function yoastPermalinkSlugWorkaround(){
	echo '<script>
		try {
			document.getElementById("wpseo_meta").onclick = function() {
				var yoast_slug = document.getElementById("snippet-editor-slug");
				if (yoast_slug.value.length == 0) {
					yoast_slug.value = document.getElementById("post_name").value;
				}
			};
		} catch(err) {};
	</script>';
}

@benvaassen
Copy link
Contributor

Thank you for reporting.
We've labeled this issue needs decision which means our product managers will assess your request.

@jdevalk
Copy link
Contributor

jdevalk commented Jan 20, 2018

I consider this a bug.

@JorindeReijnierse
Copy link

Do you have an idea if this is going to be solved in the near future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants