Auto Create And Assign Static Homepage In WordPress

For some themes, when the homepage or more exactly – the front page, is well designed, and users have to follow the theme documentation to create such that page to make it look the same as in the demo. In this article, I’ll show you a simple way to check if a static homepage has been assigned and automatically create it if it’s missed.

static homepage Auto Create And Assign Static Homepage In WordPress

There are 2 options in WordPress that control static homepage: show_on_front – type of homepage (latest posts or static page) and page_on_front – which page is shown on homepage. Here’s the snippet that checks these options and automatically creates a Homepage page:

function mytheme_create_homepage()
{
    if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) )
        return;

    $homepage = wp_insert_post( array(
        'post_title'   => 'Homepage',
        'post_status'  => 'publish',
        'post_type'    => 'page',
        'post_content' => 'Any content here',
    ) );
    if ( $homepage )
    {
        update_option( 'show_on_front', 'page' );
        update_option( 'page_on_front', $homepage );
    }
}

8 Comments

  1. Hi,

    I really like this snipped and I would like to use it on my WordPress Network to automaticall create and assign a static homepage when a new subsite is created.

    I’m sorry, I’m quite new to WordPress coding, how do I implement this snipped?

    Kind regards

    Reply
    • For multisite network, you can put it in a plugin that located in “mu-plugins” folder. This folder is “must use plugins” and auto activated for all sites. If you not sure how to create a plugin or how “mu-plugins” works, please google for that.

      Reply
    • Ah, didn’t realize that would happen.

      add_action('admin_notices', 'notify_if_front_or_posts_page');
      function notify_if_front_or_posts_page() {
      global $post;
      if (!isset($post->post_status))
      return;
      if (get_the_ID() == get_option('page_on_front')) {
      $message = 'This is the front page';
      }
      if (get_the_ID() == get_option('page_for_posts')) {
      $message = 'This is the posts page';
      }
      if (isset($message))
      echo '' . $message . '';
      }

      The difference between hers and mine is she used media_buttons instead.

      Reply
  2. Nice!

    If I may add, Kailey Lampert (http://kaileylampert.com/) recently did a pretty nice article on how to notify a user when they’re editing the static front page: https://forrst.com/login.

    Also, see the latest comment if you’d like to have the notification bar just below the h2 title.

    Btw. nice to see you’re using StudioPress’ Genesis!

    Reply
    • The link (updated one below) seems not to work :(.

      I searched an alternative theme for my previous theme, and actually I designed a theme for myself. But then I found Studio’s eleven40, which has the same design idea as I had, so I just choose it instead of reinventing the wheel.

      Reply
  3. You’re the man. I was searching all over the codex for how to check if the homepage was a static page and not only did you answer that, you took it one step further. Big fan of your blog!

    Reply

Leave a Reply