{"id":470,"date":"2011-07-09T14:30:28","date_gmt":"2011-07-10T00:30:28","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=470"},"modified":"2011-07-09T14:30:28","modified_gmt":"2011-07-10T00:30:28","slug":"restrict-comments-on-wordpress-page","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/","title":{"rendered":"Restrict Comments on WordPress Page"},"content":{"rendered":"

Problem:<\/h2>\n

A client needed just one page to have comments where the user must log in. \u00a0The client wanted the rest of the pages to be open to anyone to comment. \u00a0I found no such plugin.<\/p>\n

Solution:<\/h2>\n

At first I was going to write a plugin, but being that I have never wrote an official plugin before I decided to go the simpler route. \u00a0I ended up creating 3 template files and adding a function to the functions.php file. \u00a0 Since almost all pages will allow anonymous comments, the wordpress setting will allow comments. \u00a0The following changes will prevent users from posting a comment to specific page-templates.<\/p>\n

page-comment-restrict.php<\/strong><\/p>\n

This is a copy of page.php. \u00a0the only difference is the loop part will call ‘page-comments-restrict’ instead of the ‘page’ template. \u00a0Use this template for each page that needs the comments restricted.<\/p>\n

loop-page-comment-restrict.php<\/strong><\/p>\n

This is a copy of loop-page.php. \u00a0The only difference is the comment template. \u00a0Instead of calling the default comment template it will call ‘\/comments-restrict.php’.<\/p>\n

comments_template( '\/comments-restrict.php', true );<\/pre>\n

comments-restrict.php<\/strong><\/p>\n

This is a copy of the comments.php. \u00a0 The difference is you need to copy some login code from “\/wp-includes\/comment-template.php”. \u00a0Specifically the part where it checks for comment_registration and is_user_logged_in. \u00a0After making a copy of comments.php, replace comment_form() with the code below.<\/p>\n

\t\t<?php if ( comments_open() ) : ?>\n\t\t\t<?php do_action( 'comment_form_before' ); ?>\n\t\t\t<div id=\"respond\">\n\t\t\t\t<h3 id=\"reply-title\"><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?> <small><?php cancel_comment_reply_link( 'Cancel reply' ); ?><\/small><\/h3>\n\t\t\t\t<?php if ( !is_user_logged_in() ) : ?>\n\t\t\t\t\t<?php echo '<p>' .  sprintf( __( 'You must be <a href=\"%s\">logged in<\/a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '<\/p>'; ?>\n\t\t\t\t\t<?php do_action( 'comment_form_must_log_in_after' ); ?>\n                <?php else :\n                    comment_form();\n                endif; ?>\n        <?php endif; ?><\/pre>\n

 <\/p>\n

functions.php<\/strong><\/p>\n

All the changes up until now are to hide the comments form on the page. \u00a0However, a malicious user could still try to post a comment to your page because the wordpress setting is to allow anonymous comments. \u00a0To prevent this, we need to catch all comments before it is posted by adding an action hook at ‘pre_comment_on_post’. \u00a0 Add the following code to the functions.php file.<\/p>\n

 <\/p>\n

 <\/p>\n

if ( ! function_exists( 'comments_restrict' ) ) :\n\/**\n * Restricts comments to logged in users only for the specific pages determined by page template.\n *\n *\/\nfunction comments_restrict($post_ID) {\n    $meta_value = get_post_meta($post_ID, '_wp_page_template', true);\n    if ($meta_value == 'page-comments-restrict.php') {\n        $user = wp_get_current_user();\n        if (!$user->ID) wp_die( __('Sorry, you must be logged in to post a comment.') );\n    }\n}\nendif;\n\nadd_action('pre_comment_on_post', 'comments_restrict');<\/pre>\n

 <\/p>\n

That’s it. \u00a0It should restrict users who are not logged in from commenting. \u00a0Test it out with the following steps:<\/p>\n

    \n
  • Replace the code in comments-restrict.php with the normal comments_form(); method. \u00a0 Try to post a comment. \u00a0The result should be a page that says you need to be logged in.<\/li>\n
  • View the page with and without the wordpress setting to allow anonymous comments. \u00a0You should see no difference.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[54,64],"yoast_head":"\nRestrict Comments on Wordpress Page - My Monkey Do<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Restrict Comments on Wordpress Page - My Monkey Do\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/\" \/>\n<meta property=\"og:site_name\" content=\"My Monkey Do\" \/>\n<meta property=\"article:published_time\" content=\"2011-07-10T00:30:28+00:00\" \/>\n<meta name=\"author\" content=\"Webhead\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Webhead\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/\",\"url\":\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/\",\"name\":\"Restrict Comments on Wordpress Page - My Monkey Do\",\"isPartOf\":{\"@id\":\"https:\/\/mymonkeydo.com\/#website\"},\"datePublished\":\"2011-07-10T00:30:28+00:00\",\"dateModified\":\"2011-07-10T00:30:28+00:00\",\"author\":{\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\"},\"breadcrumb\":{\"@id\":\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mymonkeydo.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Restrict Comments on WordPress Page\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mymonkeydo.com\/#website\",\"url\":\"https:\/\/mymonkeydo.com\/\",\"name\":\"My Monkey Do\",\"description\":\"A Log of Coding Solutions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mymonkeydo.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\",\"name\":\"Webhead\",\"url\":\"https:\/\/mymonkeydo.com\/author\/corey\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Restrict Comments on Wordpress Page - My Monkey Do","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/","og_locale":"en_US","og_type":"article","og_title":"Restrict Comments on Wordpress Page - My Monkey Do","og_url":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/","og_site_name":"My Monkey Do","article_published_time":"2011-07-10T00:30:28+00:00","author":"Webhead","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Webhead","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/","url":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/","name":"Restrict Comments on Wordpress Page - My Monkey Do","isPartOf":{"@id":"https:\/\/mymonkeydo.com\/#website"},"datePublished":"2011-07-10T00:30:28+00:00","dateModified":"2011-07-10T00:30:28+00:00","author":{"@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829"},"breadcrumb":{"@id":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mymonkeydo.com\/restrict-comments-on-wordpress-page\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mymonkeydo.com\/"},{"@type":"ListItem","position":2,"name":"Restrict Comments on WordPress Page"}]},{"@type":"WebSite","@id":"https:\/\/mymonkeydo.com\/#website","url":"https:\/\/mymonkeydo.com\/","name":"My Monkey Do","description":"A Log of Coding Solutions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mymonkeydo.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829","name":"Webhead","url":"https:\/\/mymonkeydo.com\/author\/corey\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/470"}],"collection":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":0,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:attachment":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}