We’re back again with another exciting and code-filled tutorial. This time, we’re going to create a WordPress page, not manually, but programmatically! Get ready to take a deep dive into the code!
The functions.php file is the beating heart of your WordPress theme. It’s where you can add your own custom functions to enhance your theme’s capabilities. So, let’s start by opening this file. You can find it in your current theme’s directory. The path usually looks something like this: /wp-content/themes/your-theme/functions.php.
Once you’ve opened the functions.php file, it’s time to add a new function. This function will create a new WordPress page for us. But how? Well, WordPress provides a handy function called wp_insert_post() which does exactly that. It inserts a new post into the database. Let’s create our function:
function create_my_page() {
// Page data
$my_post = array(
'post_title' => 'My awesome page',
'post_content' => 'This is the content of my awesome page.',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page' // Set the post type to 'page'
);
// Insert the post into the database
wp_insert_post( $my_post );
}
In the above code, we’re creating a new page with the title ‘My awesome post’, some content, a status of ‘publish’, an author ID of 1, and assigning it to categories with the IDs 8 and 39.
Now, we have our function ready, but it’s not doing anything yet because we haven’t called it. To call this function, you can add the following code after the function:
create_my_post();
But beware! With the function call here, a new page will be created every time WordPress loads. We don’t want that, do we? Instead, you could call this function in response to a specific event, like form submission, button click, or something else.
function create_my_page() {
// Page data
$my_post = array(
'post_title' => 'My awesome page',
'post_content' => 'This is the content of my awesome page.',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page' // Set the post type to 'page'
);
// Insert the post into the database
wp_insert_post( $my_post );
}
// Uncomment the following line to create a new post on page load.
// create_my_post();
As you can see, creating pages programmatically in WordPress is as simple as that. This gives you the power to generate content dynamically and could be especially handy in certain scenarios.
Remember, with great power comes great responsibility. Be careful about when and how you call your function to avoid any unintended consequences like flooding your site with new posts.
Happy coding, everyone!
Jonas is the Co-Founder of LPagery, a yearlong WordPress enthusiast and expert with over 8 years of WordPress experience.
Enables the creation of unique, personalized landing pages at scale by offering unlimited placeholders for dynamic content.
Essential for crafting content-rich, unique pages for each visitor or target group, optimizing for local SEO, e-commerce variability, or specific event details.