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

Using Taxonomy field in Gutenberg panel, does not update Post URL #924

Open
crstauf opened this issue Aug 1, 2024 · 0 comments
Open

Using Taxonomy field in Gutenberg panel, does not update Post URL #924

crstauf opened this issue Aug 1, 2024 · 0 comments

Comments

@crstauf
Copy link

crstauf commented Aug 1, 2024

Describe the bug
When using the Taxonomy field to replace the core panel for assigning taxonomy terms, and the permalink includes the taxonomy term slug, on Publish the URLs to view the Post in Gutenberg are not updated.

To Reproduce
Steps to reproduce the behavior:

  1. Create a field group with Taxonomy field
  2. Update Permalink structure to include slug of assigned term from taxonomy used in Taxonomy field (%{taxonomy}%)
  3. Disable the core panel for assigning terms
  4. Add filter on post_link to replace the %{taxonomy}% with the assigned term's slug
  5. Create a new Post, verifying core taxonomy panel is not available, and ACF panel with Taxonomy field is available
  6. Select a term in ACF Taxonomy field
  7. Publish the Post
  8. Notice the URL is not updated with the assigned term's slug

Expected behavior
Post's URL is updated with the slug of the assigned term immediately on Publish/Update.

Screenshots or Video
If applicable, add screenshots or video to help explain your problem.

Code

Disable core panel:

wp.data.dispatch( 'core/edit-post' ).removeEditorPanel( 'taxonomy-panel-{taxonomy}' );

Filter URL:

add_filter( 'post_link', static function ( string $permalink, \WP_Post $post, bool $leavename ): string {
	if ( 'post' !== $post->post_type ) {
		return $permalink;
	}

	if ( ! str_contains( $permalink, '%{taxonomy}%' ) ) {
		return $permalink;
	}

	$types = get_the_terms( $post, '{taxonomy}' );

	if ( empty( $types ) ) {
		return $permalink;
	}

	$term = reset( $types );

	return str_replace( "%{taxonomy}%", $term->slug, $permalink );
}, 10, 3 );

Version Information:

  • WordPress Version 6.5.3
  • PHP Version 8.2
  • ACF PRO 6.3.0.1
  • Brave Version 1.66.110 Chromium: 125.0.6422.60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant