Tutorial WordPress

Restrict post to WordPress user

Over the that weekend I had been using WordPress to take notes at WordCamp Orange County and realized that WordPress’ block editor is useful as a note taking environment. But what If I don’t want these notes to be public?

Using a template to restrict access

I duplicated TwentyNineteen’s single.php template and wrapped the loop in a conditional tag is_user_logged_in().

The code

 * Single post and page template for restricting the title and content unless the user is logged in
 * Sources: 
 * @link
 * @package WordPress
 * @subpackage Twenty_Nineteen
 * @since 1.0.0
 * License: GPL 2 or later
 * Template Name: Log in Required
 * Template Post Type: post, page


	<div id="primary" class="content-area">
		<main id="main" class="site-main">

	if ( is_user_logged_in() ) {  // check if user is logged in

/* * 
 * Start the Loop 
 * This example  uses TwentyNineteen for the loop, if using a different theme subsitute that loop here.
 * */
		while ( have_posts() ) :

			get_template_part( 'template-parts/content/content', 'single' );

			if ( is_singular( 'attachment' ) ) {
				// Parent post navigation.
						/* translators: %s: parent post link */
						'prev_text' => sprintf( __( '<span class="meta-nav">Published in</span><span class="post-title">%s</span>', 'twentynineteen' ), '%title' ),
			} elseif ( is_singular( 'post' ) ) {
				// Previous/next post navigation.
						'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next Post', 'twentynineteen' ) . '</span> ' .
							'<span class="screen-reader-text">' . __( 'Next post:', 'twentynineteen' ) . '</span> <br/>' .
							'<span class="post-title">%title</span>',
						'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous Post', 'twentynineteen' ) . '</span> ' .
							'<span class="screen-reader-text">' . __( 'Previous post:', 'twentynineteen' ) . '</span> <br/>' .
							'<span class="post-title">%title</span>',

			// If comments are open or we have at least one comment, load up the comment template.
			if ( comments_open() || get_comments_number() ) {

		endwhile; // End of the TwentyNineteen  loop.
	} else { // If not logged in present a link to log into the site
		echo '<p style="text-align: center;"><a href="' .  wp_login_url( get_permalink() ) . '" title="Login">Log in to view this post</a></p>';

		</main><!-- #main -->
	</div><!-- #primary -->


View this code in my GitHub gist.

How does this code work?

Simply put, is_user_logged in() will check if the user is logged before it displays the post. If not they’re presented with a link to log in.

Left, the user is logged in. Right, post content and title will not display.

What it doesn’t do?

Using this post and page template will only restrict the post page. The excerpt or content will still display on archive pages unless those loops run a similar conditional check.

Tutorial WordPress

Creating a custom attachment loop for images and other files in WordPress

While building an attachment.php template for a project I opted to customize the loop to properly display an image within a figure tag and the caption within figcaption to aid accessibility.

Additionally if the file is not an image it will provide a button that can be styled along with a mime type.

The following code would have to be placed within the loop.

if ( wp_attachment_is_image() ) {

	// Set the image's ID to an array so it can be reused later
	$image_id = get_the_ID();
	$caption = wp_get_attachment_caption( $image_id );

	echo '<figure>';

		// Displays the large version of the image if available
		echo wp_get_attachment_image( $image_id, 'large' );

		// if no caption is set or the value is null do nothing and don't display the empty tags
		if (!empty($caption) ) {
			echo '<figcaption>' . $caption . '</figcaption>';

	echo '</figure>';

} else {

	// If the attachment is not an image display a button and mime type.
	$attachment_id = get_the_ID();

	$attachment_url = wp_get_attachment_url( $attachment_id ); 

	echo '<button type="button"><a href="' . $attachment_url . '" target="_blank">' . get_the_title( $attachment_id ) . '</a></button> ';

	echo '<span class="mime-type">' . get_post_mime_type( $attachment_id ) . '</span>';



Twenty Nineteen Child Theme

This theme was created by Joseph Dickson to add additional features for use on my personal websites. Feel free to learn from this code, reuse it. Even make fun of how poorly written it may be.


Version 1.0.1

  • Added Open Graph support.
    • Theme will use the custom logo set in the customizer.
    • Theme will use the featured image on posts and pages if available.
  • Updated functions.php so block.css can be cached by last modified time.

Version 1.0.0

  • Pins a post slugged ‘who-am-i’ to the homepage if it exists and doesn’t display a featured even if one is assigned.
  • Increases the Code block width to 100% of it’s container and bumps up the font size.



Excluding a software as a service script from Events Calendar pages and others using Conditional Tags

Recently, I hit a hard page limit with our web governance subscription and decided to exclude lower priority pages such as calendar entries, media attachment pages and blog archive indexes to removing them from the automated check I can avoid upgrading our plan and focus on pages and posts that actually matter.

Last year I created a simple plugin that loads this software as a service into our footer. Here, it’s been extended to omit certain templates using WordPress’ conditional tags method to load it only where it will be useful.

Tutorial WordCamp WordPress

Overriding Gutenberg’s default editor width and font

By default Gutenberg sets the editor width to 580px on the desktop unless the browser window is narrower. Since Kubrick’s post content area is 480px we’ll need to override the width so text wrapping occurs at the same width as the final post. for that we simply set new declarations for the wp-block class tag.