admin管理员组

文章数量:1278983

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'
                ],

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'
                ],
Share Improve this question edited Nov 12, 2021 at 16:45 Jon Chubb asked Nov 9, 2021 at 14:58 Jon ChubbJon Chubb 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Block error posttaxsobj is not a valid property of Object