Create Set Static Front Page and Blog with Content on Theme Activate

Categories:

Recently Adding a function to my theme that user don’t have any problem while setting front page on their theme, So here is solution that create front page automatically and set it to static front page also you can put some content on it as you desire.

If users desire they can change page from Dashboard > Settings > Reading > here front and blog page can be changed

static-front-page

Set Static Front Page and Blog with Content

if (isset($_GET['activated']) && is_admin()){
 
 $new_page_title = 'InsertCart Front'; 
 $new_page_content = 'My Short Code
[Any short code function]
Titles 
 This is the page content';
 $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
 //don't change the code bellow, unless you know what you're doing
 $page_check = get_page_by_title($new_page_title);
 $new_page = array(
 'post_type' => 'page',
 'post_title' => $new_page_title,
 'post_content' => $new_page_content,
 'post_status' => 'publish',
 'post_author' => 1,
 );
 if(!isset($page_check->ID)){
 $new_page_id = wp_insert_post($new_page);
 if(!empty($new_page_template)){
 update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
 }
 }
 
 $setpage = get_page_by_title( 'InsertCart Front' );
 update_option( 'page_on_front', $setpage->ID );
 update_option( 'show_on_front', 'page' );

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

}

Just copy and paste above code into theme function.php file Above code won’t overwrite your current page if already created. Rewrite above settings as you want let me know if any question or suggestion about my post.

Comments

One response to “Create Set Static Front Page and Blog with Content on Theme Activate”

  1. Chelsea Avatar
    Chelsea

    Exactly what I was looking for, thank you!!!

Leave a Reply

Your email address will not be published. Required fields are marked *