Skip to content

Instantly share code, notes, and snippets.

@iamtyce
Created August 10, 2013 06:43
Show Gist options
  • Save iamtyce/6199378 to your computer and use it in GitHub Desktop.
Save iamtyce/6199378 to your computer and use it in GitHub Desktop.
Page Addition Function (WordPress)
<?
// **********************
// PAGE ADDITION FUNCTION
// **********************
if ($_GET['add_pages'] == "true") {
global $current_screen;
$user_id = $_GET['user_id'];
$user_meta_cats = get_user_meta($user_id,'events');
if ($user_meta_cats[0]) {
foreach($user_meta_cats[0] as $user_cats) {
$pages = array(
array(
'name' => $user_id.'-what-we-are-about',
'title' => 'What We Are About',
'order' => 1,
'child' => array(
array(
'name' => '1-1',
'title' => 'Key Results',
'order' => 1
),
array(
'name' => '1-2',
'title' => 'Event Overview',
'order' => 2
),
array(
'name' => '1-3',
'title' => 'Brand Value',
'order' => 3
)
)
),
array(
'name' => $user_id.'-where-you-could-fit',
'title' => 'Where You Could Fit',
'order' => 2,
'child' => array(
array(
'name' => '2-1',
'title' => 'Partnership Opportunities',
'order' => 1
),
array(
'name' => '2-2',
'title' => 'Tailored Proposal',
'order' => 2
)
)
),
array(
'name' => $user_id.'-our-program',
'title' => 'Our Program',
'order' => 3,
),
array(
'name' => $user_id.'-what-we-can-do-for-you',
'title' => 'What We Can Do For You',
'order' => 4,
'child' => array(
array(
'name' => '4-1',
'title' => 'Exposure &amp; Comms',
'order' => 1
)
)
),
array(
'name' => $user_id.'-our-audience',
'title' => 'Our Audience',
'order' => 5,
'child' => array(
array(
'name' => '5-1',
'title' => 'Who We Speak To',
'order' => 1
),
array(
'name' => '5-2',
'title' => 'Who Follows Us',
'order' => 2
)
)
),
array(
'name' => $user_id.'-next-steps',
'title' => 'Next Steps',
'order' => 6,
'child' => array(
array(
'name' => '6-1',
'title' => 'Action Plan',
'order' => 1
)
)
),
array(
'name' => $user_id.'-contact-us',
'title' => 'Contact Us',
'order' => 7
)
);
$template = array(
'post_type' => 'sponsors',
'post_status' => 'publish',
'post_author' => $user_id
);
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'],
'post_title' => $page['title'],
'menu_order' => $page['order']
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
// Set category for new post
wp_set_object_terms( $id, $user_cats, 'category' );
// Set default template for new post
add_post_meta($id, 'template', 'Blank');
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $child_page ) {
$child_id = get_page_by_title( $child_page['name'] );
$child_page = array(
'post_name' => $child_page['name'],
'post_title' => $child_page['title'],
'menu_order' => $child_page['order'],
'post_parent' => $id
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) $child_post_id = wp_insert_post( $child_page );
// Set category for new post
wp_set_object_terms( $child_post_id, $user_cats, 'category' );
// Set default template for new post
add_post_meta($child_post_id, 'template', 'Blank');
}
}
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment