wordpress snippets last updated   : July 14, 2014
we now have 634 snippets

Create page on theme activation

wordpress snippet

If you are creating a new theme either for sale of for a client you may want to have it create a custom page or pages when the theme is activated. Adding this snippet to the functions.php of your theme will do just that.

snippet :  PHPcopy
if (isset($_GET['activated']) && is_admin()){
	$new_page_title = 'This is the page title';
	$new_page_content = '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);
		}
	}
}
source →
  • Pingback: Tweets that mention Wordpress Create page on theme activation – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com

  • http://www.cliffpaulick.com Cliff Paulick

    Sorry if a dumb question, but why would you want to do this?
    I have a test WP installation where I manually create a new post (or sometimes a page) whenever I activate a new plugin – to test out what it can do. I can see the benefit of a page/post generator for that action, but I’m not sure why a new page for activating a theme. Please help me understand. Thanks!

    • http://wpsnipp.com Kevin Chard

      No such thing as a dumb question on wpsnipp, this is a good question. Lets say that I have a new theme that will function more like a CMS with a default static front page. You could use this in your theme to create the needed pages for the person rather then asking them to do it by hand. This way I could create any number of pages needed for a theme I was creating then all the person would need to do is link pages to templates required. But this is just one example could be reasons we can’t even think of.

      • Vignesh Diwakar

        How I add this new page created by your code in main menu. (‘post_type’ => ‘nav-menu-item’) not working for me

    • chaos67731

      This is kind of old, but for me I am making a theme and setting up a custom page that will make a site map, and I want to link to that sitemap in the footer.

      This is a great bit of code, it lets me know for sure that this sitemap page will be there all the site.

  • http://www.tysonpark.com Tyson

    Nice! Big thanks!
    Just used your code to add menu items as well.
    (‘post_type’ => ‘nav-menu-item’)

    • http://wpsnipp.com Kevin Chard

      Hi Tyson, Cool glad I could help. Don’t forget to follow on twitter, facebook or RSS I post new snippets regularly.

  • http://zeaks.org/ Zeaks

    I don’t suppose there’s a way of directing users to the page after activation is there no matter what permalnk they have set is?

    I wanted to use this to create a theme FAQ page and direct the user to it after activation.

    • http://wpsnipp.com Kevin Chard

      I’m don’t think that is the best way to do things what you could do is create an admin page much like people do when they create a plugin and add your FAQ or other details in that. This tutorial on net.tuts has details about creating the admin panel for a plugin but the idea is the same.

      http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/

      • http://zeaks.org/ Zeaks

        Yeah You’re probably right. I’m using a child theme for Twenty Eleven and haven’t figure out how to hook into the current theme options yet. I’ll probably just create a html page with the info I need on it and link it but will check that link out, thanks

        • http://wpsnipp.com Kevin Chard

          Hi Zeaks,

          The tutorial I posted should work fine! Should not matter the theme
          you are using, the code in the tutorial will just have you add code to
          your functions.php to create the admin panel then it is just html to
          style and display your faq or other information.

    • http://wpsnipp.com Kevin Chard

      I’m don’t think that is the best way to do things what you could do is create an admin page much like people do when they create a plugin and add your FAQ or other details in that. This tutorial on net.tuts has details about creating the admin panel for a plugin but the idea is the same.

      http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/

  • http://cudjex.com/ cudjex

    Hi Kevin. How to create multiple pages with this method?Thankss..

  • Sandip

    Hi in my plugin automatic page generated and short code also automatic generated. i want in detail page no need to display header footer and sidebar.. any idea please ?

jqsnipp.com | we just launched jquery snippets website