Create a 404 page with editable content in WordPress

In most cases default content on a 404 page is all that would ever be needed, in a default 404.php it would look some thing like this:

<div class="page-content">
    <p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'your-textdomain' ); ?></p>

    <?php get_search_form(); ?>
</div>

However, occasionally there is a need to give the site owner the flexibility to change the content displayed on the 404 page.

This can be done in a number of different ways, but the simplest implementation I found is to search to see if a 404 page has been created in the admin and if so display the content of that page.

First of all, in functions.php add the following function:

function get_404_page() {
    $page_exists = get_page_by_title( '404' );

    if ( ! empty( $page_exists->ID ) ) {
        return $page_exists;
    } else {
        return;
    }
}

Let’s look at this a bit more closely.

$page_exists = get_page_by_title( '404' );

First, we are searching through the pages created in the admin to see if a page with the title of 404 has been created. Next we do a check to see if this page has been found or not.

if ( ! empty( $page_exists->ID ) ) {

If the page ID is not empty then we need to return the page as WP_Post object so that we can later use the post_content in our 404.php.

return $page_exists;

By using the function get_page_by_title() to find the page, we get the WP_Post object directly, which we then return for later use.

} else {
    return;
}

Otherwise we return nothing to prevent any php errors occurring when we use the function within our page.

Finally, to implement the function, open your 404.php in your theme and add the following line to the top of the file.

$error_content = get_404_page();

If the 404 page exists in the admin the value of $error_content will be the WP_Post object, otherwise it will be empty.

All that is left now is to display the editable content on the page if it exists.

<div class="page-content">
    <?php
    if ( $error_content ) {
        echo apply_filters( 'the_content', $error_content->post_content );
    }
    ?>
    <p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'your-textdomain' ); ?></p>

    <?php get_search_form(); ?>
</div>

And that’s it! You can of course, remove the default text and search form. You could also add in other content from the post object and customise it as much or as little as you want.

HeroThemes Tutorial: Creating a search widget using the WordPress REST API

Following on from Heroic Tutorial: What you need to know about the new WordPress REST API, this article covers how to use the WordPress REST API to create a widget that can search an external WordPress website and display the results in the widget.

Once you are through the tutorial, I’m sure you will agree that the WordPress REST API is pretty cool! Enjoy 🙂

You can read the full article here

The code for the tutorials can be found on Github here.

HeroThemes Tutorial: What you need to know about the new WordPress REST API

The launch of the REST API for WordPress was big news this year, as it gets rolled into core from version 4.4. and onwards it is important to understand what this means for plugin and theme developers. Exciting stuff! 🙂

In this article I explain why this development is huge for WordPress, provide a primer on the REST API and show you how to use it to simplify the process of communication between WordPress and other apps.

The end result is a small widget to display the latest posts from an external WordPress website.

I hope you enjoy it 🙂

The full article can be found here: What you need to know about the new WordPress REST API

The code for the tutorials can be found on Github here.

HeroThemes Tutorial: Restricting Access to WordPress Pages & Creating a Members Area

This article for HeroThemes is again a continuation of the previous two “HeroThemes Tutorial: Using the KnowledgeBase templating system & theme integration” and “HeroThemes Tutorial: Integrating Heroic Knowledge Base with WooCommerce“. This time adding more value by creating private content as well. Something that is especially useful if you want to provide premium content only available to those people that have purchased certain products.

This article covers:

  • The difference between password protected and private pages (and where the two solutions fall short)
  • A 2-min hack to create a simple Members Area on your WordPress site (without using a plugin)
  • Getting your knowledge base articles to show up in your Members Area (this section is for you if you use the KnowAll theme or Heroic Knowledge Base solution)
  • Some free plugins that will help you control access to your site’s pages, posts, and other content

The end result of the article is creating a members area that can include content from the Knowledge Base plugin.

I hope you find it useful! 🙂

The full post can be read here: HeroThemes Tutorial: Restricting Access to WordPress Pages & Creating a Members Area

The code for the tutorials can be found on Github here.