I created the function below that changes the permalink structure of posts in a specific category. Everything is working, but the button inside the post to edit the slug is gone.
Print edit button image
add_filter( 'post_link', 'idinheiro_custom_permalink', 10, 3 );
function idinheiro_custom_permalink( $permalink, $post ) {
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->slug == "negocios" ) {
$permalink = trailingslashit( home_url('/'. $category[0]->slug . '/' . $post->post_name .'/' ) );
}
return $permalink;
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The post_link hook has a third parameter named $leavename:
$leavename
(bool) Whether to keep the post name.
So what that means is, if $leavename is true, then the post name/slug should be kept in the permalink and thus it needs to contain %postname% (or %pagename% for the page post type) instead of being replaced with the actual post slug (or something else).
Because if that placeholder is missing, the permalink will become non-editable on the post editing screen, therefore the Edit button is disabled. (See get_sample_permalink_html(), specifically this part, and that function is the one which generates the post permalink editor)
So to fix the issue, define the variable: function idinheiro_custom_permalink( $permalink, $post, $leavename ) and change the '/' . $post->post_name .'/' to:
'/' . ( $leavename ? '%postname%' : $post->post_name ) .'/'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
