0

One of our blocks has stopped working, I understand its probably due to the 'post_taxs_obj' not being included in the PHP register_block_type attributes but I'm not sure how to resolve this - can anyone please advise? (I'm not a dev, just trying to jump in and fix this so any help would be much appreciated):

Error:

{"code":"rest_invalid_param","message":"Invalid parameter(s): attributes","data":{"status":400,"params":{"attributes":"post_taxs_obj is not a valid property of Object."},"details":{"attributes":{"code":"rest_additional_properties_forbidden","message":"post_taxs_obj is not a valid property of Object.","data":null}}}}

Here's the PHP:

register_block_type( 'performancein/article-listing',
        array(
            'render_callback' => 'performancein_article_listing_callback',
            'attributes'      => [
                'post_type'            => [
                    'default' => '',
                    'type'    => 'string'
                ],
                'post_taxs'            => [
                    'default' => '',
                    'type'    => 'string'
                ],
                'post_category'        => [
                    'default' => '',
                    'type'    => 'string'
                ],
                'number_of_post'       => [
                    'default' => '0',
                    'type'    => 'number'
                ],
                'category_description' => [
                    'default' => false,
                    'type'    => 'bool'
                ],
                'exclude_post'         => [
                    'default' => '',
                    'type'    => 'string'
                ],

            ]
        )
    );

And the JS:

registerBlockType('performancein/article-listing', {
  title: 'Article Listing',
  icon: ArticleIcon,
  category: 'performancein',
  keywords: [__('ArticleFilter'), __('gutenberg'), __('performancein')],
  description: __( 'It showcase article listing ' ),
  example: {
    attributes: {
      caption: __( 'Article Listing' ),
    },
  },
  attributes: {

    number_of_post: {
      type: 'number',
      default: '0',
    },
    post_type: {
      default: '',
      type: 'string',
    },
    post_type_obj: {
      type: 'json',
    },
    post_taxs: {
      default: '',
      type: 'string',
    },
    post_taxs_obj: {
      default: [{label: '--Select Taxonomy--', value: ''}],
      type: 'json',
    },
    post_category: {
      default: '',
      type: 'string',
    },
    post_category_obj: {
      default: [{label: '--Select Category--', value: ''}],
      type: 'json',
    },
    category_description: {
      default: false,
      type: 'boolean',
    },
    exclude_post: {type: 'string'},
    design_option: {
      type: 'string',
      default: '',
    },
  },

UPDATE

Thanks for the fix Tom, solution was simply to follow his guidance with the format of:

'post_type_obj'        => [
                    'default' => '',
                    'type'    => 'json'
                ],

1 Answer 1

0

Your PHP registers a block with 6 attributes.

Your JS registers a block with 10 attributes.

They must match, it is non-negotiable. You can't claim one thing in PHP then say another in javascript. Your code lied to one of the APIs.

You can either:

  • add the missing attributes to PHP
  • or remove the extra attributes from javascript

Most likely the first one.

As an aside, have you considered using server side registration with a block.json and sidestepping the problem completely? Then your JS registration would only need 3 things, the blocks name and its edit/save components, everything else would be pulled automatically from block.json, and WP would inline all that data and use it automatically. It would even enqueue the frontend and editor scripts for you.

2
  • Thanks Tom – I couldn't paste a code sample properly here in a comment so I've added an answer below. Would you mind casting your eye over it please!? Thanks again
    – Jon Chubb
    Nov 9, 2021 at 17:43
  • Thanks Tom, I've updated the original question as you requested. Apologies for getting that wrong
    – Jon Chubb
    Nov 11, 2021 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.