WordPress

Building a “Scotch Tape” Image Block Style – WP Tavern


In immediately’s submit, Tavern readers are in for one thing slightly totally different than our common stream of reports and opinion. Welcome to the Building with Blocks collection. It’s a new kind of submit we are attempting out to indicate individuals a number of the enjoyable, distinctive, or inventive issues they will do with WordPress blocks.

Our group determined to separate the collection into each developer and user-centric tracks. Posts could cowl creating an “about me” web page, coding customized block kinds, piecing collectively a sample, or one thing else completely.

Since I’m kicking off this collection, I needed to reply a query we already obtained from one in all our readers. I had beforehand shared a customized block type with a “Scotch tape” look in my protection of the FSE Outreach Program’s call for media testing:

Photo of a giraffe in the WordPress editor with a Polaroid-style frame and piece of tape holding it.
Authentic block type.

Devendra Meena asked in the comments:

Hey man, easy methods to get the “tape” customized block picture?

This felt like a pure start line with my first submit within the collection.

The design exhibits a chunk of tape holding a Polaroid-style, framed picture. Technically, I additionally had an alternate type with two items of tape on the corners. Nonetheless, this tutorial will give attention to the previous. The 2-corner design requires overwriting some editor-specific CSS, technically breaking performance, and isn’t one thing I ought to encourage.

I additionally needed to start with block kinds as a result of they’re underutilized. Most variations I’ve seen have been easy adjustments like including borders and eradicating margins. These are sometimes greatest left to dam design instruments. In fact, themers are including these kinds as a result of WordPress at the moment lacks or has beforehand lacked the UI controls for dealing with them. It is usually one of many causes so many have requested to mix and match multiple block styles — themes are doing the work that core must be doing. Extra instruments are regularly being added, however we nonetheless have an extended method to go.

After I consider block kinds, I wish to create designs which are unlikely to be out there by way of the usual design instruments. I wish to serve one thing distinctive to theme customers. That’s the place we’re beginning immediately.

As a bonus, customized block kinds work in basic and block themes.

Regardless of having written a whole lot of tutorials in my life, this train turned out to be slightly harder than I had anticipated. It’s simple to neglect that every little thing I code begins with a base of “fixing” the issues that I discover odd concerning the default block library kinds. This makes my life simpler. Nonetheless, many theme authors will lean on core’s defaults, so I wanted to make this common sufficient to work for them.

Subsequently, I opted to start out from the default Twenty Twenty-Two theme. In testing, I recommend working with it. The CSS code within the following part might have slight alterations for others.

Creating the “Scotch Tape” Block Model

Step one is to register a customized block type by way of the theme. WordPress has each server-side and JavaScript APIs for this characteristic. Utilizing PHP is simpler to arrange if you don’t have already got an editor script file to drop the code into.

To register the customized block type, add the next code to your theme’s features.php file:

// Register block kinds on the init hook.
add_action( 'init', 'tavern_register_block_styles' );

// Wrapper operate for registering all block kinds.
operate tavern_register_block_styles() {

        register_block_style( 'core/picture', [
        	'name' => 'scotch-tape',
        	'label' => __( 'Scotch Tape', 'tavern' )
        ] );
}

Doing so will register the type within the editor. You’ll be able to check this by including an Picture block on the post-editing display. “Scotch Tape” must be selectable underneath the kinds tab.

WordPress editor with an Image block inserted into the content canvas.  On the right, there are three block styles, including a "Scotch Tape" variation.
Registered type accurately seems for the Picture block.

Registering a mode is the simple half. Writing the code is the place issues can get dicey. WordPress has so many strategies of loading CSS kinds that you just won’t know the place to start out.

The register_block_style() operate used earlier permits builders so as to add a style_handle, a reference to a registered stylesheet. Themers may also add an inline type instantly by way of the inline_style argument. For just some strains of CSS, this works effectively.

In my very own themes, I register block-specific stylesheets by way of the wp_enqueue_block_style() operate — sure, the operate names are terribly complicated. This was formally added in WordPress 5.9. It can solely output the CSS when a block is used on the web page. For instance, I add a core-image.css file to accommodate all Picture block CSS. That is the tactic I like to recommend.

Nonetheless, for the sake of simplicity, I added the next code to the top of Twenty Twenty-Two’s type.css file:

/* Design for the <determine> wrapper. */
.wp-block-image.is-style-scotch-tape {
	place:         relative;
	overflow:         seen;
	padding:          1rem;
	background-color: #fff;
	box-shadow:       0 4px 10px 0 rgba( 0, 0, 0, 0.3 ),
	                  0 0 4rem rgba( 255, 255, 235, 0.5 ) inset;
}

/* Design for the "tape". */
.wp-block-image.is-style-scotch-tape::earlier than {
	content material:    "";
	show:    block;
	place:   absolute;
	width:      7rem;
	top:     2rem;
	margin:     auto auto auto -3.5rem;
	z-index:    1;
	left:       50%;
	prime:        -0.5rem;
	background: rgba( 255, 255, 235, 0.5 );
	box-shadow: 0 4px 10px 0 rgba( 0, 0, 0, 0.3 );
}

/* Take away TT2's picture shadow. */
.wp-block-image.is-style-scotch-tape img {
	box-shadow: none;
}

/* Changes for the caption. */
.wp-block-image.is-style-scotch-tape figcaption {
	show:     block;
	margin:      1rem 0 0;
	line-height: 1;
	font-size:   1rem;
	font-family: 'Fuzzy Bubbles', sans-serif;
}

With the CSS in place, it’s merely a matter of discovering a picture to check with. I selected Marcus Burnette’s whale shark from WordPress Photographs.

WordPress post editor with an photo of a whale shark in the content canvas.  It has a Polaroid-style border with a piece of tape that appears to be holding it up.
Including photographs in type!

If you wish to add a little bit of aptitude to your captions, load up Fuzzy Bubbles or one other handwriting font from Google Fonts.

This type doesn’t essentially should be tied to the Picture block. There isn’t a motive it couldn’t apply to Paragraphs, Teams, and extra with some changes.

For now, I hope you all have enjoyable with it. Additionally, be at liberty to share within the feedback the place you wish to see the brand new “Constructing with Blocks” collection head sooner or later.


Leave a Reply

Your email address will not be published. Required fields are marked *