wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

Taxonomies without rewrite slugs triggering PHP notices

Open sangtlee opened this issue 3 years ago • 3 comments

When editing a taxonomy term that has 'rewrite' set to false, Yoast SEO is triggering a warning:

Notice: Trying to access array offset on value of type bool in /var/www/wp-content/plugins/wordpress-seo/admin/formatter/class-term-metabox-formatter.php on line 135

This is the line referenced in the message -- found inside the base_url_for_js() function:

$base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] );

Not sure exactly what this $base_url is used for, but adding a check for the 'rewrite' value might be good here to avoid the PHP notice...something like:

$base_url = trailingslashit( $base_url . (isset($this->taxonomy->rewrite['slug']) ? $this->taxonomy->rewrite['slug'] : '' ));

  • WordPress version: 6.0
  • Yoast SEO version: 19.2

sangtlee avatar Jul 08 '22 09:07 sangtlee

Thanks for submitting the bug report @sangtlee. Do you mind sharing the exact code snippet that you have used to create the relevant custom taxonomy with rewrite set to false so that I could deep dive into this further for you?

mmikhan avatar Jul 11 '22 15:07 mmikhan

Sure...here you go:

$job_industry_labels = array(
  'name'                       => _x( 'Job Industries', 'mytheme' ),
  'singular_name'              => _x( 'Job Industry', 'mytheme' ),
  'menu_name'                  => __( 'Job Industries', 'mytheme' ),
  'add_new_item'               => __( 'Add New Job Industry', 'mytheme' ),
  'edit_item'                  => __( 'Edit Job Industry', 'mytheme' )
);
$job_industry_args = array(
  'labels'                     => $job_industry_labels,
  'hierarchical'               => false,
  'public'                     => true,
  'show_ui'                    => true,
  'show_in_quick_edit'         => true,
  'meta_box_cb'                => false,
  'show_admin_column'          => true,
  'show_in_nav_menus'          => false,
  'show_tagcloud'              => false,
  'rewrite'                    => false,
);
register_taxonomy( 'job_industry', array( 'job' ), $job_industry_args );

The important bit is the 'rewrite' => false line.

sangtlee avatar Jul 11 '22 16:07 sangtlee

Thanks for sharing the relevant information here with me. I was able to reproduce the issue. It's definitely specific to the code that you are referring to:

https://github.com/Yoast/wordpress-seo/blob/999850e9cbe6d0ebe1360b7d9cb0a685c8dbb098/admin/formatter/class-term-metabox-formatter.php#L136

In this case, we can either use the singular name to get the taxonomy name or switch only when the rewrite is set to false. So, it could be something like the following:

Possible solution 1

$base_url = trailingslashit( $base_url . strtolower($this->taxonomy->labels->{'singular_name'}) );

Possible solution 2

if ( $this->taxonomy->rewrite ) {
	$base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] );
} else {
	$base_url = trailingslashit( $base_url . strtolower( $this->taxonomy->labels->{'singular_name'} ) );
}

Both options are fine but possible solution 1 seems to do the job without complicating things. I mark this report as a bug and will see what we can do with it.

mmikhan avatar Jul 11 '22 23:07 mmikhan