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' );
?>

32 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

    1. avatar
      wrote this comment on

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

  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?

    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 :-)

  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?

    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.
      1. avatar
        wrote this comment on
        Just for reference there is now a hook for theme activation: <a href="http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme" target="blank">after_switch_theme</a>

        I know this is about 4 years too late for Nick but will prove useful for anyone else who happens across this in future :-)
  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?
    1. avatar
      wrote this comment on
      Hello Errol, I can certainly help. Please use my contact form if you're interested in hiring me.
      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??
        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.
  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.
    1. avatar
      wrote this comment on
      Either the css or the right template, see http://codex.wordpress.org/Template_Hierarchy
  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
    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.
  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.
    1. avatar
      wrote this comment on
      Hi, have a look at http://codex.wordpress.org/Theme_Development
  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.
  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
  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.
  11. avatar
    wrote this comment on
    It sounds like you may want http://codex.wordpress.org/Theme_Development
  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!
    1. avatar
      wrote this comment on
      Not sure what you mean?
    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...
  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
  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 :)
    1. avatar
      wrote this comment on
      Sorry, I don't understand your question.
  15. avatar
    wrote this comment on
    Hi,
    any ideas why it is not working:

    [et_widget type="ET_Widget_Recent_Posts" title="News"]
    1. avatar
      wrote this comment on
      No idea what et_widget type is. Certainly not one of my plugins.
  16. avatar
    wrote this comment on
    perfect - just the codeI was looking for thank you...
  17. avatar
    wrote this comment on
    Thanks mate!
  18. avatar
    wrote this comment on
    I am trying to display a different front page, depending on the value of a counter I have tried to implement. But it is not working - can somebody tell me what I am doing wrong? Any help is greatly appreciated.

    <?php
    if (is_new_day() ):
    $SignupCounter = 0;
    ?>

    <?php
    if (is_page(confirmed-signup) ):
    $SignupCounter++;
    ?>

    <?php
    if ($SignupCounter == 0) : {
    $LP = get_page_by_path( '/home-0' );
    update_option( 'page_on_front', $LP->ID );
    update_option( 'show_on_front', 'page' );
    }

    elseif ($SignupCounter == 1) : {
    $LP = get_page_by_path( '/home-1' );
    update_option( 'page_on_front', $LP->ID );
    update_option( 'show_on_front', 'page' );
    }
    ?>

Start a new thread

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