Automatically create pages by activating a theme WordPress

  • Spammers will be banned immediately without warning. Read our Signup Policies here.
  • 🎉 We've Hit 1 MILLION VIEWS in April 2025! 🚀

    We're proud to announce that blackhatforums.net has officially crossed 1,000,000 page views in a single month — April 2025! 🤯

    This milestone is a direct result of your hustle, strategies, and contributions. Whether you're dominating SEO, pushing traffic, or just absorbing knowledge — this is your win. 💪

    👉 Read the Celebration Thread Here

    — The blackhatforums.net Team

albertc

Newbie
Messages
37
Likes
1
Points
8
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.