Creating Custom Block Patterns For WordPress

Block Patterns

The WordPress block editor includes Block Patterns which allow designers and developers to copy a block group and build it into a Plugin, Theme or Child Theme then quickly add and re-configure it in a post.

Effectively copy and paste the entire group into an editor

Joseph Dickson

Call to Action Block

		register_block_pattern(
			'josephd-patterns/call-to-action-tiles',
			array(
				'title'       => __( 'Call to Action Tiles', 'textdomain' ),
				'categories'  => array( 'josephd-patterns' ), // use the category slug
				'description' => _x( 'A column row of images and buttons.', 'A column row using images and buttons', 'textdomain' ),
				'content'     => implode(
					'', array(
						'<!-- wp:columns -->',
						'<div class="wp-block-columns"><!-- wp:column -->',
						'<div class="wp-block-column"><!-- wp:group -->',
						'<div class="wp-block-group"><div class="wp-block-group__inner-container"><!-- wp:image  -->',
						'<figure class="wp-block-image"><img src="' . $dir  . '" alt="camera icon" /></figure>',
						'<!-- /wp:image -->',

						'<!-- wp:buttons -->',
						'<div class="wp-block-buttons"><!-- wp:button {"borderRadius":0,"width":100} -->',
						'<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link no-border-radius">Button Text</a></div>',
						'<!-- /wp:button --></div>',
						'<!-- /wp:buttons --></div></div>',
						'<!-- /wp:group --></div>',
						'<!-- /wp:column -->',

						'<!-- wp:column -->',
						'<div class="wp-block-column"><!-- wp:image -->',
						'<figure class="wp-block-image"><img src="' . $dir  . '" alt="camera icon" /></figure>',
						'<!-- /wp:image -->',

						'<!-- wp:buttons -->',
						'<div class="wp-block-buttons"><!-- wp:button {"borderRadius":0,"width":100} -->',
						'<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link no-border-radius">Button Text</a></div>',
						'<!-- /wp:button --></div>',
						'<!-- /wp:buttons --></div>',
						'<!-- /wp:column -->',

						'<!-- wp:column -->',
						'<div class="wp-block-column"><!-- wp:image -->',
						'<figure class="wp-block-image"><img src="' . $dir  . '" alt="camera icon" /></figure>',
						'<!-- /wp:image -->',

						'<!-- wp:buttons -->',
						'<div class="wp-block-buttons"><!-- wp:button {"borderRadius":0,"width":100} -->',
						'<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link no-border-radius">Button Text</a></div>',
						'<!-- /wp:button --></div>',
						'<!-- /wp:buttons --></div>',
						'<!-- /wp:column --></div>',
						'<!-- /wp:columns -->',
					)
				),

			)
		);

YouTube Embed Block

register_block_pattern(
			'josephd-patterns/youtube-video-embed',
			array(
				'title'       => __( 'YouTube Video', 'textdomain' ),
				'categories'  => array( 'josephd-patterns' ), // use the category slug
				'description' => _x( 'A video and supporting text', 'A YouTube embed, text and, buttons', 'textdomain' ),
				'content'     => implode(
					'', array(					
						'<!-- wp:cover {"url":"' . $dir . '","dimRatio":50,"focalPoint":{"x":"0.50","y":"1.00"},"contentPosition":"center center","align":"full","className":"is-style-default"} -->',
						'<div class="wp-block-cover alignfull has-background-dim-50 has-background-dim is-style-default"><img class="wp-block-cover__image-background" alt="" src="' . $dir . '" style="object-position:50% 100%" data-object-fit="cover" data-object-position="50% 100%"/><div class="wp-block-cover__inner-container"><!-- wp:columns {"align":"full"} -->',
						'<div class="wp-block-columns alignfull"><!-- wp:column -->',
						'<div class="wp-block-column"><!-- wp:embed {"url":"https://www.youtube.com/watch?v=JL7DJay1xtA","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->',
						'<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">',
						'https://www.youtube.com/watch?v=JL7DJay1xtA',
						'</div></figure>',
						'<!-- /wp:embed --></div>',
						'<!-- /wp:column -->',

						'<!-- wp:column -->',
						'<div class="wp-block-column"><!-- wp:heading -->',
						'<h2>Lorem Ipsum</h2>',
						'<!-- /wp:heading -->',

						'<!-- wp:paragraph -->',
						'<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras auctor id odio ut feugiat. In vitae metus libero. Curabitur nulla mi, ultrices ut maximus non, pellentesque non erat. Nam accumsan sapien sodales augue tempus iaculis. Etiam interdum vestibulum eleifend. Ut ornare dignissim lacus sit amet placerat. Mauris et diam pulvinar orci consectetur malesuada non nec libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>',
						'<!-- /wp:paragraph -->',

						'<!-- wp:buttons {"contentJustification":"center"} -->',
						'<div class="wp-block-buttons is-content-justification-center"><!-- wp:button {"backgroundColor":"black","textColor":"white"} -->',
						'<div class="wp-block-button"><a class="wp-block-button__link has-white-color has-black-background-color has-text-color has-background">Call to Action</a></div>',
						'<!-- /wp:button -->',

						'<!-- wp:button {"textColor":"white","className":"is-style-outline"} -->',
						'<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-white-color has-text-color">Supporting Action</a></div>',
						'<!-- /wp:button --></div>',
						'<!-- /wp:buttons --></div>',
						'<!-- /wp:column --></div>',
						'<!-- /wp:columns --></div></div>',
						'<!-- /wp:cover -->',
						
					)
				),

			)
		);

