Skip to main content

How to create wordpress page with code in wordpress?

Create wordpress page with code in wordpress.Define page template in wp_insert_post.

add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'mytheme_installed' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Blog',
            'post_type'      => 'page',
            'post_name'      => 'blog',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            'page_template'  => 'template-blog.php'
        ) );

        update_option( 'mytheme_installed', true );
    }
}
In working...........
add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'front page' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'front page',
            'post_type'      => 'page',
            'post_name'      => 'front page',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => 1,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'front page', true );
    }
 
      if ( ! get_option( 'Contact Us' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Contact Us',
            'post_type'      => 'page',
            'post_name'      => 'Contact Us',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Contact Us', true );
    }
 
 
      if ( ! get_option( 'About Us' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'About Us',
            'post_type'      => 'page',
            'post_name'      => 'About Us',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'About Us', true );
    }
 
 
      if ( ! get_option( 'Privacy Policy' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Privacy Policy',
            'post_type'      => 'page',
            'post_name'      => 'Privacy Policy',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Privacy Policy', true );
    }
 
 
     if ( ! get_option( 'term and condition' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'term and condition',
            'post_type'      => 'page',
            'post_name'      => 'term and condition',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'term and condition', true );
    }
 
 
   if ( ! get_option( 'Test' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Test',
            'post_type'      => 'page',
            'post_name'      => 'Test',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Test', true );
    }
}

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...