gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Block Editor is not using the wp_terms_checklist_args filter

Open theMikeD opened this issue 7 years ago • 11 comments

Describe the bug There is a filter called wp_terms_checklist_args that allows me to modify how a taxonomy's terms are shown to the end user on the post edit screen. This filter works fine in the classic editor, but is not called with gutenberg and I can't seem to find a way to do it using filters. This is happening with WP 5.2.2 with the default theme and no plugins other that Classic Editor active.

To reproduce Steps to reproduce the behaviour:

  1. Create a new WP install using 5.2.2.
  2. Install the Classic Editor plugin and select 'Allow users to switch editors'
  3. Add the following code to the default theme's functions.php
function restrict_metabox_selections( $opts, $post_id ) {
	error_log( __FUNCTION__ );
	return $opts;
}
add_filter( 'wp_terms_checklist_args', 'restrict_metabox_selections', 10, 2 );
  1. Open the php error log
  2. Edit any post in the Classic Editor Result: You will see restrict_metabox_selections in the log, indicating that the filter is being called.
  3. Edit any post in the Block Editor Result: You will not see restrict_metabox_selections in the log, indicating that the filter is not being called.

Expected behavior The filter should be called by both Classic and Block editors.

Desktop (please complete the following information):

  • OS: OSX
  • BrowserChrome, Firefox
  • Version Latest

theMikeD avatar Aug 06 '19 21:08 theMikeD

The block editor uses the REST API to retrieve a list of tags, so the wp_terms_checklist_args does not work in that context.

What do you want to achieve? Maybe there's another way to do what you're looking for.

swissspidy avatar Aug 07 '19 12:08 swissspidy

Ah well that would explain it. I have a two-level hierarchical taxonomy and I restrict the user from selecting the parent, only the children, by removing the checkbox for the parent in the Walker.

Here is what that filter allows me to do in the classic editor: https://pasteboard.co/IrAw6yK.jpg Notice how the top-level terms ( "Cakemakers and Bakers" for example) are not selectable.

Here is what happens in the block editor https://pasteboard.co/IrAwCaz.jpg

theMikeD avatar Aug 07 '19 12:08 theMikeD

My similar problem is to modify the post-new.php screen. I have a custom taxonomy, and I want to default certain terms as "checked" or not. I think I can use wp_terms_checklist_args in classic, but I don't know how to use it in Gutenberg.

JohnDeeBDD avatar Aug 12 '19 02:08 JohnDeeBDD

Is there a new way to disable moving checked terms out of the hierarchy and to the top of the list when editing a post? I used the wp_terms_checklist_args hook with $args['checked_ontop'] = false pre-Gutenberg, but it doesn't work with Gutenberg.

bridgetwes avatar Oct 16 '19 20:10 bridgetwes

@bridgetwes Thats another good use case

theMikeD avatar Oct 25 '19 19:10 theMikeD

This seems to still be a problem. After some more investigation, I see that I can use wp.hooks.addFilter to call a function that can then determine if the taxonomy I want is present, and then to run something if it is, like this

	var el = wp.element.createElement;

	function restrict_taxonomy_selections( OriginalComponent ) {
		return function( props ) {
			if ( props.slug === 'resource' ) {
				return el(
					OriginalComponent, // this needs to get replaced with something
					props
				);
			} else {
				return el(
					OriginalComponent,
					props
				);
			}
		}
	}

	wp.hooks.addFilter(
		'editor.PostTaxonomyType',
		'restrict-taxonomy-selections',
		restrict_taxonomy_selections
	);

However as you can see, the line that needs to be replaced...that's where I'm stuck. Based on other sleuthing on the net it seems as if I have to completely duplicate the hierarchical-term-selector.js file found here (along with the imported utils/terms.js) and then modify that some how.

So as a test, I copied that file out of GB, renamed it and imported it like this

import NoParentSelectableComponent from './NoParentSelectableComponent';

but when I do this (without any changes to the file as copied other than adjusting line 33 / where it finds terms ) I get a build error from NPM:

$ npm run build

> [email protected] build /Volumes/EVO/Active_Sites/RA-Biz/SITE/wp-content/mu-plugins/site-core-functionality/js
> wp-scripts build

Hash: 149c84580f6618334a20
Version: webpack 4.41.0
Time: 59ms
Built at: 2020-02-06 5:54:34 p.m.
 1 asset
