RAJMOHAN RAJMOHAN - 3 months ago 8
PHP Question

How to create a new page in wordpress from template page?

I am in a Wordpress Project and I am Developing a Tree structure to implementing Projects and SubProjects and its Subprojects and going on.

I could manage to create a table in database when user creates new level of projects. But each project need a specific wordpress Page. I struggling to get a page created when user go for new projects. How it possible those create a wordpress page when an unlogined to wordpress user tries to create a page?

In short I want to create a page inside wordpress on clicking a Button from outside the wordpress. Keep that the user is not logined on the wordpress, he has no relation to wordpress. It is the process happen on the front end.How it make possible?

Answer

I have the exact same setup and I'm using a custom post type called project to achieve this. Here's how to set it up:

  1. Register you custom post type in the functions.php

    add_action( 'init', 'rajmohan_register_project_post_type' );
    
    function rajmohan_register_project_post_type() {
    
        // Set UI labels for Custom Post Type
        $labels = array(
            'name'                  => 'Projects',
            'singular_name'         => 'Project',
            'menu_name'             => 'Post Types',
            'name_admin_bar'        => 'Post Type',
            'archives'              => 'Project Archives',
            'parent_item_colon'     => 'Parent Project:',
            'all_items'             => 'All Projects',
            'add_new_item'          => 'Add New Project',
            'add_new'               => 'Add New',
            'new_item'              => 'New Project',
            'edit_item'             => 'Edit Project',
            'update_item'           => 'Update Project',
            'view_item'             => 'View Project',
            'search_items'          => 'Search Project',
            'not_found'             => 'Not found',
            'not_found_in_trash'    => 'Not found in Trash',
            'featured_image'        => 'Featured Image',
            'set_featured_image'    => 'Set featured image',
            'remove_featured_image' => 'Remove featured image',
            'use_featured_image'    => 'Use as featured image',
            'insert_into_item'      => 'Insert into Project',
            'uploaded_to_this_item' => 'Uploaded to this Project',
            'items_list'            => 'Projects list',
            'items_list_navigation' => 'Project list navigation',
            'filter_items_list'     => 'Filter Projects list',
        );
    
        // Set other options for Custom Post Type
        $args = array(
            'label'               => 'Project',
            'description'         => 'Projects',
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => false,
            'exclude_from_search' => true,
            'publicly_queryable'  => true,
            'capability_type'     => 'post',
        );
    
        // Registering your Custom Post Type
        register_post_type( 'project', $args );
    }
    
  2. Create a template for a page that creates the new project, for example new-project.php

    <?php
    /*
        Template Name: rajmohan-new-project
    */
    
    // Create post object
    $project = array(
        'post_type'   => 'project',
        'post_status' => 'publish',
        'post_author' => 1,
    );
    
    // Insert the post into the database
    $id = wp_insert_post( $project );
    $permalink = get_permalink( $id );
    wp_redirect( $permalink );
    
  3. Create a template called single-project.php in the root of your theme folder. This template will display the data for a single project.

    <?php
    echo 'Hello world!';
    // This is where you would print out any info about the project which are stored in the global post object
    
  4. Go to your WordPress dashboard and create a new page called 'New Project' and assign the new-project.php template to it.

  5. Finally you will need to update your rewrite rules to include the new custom post type by going to 'Settings > Permalinks' in your WordPress dashboard and clicking the save changes.

  6. Now when your visitors navigate to the New Project page (example.com/new-project) a new project will be created and they will be redirected to view the project (WordPress will use the single-project.php we created to display data for this project).