Create Page in WordPress Database on Plugin Activation

Reading Time: 5 minutes
1,664 Views

Inside this article we will see how to create page in wordpress database on plugin activation. Article contains very classified information about generating dynamic pages on plugin activation.

After this article you will be sure how to create your own pages for wordpress by your own custom plugin. We usually create custom pages in wordpress when we need some more views of custom plugin or theme for UI.

Apart from creating pages also we can add more functions which we want should be executed at the time of activation.

WordPress works on the basis of filters and hooks. WordPress also provides a activation hook. We will see here into complete detail.

Learn More –

Let’s get started.


Plugin Activation Hook

WordPress provide a hook for activation. We use this activation to attach all functions that we need on plugin activation or theme activation.

register_activation_hook(__FILE__, callback);

callback function contains all code which will be executed when plugin/theme activates. We will add our dynamic table generation code into this.


Code To Generate Table

Here, is the steps to generate dynamic page.

  • Add Activation hook
  • Callback function
function add_my_custom_page()
{
    // Create post object
    $my_post = array(
        'post_title'    => wp_strip_all_tags('My Custom Page'),
        'post_content'  => 'My custom page content',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_type'     => 'page',
    );

    // Insert the post into the database
    wp_insert_post($my_post);
}

register_activation_hook(__FILE__, 'add_my_custom_page');

Plugin Setup

We will see dynamic page generation using a plugin. So, we will create a custom plugin here.

Create a folder mytable inside /wp-content/plugins folder. Inside this mytable folder, create plugin entry file mytable.php.

Open mytable.php and write this code into it.

<?php

/**
 * Plugin Name:       OWT MyTable
 * Plugin URI:        https://sample-plugin.com
 * Description:       This plugin when activates, create a page inside database.
 * Version:           1.0
 * Author:            Online Web Tutor
 * Author URI:        https://onlinewebtutorblog.com/
 */

function add_my_custom_page()
{
    // Create post object
    $my_post = array(
        'post_title'    => wp_strip_all_tags('My Custom Page'),
        'post_content'  => 'My custom page content',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_type'     => 'page',
    );

    // Insert the post into the database
    wp_insert_post($my_post);
}

register_activation_hook(__FILE__, 'add_my_custom_page');

Once plugin will setup inside wordpress, you should see inside Plugins >> Installed Plugins

Click on Activate

Successfully we have created a page for custom plugin.

We hope this article helped you to learn Create Page in WordPress Database on Plugin Activation Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more