Entrypoint main = gutenberg-restrict-taxonomy-selections-min.js
[0] ./src/NoParentSelectableComponent.js 456 bytes {0} [built] [failed] [1 error]
[1] ./src/gutenberg-restrict-taxonomy-selections.js 1.13 KiB {0} [built]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in ./src/NoParentSelectableComponent.js 369:4
Module parse failed: Unexpected token (369:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 		return renderedTerms.map( ( term ) => {
| 			return (
> 				<div
| 			key={ term.id }
| 			className="editor-post-taxonomies__hierarchical-terms-choice"
 @ ./src/gutenberg-restrict-taxonomy-selections.js 7:0-72
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `wp-scripts build`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mike/.npm/_logs/2020-02-06T22_54_34_446Z-debug.log

That and my editor sees a problem on that line too, but I'm not skilled enough yet with this to understand why.

Any help would be greatly appreciated.

theMikeD avatar Feb 06 '20 23:02 theMikeD

+1

Has anyone figured out a work around?

karneaud avatar May 29 '20 02:05 karneaud

@theMikeD @karneaud You need to add a walker, look here

severineFF-tanaguru avatar Jan 27 '21 16:01 severineFF-tanaguru

@theMikeD @karneaud You need to add a walker, look here

The entire point of this ticket is that the wp_terms_checklist_args was not being called in gutenberg, and the very first line of code in your linked gist is to add to the wp_terms_checklist_args filter. So no, this won't work either.

theMikeD avatar Jan 27 '21 17:01 theMikeD

Has anyone figured it out? I have a custom taxonomy, and I want to default certain terms as checked initially.

mahbubme avatar Nov 24 '23 22:11 mahbubme

As @swissspidy correctly notes, the block editor uses the REST API and so will not call wp_terms_checklist_args. You'll need to use REST API filters or block editor filters to customise taxonomy selection.

You could also write your own custom taxonomy selection React component using the editor.PostTaxonomyType filter but that's quite advanced so I'll avoid doing that in my response here.

I have a two-level hierarchical taxonomy and I restrict the user from selecting the parent, only the children, by removing the checkbox for the parent in the Walker.

One solution for this is to add a filter that causes the REST API to only return terms in the taxonomy that have no children.

add_filter(
	'rest_custom_taxonomy_query', // Replace custom_taxonomy with the name of the custom taxonomy.
	function ( $args, $request ) {
		$args['childless'] = true;
		return $args;
	},
	10,
	2
);

https://developer.wordpress.org/reference/hooks/rest_this-taxonomy_query/

Unfortunately the block editor does not support displaying hierarchical terms unless all of the term's parents are returned from the REST API, so for this to work we'll need to force the block editor to treat our taxonomy as if it were not hierarchical.

const el = React.createElement;
const FlatTermSelector = wp.editor.PostTaxonomiesFlatTermSelector;

function customizeTrackSelector( OriginalComponent ) {
	return function ( props ) {
		if ( props.slug === 'custom_taxonomy' ) {  // Replace custom_taxonomy with the name of the custom taxonomy.
			return el( FlatTermSelector, props );
		}
		return el( OriginalComponent, props );
	};
}

wp.hooks.addFilter(
	'editor.PostTaxonomyType',
	'my-plugin/set-hierarchical-term-selector',
	customizeTrackSelector
);

https://github.com/WordPress/gutenberg/blob/trunk/packages/editor/src/components/post-taxonomies/README.md

My similar problem is to modify the post-new.php screen. I have a custom taxonomy, and I want to default certain terms as "checked" or not. I think I can use wp_terms_checklist_args in classic, but I don't know how to use it in Gutenberg.

Has anyone figured it out? I have a custom taxonomy, and I want to default certain terms as checked initially.

One solution for this is to add a hook that adds terms to the post that WordPress creates behind the scenes when you create a new post. Those terms will then appear as checked in the block editor.

add_action(
	'wp_after_insert_post',
	function ( $post_id, $post, $update ) {
		if ( ! $update && $post->post_status === 'auto-draft' ) {
			wp_set_post_terms(
				$post_id,
				array( 8, 12 ), // Replace these numbers with the IDs of the terms you want posts to receive by default. You can use term_exists() to get the ID from a slug.
				'custom_taxonomy' // Replace custom_taxonomy with the name of the custom taxonomy.
			);
		}
	},
	10,
	3
);

https://developer.wordpress.org/reference/functions/wp_after_insert_post/ https://developer.wordpress.org/reference/functions/wp_set_post_terms/

I hope this is helpful. I'll close this issue as answered but feel free to reply if you have further questions.

noisysocks avatar Apr 02 '24 06:04 noisysocks