Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set Reading Settings on Theme Activation
<?php
/**
* Set Reading Settings on Theme Activation
*
* @author Edwin Velez (https://edwinvelez.net)
* @link https://edwinvelez.net/set-reading-settings-on-theme-activation/
*/
function ev_set_reading_settings() {
$author_id = 1;
$page_title_home = 'Home';
$page_title_blog = 'Blog';
// Does the Home page exist?
if ( null == get_page_by_title( $page_title_home, 'page' ) ) {
// Home page does not exist so it must be created.
wp_insert_post(
array(
'post_author' => $author_id,
'post_title' => $page_title_home,
'post_status' => 'publish',
'post_type' => 'page',
)
);
}
// Does the Blog page exist?
if ( null == get_page_by_title( $page_title_blog, 'page' ) ) {
// Blog page does not exist so it must be created.
wp_insert_post(
array(
'post_author' => $author_id,
'post_title' => $page_title_blog,
'post_status' => 'publish',
'post_type' => 'page',
)
);
}
// Set static front page.
update_option( 'page_on_front', get_page_by_title( $page_title_home )->ID );
update_option( 'show_on_front', 'page' );
// Set blog page.
update_option( 'page_for_posts', get_page_by_title( $page_title_blog )->ID );
}
add_action( 'after_switch_theme', 'ev_set_reading_settings' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.