Comments On Dynamically created Pages in Wordpress
Posted By : Annu Sehrawat | 21-Apr-2020
Comments On Dynamically Created Pages in Wordpress
Wordpress provides a default comments table i.e (wp_comments) to store comments on its post, pages and also we can store comments of custom post type but sometimes if we have a requirement to store comments on the dynamically created page.
For Eg:- If I have a page with a custom template in Wordpress. With the actual page content (like header and footer) which is similar for all dynamically created pages, we also show the dynamically created pages content which is different for all pages and differentiated via passed in URL variables.
Suppose I created a page called Blogs in WordPress and assigned it to my ‘blog.php’ template. Users can hit the page at https://mywebsite.com/blogs
They can also go to https://mywebsite.com/blogs/what-is-ML/ and the page template reads in the “blog URL” via our get_query_var(‘blog_url’) and it shows blog content for just that blog.
Here's what our requirement is to add comments to the "specific blog" which is not an actual page inside WordPress, but are created on the basis of “blog URL”.
We can add comments_template() to the page, but the comments_template() is based on page id or post id. So if we add a comment on one blog then, it will also show on all blogs because all blogs have the same page id.
For eg:- If we post a comment on https://mywebsite.com/blogs/what-is-ML/ then it will also show on https://mywebsite.com/blogs/what-is-AI/ but we don’t want to show comments on https://mywebsite.com/blogs/what-is-ML/ to show up on https://mywebsite.com/blogs/what-is-AI/ and vice-versa.
There are 2 ways to do that, one is to customize the wp_comments table and add a new unique field but I don’t think it’s a good idea to mess up with the main functionality of wordpress. Second one is to create your own custom table (e.g wp_custom_comments) having the same structure as the default wp_comments table but with your extra fields.
You can write a function in your function.php file to create a custom comments table like this :-
$table_name = $wpdb->prefix. "custom_comments";
$charset_collate = $wpdb->get_charset_collate();
if( $wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") != $table_name)
$sql = "CREATE TABLE `$table_name` (
`comment_ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`comment_post_ID` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`comment_author` tinytext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`comment_author_email` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`comment_author_url` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`comment_author_IP` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`comment_karma` int(11) NOT NULL DEFAULT 0,
`comment_approved` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
`comment_agent` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`comment_type` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`comment_parent` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
`user_id` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
/* here you can add your own custom fields*/
PRIMARY KEY (`comment_ID`))$charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
Once the table is created successfully, you can store your comments on dynamically created pages and can also show that data in the wordpress admin panel using ‘WP_List_Table’ class.