Automatically create pages by activating a theme WordPress

albertc

Newbie
Messages
32
Likes
0
Points
6
Sometimes, when you create a WordPress theme for a client , it can be more than interesting that nothing turn, automatically create pages that are of particular utility.

Whether pages useful to the specific issue (imagine secure payment pages, or product catalog or whatever is appropriate to the topic) or a page of information about your customers, it's often a good idea, and a more quality, that enabling the customer to see your subject has something just for use .

There are many issues that come with custom page templates, but this is a step, because you can create both a custom page template and even an entire page and its specialized content .

Furthermore, it is very easy, you just have to create a function in the file functions.php  theme like this, nothing else you have to modify to your liking the page title and your custom content :
Code:
// Automatic page creation

if (isset ( $ _ GET [ 'activated' ]) && is_admin ()) {

 

        $ New_page_title = 'Page title' ;

        $ New_page_content = 'Here is the content of the page, of course you can add HTML at will' ;

        $ New_page_template = '' ; // File name custom page template, for example: template-personal.php. Leave blank if you do not want to create a custom page template.

 

        // Do not change the code that follows unless you really know what you're doing

 

        $ Page_check = get_page_by_title ( $ new_page_title );
Eleven
        $ New_page = array (

                'Post_type' => 'page' ,

                'Post_title' => $ new_page_title ,

                'Post_content' => $ new_page_content ,
Fifteen
                '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 );

                }

        }

 

}

I think the same code is self-explanatory, it's up to you to decide the way you use it.

Thanks :)
 

Members online

No members online now.