Image & Quote

register_block_pattern(
			'josephd-patterns/image-quote',
			array(
				'title'       => __( 'Image & Quote', 'textdomain' ),
				'categories'  => array( 'josephd-patterns' ), // use the category slug
				'description' => _x( 'An image and quote', 'A portrait alongside the quote', 'textdomain' ),
				'content'     => implode(
						'', array(					
						'<!-- wp:columns -->',
						'<div class="wp-block-columns"><!-- wp:column {"width":"33.33%"} -->',
						'<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:image {"large","linkDestination":"none"} -->',
						'<figure class="wp-block-image"><img src="' . $dir  . '" alt="camera icon" /></figure>',
						'<!-- /wp:image --></div>',
						'<!-- /wp:column -->',

						'<!-- wp:column {"width":"66.66%"} -->',
						'<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:quote {"className":"is-style-large"} -->',
						'<blockquote class="wp-block-quote is-style-large"><p>Inspiring Quote"</p><cite>Citation </cite></blockquote>',
						'<!-- /wp:quote --></div>',
						'<!-- /wp:column --></div>',
						'<!-- /wp:columns -->',
					)
				),

			)
		);

Articles Referenced In The Video

Sort Archive Posts Alphabetically

I recently worked on a documentation project that required posts to be sorted alphabetically by title. The process can be done by using pre_get_posts() and the is_archive() conditional tag.

/*
 * Order post archives by title in descending order
 */
add_action( 'pre_get_posts', 'jd_archive_sort_alpha_title'); 

function jd_archive_sort_alpha_title($query){

	if(is_archive()) {

		// Set the order to ascending order
		$query->set( 'order', 'ASC' );

		// set the orderby to title
		$query->set( 'orderby', 'title' );

	}

};

As always if you want to use this plugin on your website test it first in a safe environment to verify that it doesn’t create a conflict. Happy hacking. 🙂

Private Notes

My Private Notes plugin allows me self host my notes while hiding it from public view.

The Private Notes WordPress plugin is in active development, I’m providing it here for the public to hack, tweak and learn from… Use at your own risk.

Private Notes allows users create and store notes on their personal website.

How Private Notes works

The Plugin creates a Notes section within the dashboard where you can create and maintain your notes.

A Custom Post Type is created to hold your content.

These notes are visible when logged into your website while the content remains hidden from a public guest who could be viewing the page.

What users can see when they’re logged in.

An example of what the public would see if they visit the post

Important

The post’s title, author, date and other data will remain visible to the public. Additionally any media files such as images, videos, audio, and documents will remain visible from their attachment pages. Private Notes does not secure any files attached to the post! It only hides the post’s content area.

Creating a Private Note

After activating the private Private Notes plugin a notebook icon will appear in your admin menu under Posts. Simply Add New, type out some notes, add media, links, embeds or whatever you like. Then hit publish.

The Post Editor of a Note
The Post Editor of a Note


Although self hosting notes in WordPress isn’t as convenient as a dedicated note taking app it does allows personal control and ownership of my thoughts.