JavaScript for WordPress Forums Gutenberg Development Saving Post Meta on Custom Post type not working

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #107452
    Barry Ross Jr
    Participant

    Hello,

    I’m trying to save post meta for a custom post type in Gutenberg, but am not able using steps you provided and wondering if you could take a look at the issue am having.

    php registering the post type + meta
    <?php 
    namespace cmg\schema_reviews;
    
    add_filter('the_content', __NAMESPACE__.'\cmg_the_content');
    function cmg_the_content($content){
    	if (is_singular('cmg_review_pt')){
    		$company_name = get_bloginfo('blogname');
    		$title = get_the_title();
    		$content = '
    		<div class="review-row" itemscope itemtype="http://schema.org/Review">
    		<meta itemprop="author" content="'.$title.'" />
    		<meta itemprop="itemReviewed" content="'.$company_name.'" />
    		'.$content.'
    		</div>';
    	}
    	return $content;
    }
    add_action('init', __NAMESPACE__.'\build_cpt');
    function build_cpt(){
    	$singular = 'Review';
    	$plural = 'Reviews';
    	register_post_type(strtolower('cmg_review_pt'), array(
    		'labels' => array(
    			'name' => $plural,
    			'singular' => $singular,
    			'add_name' => sprintf('Add New %s', $singular),
    			'add_new_item' => sprintf('Add New %s', $singular),
    			'edit_item' => sprintf('Edit %s', $singular),
    			'new_item' => sprintf('New %s', $singular),
    			'view' => sprintf('View %s', $singular),
    			'view_item' => sprintf('View %s', $singular),
    			'search_term' => sprintf('Search %s', $plural),
    			'parent' => sprintf('Parent %s', $singular),
    			'not_found' => sprintf('No %s found', $singular),
    			'not_found_in_trash' => sprintf('No %s in Trash', $plural),
    		),
    		'public' 			  => true,
    		'publicly_queryable'  => true,
    		'exclude_from_search' => false,
    		'show_in_nav_menus'   => true,
    		'show_ui' 			  => true,
    		'show_in_menu' 		  => true,
    		'show_in_rest'        => true,
    		'show_in_admin_bar'	  => true,
    		'menu_position' 	  => 10,
    		'menu_icon' 		  => plugin_dir_url( __FILE__ ).'reviews-icon.png',
    		'can_export' 		  => true,
    		'delete_with_user' 	  => false,
    		'hierarchical' 		  => false,
    		'has_archive' 		  => false,
    		'query_var' 		  => true,
    		'capability_type' 	  => 'post',
    		'map_meta_cap' 		  => true,
    		'rewrite' 			  => array('slug' => 'reviews', 'with_front' => false),
    		'supports' 			  => array('title', 'editor', 'excerpt', 'thumbnail')
    	));
    	$singular = 'Review category';
    	$plural = 'Review categories';
    	register_taxonomy(strtolower('cmg-reviews-tax'), 'cmg_review_pt', array(
    		'labels' => array(
    			'name' => $plural,
    			'singular_name' => $singular,
    			'menu_name' => $singular,
    			'all_items' => sprintf('All %s', $plural),
    			'edit_item' => sprintf('Edit %s', $singular),
    			'view_item' => sprintf('View %s', $singular),
    			'update_item' => sprintf('Update %s', $singular),
    			'add_new_item' => sprintf('Add New %s', $singular),
    			'new_item_name' => sprintf('New %s', $singular),
    			'parent_item' => null,
    			'parent_item_colon' => null,
    			'search_items' => sprintf('Search %s', $plural),
    			'popular_items' => sprintf('Popular %s', $plural),
    			'separate_items_with_commas' => sprintf('Separate %s with commas', $plural),
    			'add_or_remove_items' => sprintf('Add or remove %s', $plural),
    			'choose_from_most_used' => sprintf('Choose from the most used %s', $plural),
    			'not_found' => sprintf('No %s found.', $plural)
    		),
    		'public' 			  => true,
    		'publicly_queryable'  => true,
    		'exclude_from_search' => false,
    		'show_in_nav_menus'   => true,
    		'show_ui' 			  => true,
    		'show_in_menu' 		  => true,
    		'show_in_admin_bar'	  => true,
    		'show_in_rest'        => true,
    		'menu_position' 	  => 12,
    		'menu_icon' 		  => 'dashicons-admin-tools',
    		'can_export' 		  => true,
    		'delete_with_user' 	  => false,
    		'hierarchical' 		  => true,
    		'has_archive' 		  => true,
    		'query_var' 		  => true,
    		'capability_type' 	  => 'post',
    		'map_meta_cap' 		  => true,
    		'rewrite' 			  => array('slug' => strtolower('reviews'), 'with_front' => false),
    		'supports' 			  => array('title', 'editor', 'excerpt', 'thumbnail')
    	));
    }
    /**
     * Create Global Review Fields
     */
    $option_name = "cmg_review_company_name";
    if (get_option($option_name) === false){
        update_option($option_name, "");
    }
    add_action('init', __NAMESPACE__ .'\register_meta_fields');
    /**
     * Registering meta fields for block attributes that use meta storage
     */
    function register_meta_fields(){
    	register_meta(
    		'post',
    		'review_description',
    		[
    			'type'         => 'string',
    			'single'       => true,
    			'show_in_rest' => true,
    			'object_subtype' => 'cmg_review_pt'
    		] 
    	);
    	register_meta(
    		'post',
    		'review_star',
    		[
    			'type'         => 'integer',
    			'single'       => true,
    			'show_in_rest' => true,
    			'object_subtype' => 'cmg_review_pt'
    		] 
    	);
    }
    

    Here is my gutenblock I’ve made

    
    /**
    * Block dependencies
    */
    import classnames from 'classnames';
    import icon from "./icon";
    import "./style.scss";
    /**
    * Block libraries
    */
    const { __ } = wp.i18n;
    const { Fragment } = wp.element;
    const {
        registerBlockType,
    } = wp.blocks;
    const {
        RichText,
        AlignmentToolbar,
        BlockControls,
    	BlockAlignmentToolbar,
    	InspectorControls,
    	PlainText
    } = wp.editor;
    const { 
    	PanelBody,
    	PanelRow,
    } = wp.components;
    const { dispatch } = wp.data;
    /**
    * Register Block
    */
    export default registerBlockType("cmgschemareviews/description", {
    	title: __("Review Description", "cmgschemareviews"),
    	description: __("Use this field to get Schema to detect your review Description", "cmgschemareviews"),
    	category: "cmgschemareviews",
    	icon,
    	keywords:[
    		__("Review Description", "cmgschemareviews"),
    		__("Schema", "cmgschemareviews"),
    	],
    	supports:["full", "wide"],
    	attributes: {
    		message: {
    			type: 'array',
    			source: 'children',
    			selector: '.quote',
    		},
    		excerpt:{
    			type: 'string',
    			source: 'meta',
    			meta: 'review_description',
    		},
    		textAlignment: {
    			type: 'string',
    		},
    		blockAlignment: {
    			type: 'string',
    			default: 'wide',
    		},
    	},
    	getEditWrapperProps( { blockAlignment } ) {
    		if ( 'left' === blockAlignment || 'right' === blockAlignment || 'full' === blockAlignment ) {
    			return { 'data-align': blockAlignment };
    		}
    	},
    	edit: props => {
    	  const {
    		  attributes: { textAlignment, blockAlignment, message, excerpt },
    		  className, setAttributes } = props;
    
    	  return (
    		<Fragment>
    			<InspectorControls>
    				<PanelBody 
    					title={ __("Description Excerpt", "cmgschemareviews") } 
    					initialOpen={ true }
    				>
    					<PanelRow>
    						<PlainText
    							className={ className }
    							value={ excerpt }
    							onChange={( excerpt ) => {
    								//const editPost = dispatch( 'core/editor' ).editPost;
    								//console.log(excerpt);
    								//editPost({ meta: { review_description: excerpt }});
    								setAttributes( { excerpt } );
    							}}
    						/>
    					</PanelRow>
    				</PanelBody>
    			</InspectorControls>
    			<div className={ className } >
    				<BlockControls>
    					<BlockAlignmentToolbar
    						value={ blockAlignment }
    						onChange={ blockAlignment => setAttributes( { blockAlignment } ) }
    					/>
    					<AlignmentToolbar
    						value={ textAlignment }
    						onChange={ textAlignment => setAttributes( { textAlignment } ) }
    					/>
    				</BlockControls>
    				<RichText
    					tagName="div"
    					multiline="p"
    					placeholder={ __( 'Enter your message here..', 'cmgschemareviews' ) }
    					value={ message }
    					style={ { textAlign: textAlignment } }
    					onChange={ message => setAttributes( { message } ) }
    				/>
    			</div>
    		</Fragment>
    	  );
    	},
    	save: props => {
    	  const { blockAlignment, textAlignment, message } = props.attributes;
    	  return (
    		<div 
    			itemprop="description"
    			className={classnames(
    				<code>align${blockAlignment}</code>,
    				'quote',
    			)}
    		  	style={ { textAlign: textAlignment } }
    		>
    		  { message }
    		</div>
    	  );
    	}
    });
    

    The data for the PlainText component is not being saved to the Database and is not returning after a refresh.

    Any insight would be very much appreciated.

    Additionally, I have already tried the fix from this issue from the Gutenberg github
    https://github.com/WordPress/gutenberg/issues/5622

    #107454
    Zac Gordon
    Keymaster

    Two quick things:

    1. Does the example from the course repo work for you?
    2. What happens when you remove the object_subtype?

    #107481
    Barry Ross Jr
    Participant

    Your course example only works for Post and Pages, it will not save the data in the Custom post type.

    I have removed object_subtype and am having the same issue.

    From that article, I listed it appears this might be an issue with Custom Post Types, but wanted to get some additional information from you!

    #107484
    Zac Gordon
    Keymaster

    Okay, so what happens when you just hardcode a value into the meta field using PHP does that work?

    #107518
    Barry Ross Jr
    Participant

    I used a custom rest route and the wp.apiFetch to send the data and saved it using the wordpress method ‘update_post_meta’ and that stores it in the database and does show the data in the sidebar after reload.

    However, setAttributes still doesn’t work even if I turn off the above and just use what should be the normal way ( using setAttributes).

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.