Repository navigation
Fatal error in wp_insert_term() when the term name is not a string - #13521
josephscott wants to merge 1 commit into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Tested and Verified ✅Environment:
Verification:
Regarding the discussion on JSON Schema adoption for XML-RPC: while a future schema-driven sanitization layer for XML-RPC is a worthy initiative, this PR addresses a real PHP 8+ fatal error in |
|
|
||
| $term_names = $post_data['terms_names'][ $taxonomy ]; | ||
| foreach ( $term_names as $term_name ) { | ||
| if ( ! is_string( $term_name ) ) { |
| if ( '' === trim( $term ) ) { | ||
| if ( ! is_scalar( $term ) || '' === trim( $term ) ) { |
There was a problem hiding this comment.
Is this just a hardening? Is it needed for the above fix. PHPStan isn't complaining here because $term comes from the return value of the pre_insert_term filter which is typed as string|WP_Error. It is first checked to see if it is a WP_Error in which it returns, so then the type narrows to string.
I think this is a case where the callers should be fixing the types that they pass into the wp_insert_term() function.
There was a problem hiding this comment.
For the XML-RPC case it is not strictly needed.
It is first checked to see if it is a WP_Error in which it returns, so then the type narrows to string.
I don't think that is accurate, which is why we have an is_int() call as well - https://github.com/WordPress/wordpress-develop/blame/trunk/src/wp-includes/taxonomy.php#L2595
Anything that calls wp_insert_term() directly can cause a problem. Here is an example without this fix:
$ npm run env:cli --silent -- eval 'var_dump( wp_insert_term( array( "foo" ), "post_tag" ) );'
Fatal error: Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, array given in /var/www/src/wp-includes/taxonomy.php:2599
Stack trace:
#0 /var/www/src/wp-includes/taxonomy.php(2599): trim(Array)
#1 phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(39) : eval()'d code(1): wp_insert_term(Array, 'post_tag')
#2 phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(39): eval()
#3 [internal function]: Eval_Command->__invoke(Array, Array)
#4 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func(Array, Array, Array)
#5 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
#6 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(497): call_user_func(Object(Closure), Array, Array)
#7 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(470): WP_CLI\Dispatcher\Subcommand->invoke(Array, Array, Array)
#8 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(493): WP_CLI\Runner->run_command(Array, Array)
#9 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(136): WP_CLI\Runner->run_command_and_exit()
#10 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1228): WP_CLI\Runner->do_early_invoke('before_wp_load')
#11 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#12 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(84): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#13 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(35): WP_CLI\bootstrap()
#14 phar:///usr/local/bin/wp/php/boot-phar.php(20): include('phar:///usr/loc...')
#15 /usr/local/bin/wp(4): include('phar:///usr/loc...')
#16 {main}
thrown in /var/www/src/wp-includes/taxonomy.php on line 2599
Error: There has been a critical error on this website.Learn more about troubleshooting WordPress. There has been a critical error on this website.
And here is the same call with this fix:
$ npm run env:cli --silent -- eval 'var_dump( wp_insert_term( array( "foo" ), "post_tag" ) );'
object(WP_Error)#2540 (3) {
["errors"]=>
array(1) {
["empty_term_name"]=>
array(1) {
[0]=>
string(33) "A name is required for this term."
}
}
["error_data"]=>
array(0) {
}
["additional_data":protected]=>
array(0) {
}
}
| * @param mixed $term The term name to insert. | ||
| */ | ||
| public function test_wp_insert_term_non_scalar_term_name( $term ): void { | ||
| $found = wp_insert_term( $term, 'post_tag' ); |
| * @param int|float $term The term name to insert. | ||
| */ | ||
| public function test_wp_insert_term_numeric_term_name( $term ): void { | ||
| $found = wp_insert_term( $term, 'post_tag' ); |
| public function test_wp_insert_term_numeric_term_name( $term ): void { | ||
| $found = wp_insert_term( $term, 'post_tag' ); | ||
|
|
||
| $this->assertNotWPError( $found ); |
There was a problem hiding this comment.
Slightly better:
| $this->assertNotWPError( $found ); | |
| $this->assertIsArray( $found ); |
Due to:
However, this will become irrelevant with #13614. So feel free to leave alone.
This comment was marked as low quality.
This comment was marked as low quality.
Sorry, something went wrong.
There was a problem hiding this comment.
@robelsust This looks like an AI-generated review comment without disclosure on the tooling or model used. So I'm hiding the comment.
There was a problem hiding this comment.
Apologies for that, @westonruter. I used AI assistance to help formulate the review notes after testing the PHP 8.3 runtime behavior locally, and I omitted the required AI disclosure block.
Use of AI Tools
- AI assistance: Yes
- Used for: Helping summarize review notes after local testing on PHP 8.3; reviewed and posted by me.
Completely understand hiding the comment to uphold core review standards. Thanks for keeping the standards high!



https://core.trac.wordpress.org/ticket/66108
AI assistance: Yes
Tool(s): Claude Code
Model(s): Fable 5.1
Used for: Research and experimentation. Guided directions on writing the code changes. It wrote the tests.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.