Add Dynamic page in wordpress

How to Create a Dynamic page in WordPress

Hello, friends recently, while working on one of my projects, I need to fetch some details from the Database in WordPress. Earlier I have worked on many projects, but never fetch data from the database and display the result on the page as per the custom database table.

So, I thought to share this with all my friends may it will help someone. I will demonstrate step by step how we can achieve it. In this tutorial, we will learn How to add a dynamic page in WordPress. Let’s start it.

I will show you step by step but it will be better if you have prerequisite knowledge of PHP and mysql.

Let’s divide into three parts:-

  1. Database Part.
  2. PHP coding part.
  3. Creating a page in the WordPress Admin Panel and attach that template we have created in the second phase.

1. Database

First, we need to login to the phpMyAdmin from the Cpanel. You need to find the database name from the config file in the root directory of the website.

wordpress how to create dynamic page templete

We can see the wp-config.php file in the root directory. In the right, we can see the opened file
there you can find the database name in line number 23. Similarly, the user and password just below that.

After login creates a table in the selected database. After creating the table inserts some dummy data. I have added the sample data and table creation query.

CREATE TABLE `user_data` (
  `id` int(32) NOT NULL,
  `name` varchar(300) NOT NULL,
  `city` varchar(100) NOT NULL,
  `mobile` int(11) NOT NULL

INSERT INTO `user_data` (`id`, `name`, `city`, `mobile`) VALUES
(1, 'John', 'NewYork', 132323232),
(2, 'Karl', 'Sydney', 324324232),
(3, 'David', 'Belgium', 132567232),
(4, 'Michel', 'Newyork', 23232323);

2. Create a Template for showing the data to the user

After creating the table and inserting the dummy data. Let’s create the page template for fetching the data from the database and showing it to the user. For that, we need to create a PHP file in the themes folder. We need to navigate to the theme folder in the wp-content folder in the root directory. In theme, folder navigates to the specific theme that you are using for your site.

Wordpress page creation

For example in the theme folder, there are four themes. I have used the Generate Press theme so, go that folder. I have created data.php in this file we will write our code to fetch the data from the database and display in our page.

After creating page we will need to write the code from that we will fetch the data and display on the front end.

Sample Code

/* Template Name: Display Dynamic Data */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.

    global $wpdb;
    $result = $wpdb->get_results("select * from user_data"); ?>
	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
	    <!--Main Navigation-->

	<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
		<div id="primary">
            <div class="card mt-4" id="bank_detail_after_fetch">
                <h5 class="card-header text-center">User Details</h5>
                  <div class="card-body">
                            <div class="table-responsive-md">
                                  <td>Mobile Number</td>
                                <?php foreach ( $result as $data ) {    ?>
                                        <td><?php echo $data->name; ?></td>
                                        <td><?php echo $data->city; ?></td>
                                        <td><?php echo $data->mobile; ?></td>
                                <?php } ?>
    </main> <!-- #main -->
</div><!-- #primary -->

	 * generate_after_primary_content_area hook.
	 * @since 2.0
	do_action( 'generate_after_primary_content_area' );

	generate_construct_sidebars(); ?>

We will go through this whole code to understand the flow. At the top of the file we have defined the Template name in the Comment Section. From this, WordPress will fetch the template name.
After that we have called the get_header() function to call the header part.

In the second part we have defined the global variable to fetch the data from the database.
($result = $wpdb->get_results(“select * from user_data”))

After getting the data we have simply display the data the table format. In this way we fetch dynamic content from database. This is very simple and basic example. By using this concept
we can create many tool there we need to fetch the database from the table and display dynamic content like website where we can display city details, city pincode, bank details etc. We have just shown the data in the tabular form.

In the last we add the footer function to display the footer part. Second part is also completed in the third part we will see how we can use our template to create page and fetch dynamic data from the database in WordPress.

3. Creating a page in WordPress By Using the Page Template

In this section we will see how we can create dynamic page by using the Page template we have created in the last section. For this first we need to login into the WordPress dashboard using your credential.

After login, you will find the option to create page move to that section by clicking on the Add New Page Menu. In this, you will get an option to select a template in the page attribute section while creating New page. There add the title this will become the part of the URL. For example, you have added demo data. Then the URL for the page will be

On this page, you will get the option to preview the page. By previewing the Page you will able to see the page you have created. On that page, you will see the data you have inserted in the table.
This is the small basic example to fetch the dynamic data from the self-created table in WordPress. By using method we can create a template by using the URL we can fetch different data based on the URL. We can take an example we have created a website in which by PIN Code we are fetching the area details. Consider this URL

In this URL we have passed the PIN code in the URL. By that parameter we will fetch the data from the database and fill the content by using the fetched data. Similarly we can create many pages there we just need to change the URL and based on the URL we will fetch dynamic data from database in WordPress.

4. Conclusion

By using this we can create many tools based websites easily. If anyone needs help regarding then you can contact me at Although this is my first article I have tried my best to write. So we have learned a small topic on how to fetch the data from the database in WordPress using a custom database. The same thing happens in Plugins. In the future, I am going to create a series of Tutorials in that we will learn how we can create a Plugin. Please give your valuable comment to improve the content.