Monday, June 28th, 2010 | Tips, Web Dev, downloads

{2}

How to add multiple pages at once with WordPress

Ok folks, this will be short. If you want to add several pages at once, I created a little script for you.

Here it is:

<?php
 include ('wp-blog-header.php');
 $newPages = array(
 'Page 1' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 2' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 3' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 4' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
 );
 foreach($newPages as $pagename => $content) :

 $postarr = array(
 'post_title'         =>     $pagename,
 'post_content'         =>     $content,
 'post_status'        =>    'publish', // can be changed to 'draft', 'publish', 'pending', or 'future'
 'post_author'        =>    1, // post author 1 is admin, can be changed to the ID of the post author
 'post_type'            =>    'page', // can also use 'post' here.
// 'post_parent'            =>    '22', // (completely optional) you can assign a parent ID to create child pages.
 );
 wp_insert_post($postarr); ?>
 <p>Added <?php echo $pagename?>.</p>
 <?php endforeach; ?>

And here’s how it works:

  1. the include wp_blog_header() is pulling in the file wp_blog_header.php, which gets all the important data from your WordPress blog, allowing you to access the database (very important).
  2. the $newpages variable is storing an array that is created in the next 4 lines.
  3. The foreach cycles through the array, assigning $pagename to the left side of the = and $content to the right, so whatever you put to the left side of the = will be your page title, whatever you put to the right side will be your content (simple enough).
  4. The function wp_insert_post() is called, passing the parameter of the array that was created in step 2.

That’s it, your new pages have been added. Be very careful not to reload the page, or navigate to this page again, or it will duplicate the pages, which can be quite frustrating to straighten out. But that’s it. It really is that easy. You can also see my comments in blue.

I might make a plugin that you can drop into a site to enable users to frame out a site with the pages needed and whatnot. Maybe I’ll make that my next project, anyway, hopefully you found it useful, if you’d like to download the php file, you can get it below:

insertposts.php

Put this file in your root directory (so like: http://geekoutwith.me/insertposts.php). If you put it somewhere else, you’ll have to change the path of the include for wp-blog-header.php

Leave a Comment

Recent Posts

How to Quickly Create Reusable Color Palettes in TextMate

Below is a quick video tutorial of using Mac OS’ built-in color picker to create color palletes for use in your CSS documents.

Read the Full Article

Photoshop Screencast: Creating Torn Edges

I was recently working on a project and came up with a decently automated solution for creating grunge torn edges in photoshop…and here it is:

Read the Full Article

Photoshop Screencast: How to use the defringe tool

I was working on an email blast for a client when I remembered how effective the defringe tool is, and though I don’t explain it too thoroughly in this screencast, it might be helpful for you.

Read the Full Article

Using CSS Media Queries

I wanted my website to be a little easier to read in the iPhone. I knew there was a new CSS3 property called Media Queries. This allows you to discriminate based on window size. So, in the header, as I would normally pull in a stylesheet, I can say: And that will pull in a stylesheet for the iPhone, mine look…

Read the Full Article

My first guest post at buildinternet.com

I wrote the third part of the embeddable mp3 players series over at buildinternet.com. Those guys graciously allowed me to geek out on their blog. Since they get way more traffic than I do, I think more people will find it useful. You can see how they make me look good here: http://buildinternet.com/2…

Read the Full Article

I love jQuery

The theme GeekOut, which as of this moment I am using on this site and may release once I decide to go in a different direction with the design, uses jQuery to accomplish a number of things. 1. The animations that are featured both in the navigation and the social media icons I’m using the jQuery an…

Read the Full Article

Helpful WordPress Shortcode – showthumbs

I was working on my daughter’s blog, which might be the most robust blog I’ve built, because it is my testing grounds for new features to add to WordPress sites/blogs. Tonight I added a neat kind of functionality, something I’d like to share. Shortcode to show thumbnails. Here&#…

Read the Full Article

Building stuff

We have a sort of tradition in my little family. Every Saturday we get up and make breakfast together. Sometimes we make Eggs, Bacon, biscuits and hash browns, other mornings we have waffles or pancakes. It’s always a fun way to start the day together. The next thing we do is something productiv…

Read the Full Article