WordPress.org

Ready to get started?Download WordPress

Forums

auto-create page upon activation of plugin (6 posts)

  1. rcvaughn
    Member
    Posted 2 years ago #

    Ok so I have a plugin that I would like to have automatically create a page on activation, but every piece of code I have tried has yet to work. Every once in a while, it will decide to add the page, but I can never get it to repeat itself. (deleting page, deactivating, then reactivating plugin to test)

    Here are the scenerios I have tried.

    <?php
    wp_insert_post( $new_page );
    
    $new_page = array(
    'post_title' => 'Employees',
    'post_name' => 'employees',
    'post_status' => 'publish',
    'post_type' => 'page',
    'post_author' => $user_ID,
    'post_parent' => 0,
    'menu_order' => 0
    );
    
    	//if we have created any new pages, then flush...
    	if ( $newpages ) {
    		wp_cache_delete( 'all_page_ids', 'pages' );
    		$wp_rewrite->flush_rules();
    	}
      ?>
    <?php
    if ($_GET['activated']){
    
        $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(isset($new_page_template)){
                update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
            }
        }
    
    }
    ?>

    And the one wordpress has posted in the codex:

    <?php
    // Create post object
      $my_post = array(
         'post_title' => 'My post',
         'post_content' => 'This is my post.',
         'post_status' => 'publish',
         'post_author' => 1,
         'post_category' => array(8,39)
         'post_type' => 'page' /* this actually makes the entire backend to disappear so I have to put it in $defaults=array( for it to reappear. The page doesn't always want to show up though.*/
      );
    
    // Insert the post into the database
      wp_insert_post( $my_post );
    ?>

    With this last bit, I can get the pages to appear, but posts are also created...I don't need them!

  2. rcvaughn
    Member
    Posted 2 years ago #


  3. borellidesigns
    Member
    Posted 2 years ago #

    Hi rcvaughn!

    Try getting rid of your declaration for 'post_category'

    From your code above, I see that you're setting the 'post_category' and right below setting the 'post_type' to 'page'. You should eliminate the post_category value as it's only associated to posts and not pages.

    Declaring this could be why you're creating both posts and pages, and/or confusing the loop to basically do what it wants.

    I'm developing something similar with this coding so should have some more results to share shortly.

    Hope this helps!

    ~ Shawn

  4. vabmedia
    Member
    Posted 2 years ago #

    Hey BorelliDesigns did you ever develop something? I am looking to atuto create page templates for custom post types, but I'm using the Custom Post Type UI. But something should be available because you can make a default template for categories. Anyway, let me know.

  5. borellidesigns
    Member
    Posted 2 years ago #

    Hi vabmedia!

    For our project we decided just to use shortcodes and create buttons for inserting these though the visual editor.

    Below is an untested code you could use to auto-create pages with a plugin or theme. Basically, you want to create the post/page, then insert it with a global WP function.

    $my_page = array(
    	'post_title' => 'Our New Auto-Created Page',
    	'post_content' => 'This is a new page. You can add any content you want here, including shortcodes.',
    	'post_status' => 'publish',
    	'post_type' => 'page',
    	'post_author' => 2,
    	'post_date' => '2012-08-20 15:10:30'
    );
    
    $post_id = wp_insert_post($my_page);

    Check out this post for more info: http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/

    Hope this helps!!
    ~ Shawn

  6. vabmedia
    Member
    Posted 1 year ago #

    Thanks Borellidesigns!! Appreciate the code! I'll try it out

Topic Closed

This topic has been closed to new replies.

About this Topic