Set static front page and blog page programmatically in WordPress

You want to make sure the About and Blog pages exist, so create them before if necessary, see wp_insert_post()

<?php

// Use a static front page
$about = get_page_by_title( 'About' );
update_option( 'page_on_front', $about->ID );
update_option( 'show_on_front', 'page' );

// Set the blog page
$blog   = get_page_by_title( 'Blog' );
update_option( 'page_for_posts', $blog->ID );

// Switch to our theme
//switch_theme( 'Template', 'stylesheet' );
?>

Published on Dec. 12, 2010 at 10:30 a.m. by Nicolas and tagged plugin, theme, WordPress, WordPress plugin, WordPress theme. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

30 comments

  1. avatar
    wrote this comment on

    Dear Friend, I´m try to get my posts in my blog page and I saw your code but where can I put this?

    Index.php or and different file?

    Regards
    Frank

    Reply to this comment
    1. avatar
      wrote this comment on

      This code is intended for plugin authors. You should use the usual settings page, settings->reading.

      Reply to this comment
  2. avatar
    wrote this comment on

    do you know what hook you would need to connect with to create this static home page? Or does the update_options do this automatically?

    Reply to this comment
    1. avatar
      wrote this comment on

      Good question. IIRC plugin activations hook fire before init, so I'm not sure if that would be safe.

      For themes, no idea, afaik they don't have activation hooks.

      I use this code on wpmu_new_blog, but that's probably not what you're looking for :-)

      Reply to this comment
  3. avatar
    wrote this comment on

    I am assuming this goes into functions.php, but where could it be placed to just fire once, on first install of a theme?

    Reply to this comment
    1. avatar
      wrote this comment on
      Oops, looks like I forgot to answer your question. For a theme you'll want to implement your own activation hook, as WordPress doesn't have one. I think the easiest would be to add a 'theme_setup' option or somesuch that you check on each pageload. But doing this doesn't sound ideal.
      Reply to this comment
  4. avatar
    wrote this comment on
    I administer a community website in the UK. I am currently using Seashore as our theme which has three columns. We would like to make the site member-ship only and I think that WP-Members would be fine to get viewers and contributors to register and login. However, WPM is based upon Twenty Ten and Twenty Eleven so when I activate it the registration form bleeds into my theme columns.

    I thought that a splash page would be better and have noticed your code. However I am not a coder. Can you help?
    Reply to this comment
    1. avatar
      wrote this comment on
      Hello Errol, I can certainly help. Please use my contact form if you're interested in hiring me.
      Reply to this comment
      1. avatar
        wrote this comment on
        Is there anyway to have a navigation link in my nav-bar that will point towards the author template page??
        Reply to this comment
        1. avatar
          wrote this comment on
          The nav menu system lets you add links to anything you like. There are also plugins like page-links-to if you don't use the (relatively new) menu system.
          Reply to this comment
  5. avatar
    wrote this comment on
    Hi Nicolas. I've changed my "home" page from posts to static as described in your guide. However, with this change the static page "moved" upwards and is blocking my header pic. Where do I makes changes for the static page to fit properly? Thx.
    Reply to this comment
    1. avatar
      wrote this comment on
      Either the css or the right template, see http://codex.wordpress.org/Template_Hierarchy
      Reply to this comment
  6. avatar
    wrote this comment on
    Hi, maybe you can point me in the right direction for this. I have a WP site, and I'd like to change it to have a NON-WP static front page, that I could use to link to the WP site. Any idea what I'd need to do?

    Thanks
    Reply to this comment
    1. avatar
      wrote this comment on
      You could simply drop an index.html file into the root directory. Depending on the server configuration it should be displayed by default.
      Reply to this comment
  7. avatar
    wrote this comment on
    I am using the Twenty Eleven theme and have made my home page a static page. I want to change the title and the area where it resides on all the pages to make it smaller in height, to add a logo and slogan under the title and to change the font and its color. I am new to code and do not know where to find the code that allows me to make these changes.
    Reply to this comment
    1. avatar
      wrote this comment on
      Hi, have a look at http://codex.wordpress.org/Theme_Development
      Reply to this comment
  8. avatar
    wrote this comment on
    I set the front page as static and created another page as a blog page for posts. I'm creating a different page for each article I want to publish. I left the blog page blank except for the title as instructed. But I'm confused. I want to make small posts that will appear on the blog page, linking to my article pages. How do I make that happen?

    Thanks in advance.
    Reply to this comment
  9. avatar
    wrote this comment on
    I like to create the static page in v 3.3.1 but I can't follow the same directions. The dashboard is different.

    So Q: How can I create static page in this new version?


    TIA
    Reply to this comment
  10. avatar
    wrote this comment on
    Hi Nicolas,
    I am a beginner in word press, I want to create a Joomla Site In word press.
    Also I've not any source code or admin id/password for that site. So Guide me how to create
    theme & other functionality. thanx.
    Reply to this comment
  11. avatar
    wrote this comment on
    It sounds like you may want http://codex.wordpress.org/Theme_Development
    Reply to this comment
  12. avatar
    wrote this comment on
    It seems as though setting up a static front page has changed in this newest release (3.4.something), and I cannot set up a non post - page only site anymore... what's up with that?

    HELP!
    Reply to this comment
    1. avatar
      wrote this comment on
      Not sure what you mean?
      Reply to this comment
    2. avatar
      wrote this comment on
      Chris - I just discovered that if you delete all pages or haven't created one, then the option disappears in WPadmin...
      Reply to this comment
  13. avatar
    wrote this comment on
    Hi all - Great plugin

    How can I get event to also be displayed on this theme's "Avenue Theme" carousel on th homepage. For all other posts I tag the post to be "featured" and upload a larger image so it adds it to the homepage carousel. Is this possible with events manager? Any help would be appreciated

    Also will this work with multsites?

    www.grektown.com.au

    Thanking you in advance
    Reply to this comment
  14. avatar
    wrote this comment on
    Hi Nicolas,

    tried your code,
    i can see the pages in dashboard settings but i cannot see the page on my site,
    and is there any way to specify that the page is a parent page,
    could you help me with the issue,
    I am using angular theme,

    thanks :)
    Reply to this comment
    1. avatar
      wrote this comment on
      Sorry, I don't understand your question.
      Reply to this comment
  15. avatar
    wrote this comment on
    Hi,
    any ideas why it is not working:

    [et_widget type="ET_Widget_Recent_Posts" title="News"]
    Reply to this comment
    1. avatar
      wrote this comment on
      No idea what et_widget type is. Certainly not one of my plugins.
      Reply to this comment
  16. avatar
    wrote this comment on
    perfect - just the codeI was looking for thank you...
    Reply to this comment
  17. avatar
    wrote this comment on
    Thanks mate!
    Reply to this comment

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.