Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gutenberg Block Template #5383

Closed
permrosen opened this issue Jun 4, 2019 · 5 comments

Comments

Projects
None yet
4 participants
@permrosen
Copy link

commented Jun 4, 2019

I’m trying to make a gutenberg block template (the same predefined blocks on every new post I create) for a CPT called Galleri made in Pods. Described here: https://developer.wordpress.org/block-editor/developers/block-api/block-templates/#api

function myplugin_register_template() {
$post_type_object = get_post_type_object( ‘galleri’ );
$post_type_object->template = array(
array( ‘core/paragraph’ ),
array( ‘core/gallery’ ),
);
}
add_action( ‘init’, ‘myplugin_register_template’ );

It works fine on regular posts if I replace “galleri” in the code with “post”, but not on the Pod created post type. REST API is activated in Pods so Gutenberg is there as it should but not my block template.

@issue-label-bot

This comment has been minimized.

Copy link

commented Jun 4, 2019

Issue-Label Bot is automatically applying the label Type: Support to this issue, with a confidence of 0.63. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@leepeterson

This comment has been minimized.

Copy link

commented Jun 11, 2019

@permrosen I thought the same as you (since WP docs provide that guidance). However, you can hook into pods_register_post_type or pods_register_post_type_{$post_type} for this purpose.

Example:

add_action( 'init', function() {
	add_filter( 'pods_register_post_type_{$post_type}', function( $options ) {
		$template = [
			['core/heading',
				['level' => '2','content' => 'Predefined H2']
			],
			['core/paragraph',
				['content' => 'Predefined paragraph copy']
			],
			['core/image',
				['align' => 'full']
			],
		];
		$options['template'] = $template;
		$options['template_lock'] = 'all';
		return $options;
	});
});
@JoryHogeveen

This comment has been minimized.

Copy link
Member

commented Jun 12, 2019

@leepeterson
Thank you! One edit: you don't need to wrap it within the init hook!

@permrosen the code below should work fine.

$post_type = 'galleri';
add_filter( "pods_register_post_type_{$post_type}", function( $options ) {
	$template = array(); // Configure your template array here.
	$options['template'] = $template;
	$options['template_lock'] = 'all';
	return $options;
} );
@leepeterson

This comment has been minimized.

Copy link

commented Jun 12, 2019

@JoryHogeveen Thanks for that catch! I'm a creature of habit... what can I say :)

@permrosen

This comment has been minimized.

Copy link
Author

commented Jun 14, 2019

@sc0ttkclark sc0ttkclark added this to the Pods 2.7.13 milestone Jun 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.