Upgrading Kubrick for Gutenberg

This post contains files and tutorial information related to my talk at WordCamp Santa Clarita on April 6, 2019.

Presentation Files

Upgrading Kubrick for Gutenberg

Sources and Links

Upgrading a classic theme for Gutenberg

With the release of WordPress 5.0 in early December 2018 web developers were forced to make a decision, install the Classic Editor plugin and continue to enjoy the post editor we’ve used for years or jump the new block editor experience. However, I quickly found a few of my themes were not ready.

Continue reading “Upgrading a classic theme for Gutenberg”

Disable URL Guessing in WordPress

Placing the following PHP code snippet below in your theme’s funtions.php file will disable a WordPress feature that attempts to automatically redirect a bad url to an actual post or page with a similar url.

Please note, if there are any broken links to your website or it’s pages that benefit from this feature it will now lead to a standard 404 error page.

/* Disable URL guessing - https://www.bloggersignal.com/stop-wordpress-from-guessing-urls */
add_filter('redirect_canonical', 'pz_no_redirect_404');

function pz_no_redirect_404($redirect_url)
{
    if (is_404()) {
        return false;
    }
    return $redirect_url;
}

The above was found in a post at https://www.bloggersignal.com/stop-wordpress-from-guessing-urls

Adding alt text to a post thumbnail

While working on a custom navigation using WP_Query and has_post_thumbnail for an icon I ran into an accessibility concern. By default, the_post_thumbnail will not display an alt tag when rendered in the browser. In the following code snippet I add an alt tag using the image file’s title.

<?php

// The Post Array
$args = array(
	'category_name'	=> 'disasters, emergency-procedures',
	'orderby'	=> 'title',
	'order'		=> 'ASC',
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<a href="' . get_permalink() . '">'; 
		echo '<article class="emergency-icon">';
			// check if the post has a Post Thumbnail assigned to it.
			if ( has_post_thumbnail() ) {

				the_post_thumbnail(
					array(40,40),
					array('alt' => get_the_title() )
				);
			}

			echo '<span class="entry-title">' . get_the_title() . '</span>';
		echo '</article>';
		echo '</a>';
	}
	/* Restore original Post Data */
	wp_reset_postdata();
} else {
	// no posts found
}

In short the code above starts with a query that looks for categories slugged”disasters” and emergency-procedures then orders the results by title in ascending order.

Customized html is then wrapped around the results along with an alt attribute taken from the image’s title then included within the img tag.

Technically speaking, it would be more appropriate to use the alt field set for the image. However, alt text has to be generated manually where as the title is automatically created when media is uploaded to the library.