Block Pattern: Helpful 404 Page Content
Adds a friendly 404 pattern with a message, a search box and a button home, offered when you edit the 404 template in the Site Editor.
Opens WPIDE on
Code
add_action( 'init', function () {
if ( ! function_exists( 'register_block_pattern' ) ) {
return;
}
$content = '<!-- wp:group {"layout":{"type":"constrained"}} --><div class="wp-block-group">'
. '<!-- wp:heading {"textAlign":"center","level":1} --><h1 class="wp-block-heading has-text-align-center">We could not find that page</h1><!-- /wp:heading -->'
. '<!-- wp:paragraph {"align":"center"} --><p class="has-text-align-center">The link may be old or mistyped. Try a search, or head back to the homepage.</p><!-- /wp:paragraph -->'
. '<!-- wp:search {"label":"Search","showLabel":false,"buttonText":"Search"} /-->'
. '<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} --><div class="wp-block-buttons"><!-- wp:button --><div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="/">Back to the homepage</a></div><!-- /wp:button --></div><!-- /wp:buttons -->'
. '</div><!-- /wp:group -->';
register_block_pattern( 'wpide/404-helpful', array(
'title' => '404: helpful message with search',
'description' => 'Friendly not-found message, search box and home button.',
'categories' => array( 'text' ),
'keywords' => array( '404', 'not found', 'error' ),
'templateTypes' => array( '404' ),
'content' => $content,
) );
} );
Notes
Open Appearance > Editor > Templates > Page: 404 and pick it from the patterns offered for that template.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.