{"id":147,"date":"2015-02-19T17:49:43","date_gmt":"2015-02-19T17:49:43","guid":{"rendered":"http:\/\/www.eminozlem.com\/?p=147"},"modified":"2015-06-06T21:29:59","modified_gmt":"2015-06-06T21:29:59","slug":"extending-password-protected-posts-with-name-and-email","status":"publish","type":"post","link":"https:\/\/eminozlem.com\/tr\/extending-password-protected-posts-with-name-and-email\/","title":{"rendered":"Extending password protected posts with name and email"},"content":{"rendered":"<p>If you want to add name email or any additional fields in addition to password for protected posts, see the steps below:<\/p>\n<pre class=\"lang:default decode:true \">\/****************** custom protected post form *****\/\r\nadd_filter( 'the_password_form', 'custom_password_form' );\r\nfunction custom_password_form() {\r\n\tglobal $post;\r\n\t$label = 'pwbox-'.( empty( $post-&gt;ID ) ? rand() : $post-&gt;ID );\r\n\t$a = '&lt;div class=\"clearfix\"&gt;&lt;form id=\"password_form\" class=\"protected-post-form\" action=\"' . get_option('siteurl') . '\/wp-login.php?action=postpass\" method=\"post\"&gt;\r\n\t&lt;label for=\"phone_input\"&gt;Name: &lt;\/label&gt;&lt;input type=\"text\" name=\"phone_input\" \/&gt;&lt;br \/&gt;\r\n\t&lt;label for=\"mail_input\"&gt;Email: &lt;\/label&gt;&lt;input type=\"email\" name=\"mail_input\" \/&gt;\r\n\t' . '&lt;p&gt;' . __( \"Enter Password to view post:\" ,'text-domain') . '&lt;\/p&gt;' . '\r\n\t&lt;label for=\"' . $label . '\"&gt;' . __( \"Password:\" ,'text-domain') . ' &lt;\/label&gt;&lt;div class=\"input-append\"&gt;&lt;input name=\"post_password\" id=\"' . $label . '\" type=\"password\" size=\"20\" \/&gt;&lt;input type=\"submit\" name=\"Submit\" class=\"btn btn-primary\" value=\"' . esc_attr__( \"G\u00f6nder\",'text-domain' ) . '\" \/&gt;&lt;\/div&gt;\r\n\t&lt;input type=\"hidden\" name=\"res_postid\" value=\"' . $post-&gt;ID . '\"&gt;\r\n\t&lt;\/form&gt;&lt;\/div&gt;\r\n\t';\r\n\treturn $a;\r\n}<\/pre>\n<p>Now to the validation<\/p>\n<pre class=\"lang:default decode:true\">\/**\r\n * Set the post password cookie expire time based on the email\r\n *\/\r\nadd_filter( 'post_password_expires', function( $valid ) {\r\n\r\n  $postid = filter_input( INPUT_POST, 'res_postid', FILTER_SANITIZE_NUMBER_INT );\r\n  $email = filter_input( INPUT_POST, 'mail_input', FILTER_SANITIZE_STRING );\r\n  $name = filter_input( INPUT_POST, 'name_input', FILTER_SANITIZE_STRING );\r\n  $subpw = filter_input( INPUT_POST, 'post_password', FILTER_SANITIZE_STRING );\r\n  \/\/ a timestamp in the past\r\n  $expired = time() - 10 * DAY_IN_SECONDS;\r\n  if ( empty( $postid ) || ! is_numeric( $postid ) ) {\r\n      \/\/ empty or bad post id, return past timestamp\r\n      return $expired;\r\n  }\r\n\r\n  if ( empty($email) || ! filter_var($email, FILTER_VALIDATE_EMAIL) ) {\r\n      \/\/ empty or bad email id, return past timestamp\r\n      return $expired;\r\n  }\r\n  \r\n  $the_post = get_post( $postid );\r\n\t$postpw = $the_post-&gt;post_password;\r\n\tif($postpw !== $subpw) return $expired;\r\n\/\/see below\r\n  \/\/ pw_post_email_notify($name,$email);\r\n  return $valid;\r\n\r\n}, 0 );<\/pre>\n<p>What you want to do with the information (name, email) is up to you. I wanted to be notified who logged in to view the post so I wrote a email notification as follows:<\/p>\n<pre class=\"lang:default decode:true  \">function pw_post_email_notify($name= NULL,$email= NULL) {\r\n\t$to = get_bloginfo('admin_email');\r\n\t$blogname = get_bloginfo('blogname');\r\n\t$frommail = $to;\r\n\t\r\n\tif ( ! isset($email) ) return false;\r\n\t\r\n\t$headers = sprintf( \"From: %s &lt;%s&gt;\\r\\n\", $blogname, $frommail );\r\n\t$subject = \"Someone has viewed the protected post\";\r\n\t$msg = \"Name: \" .$name. \" -  Email: \".$email;\t\r\n\t$mail_result = wp_mail( $to, $subject, $msg, $headers );\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to add name email or any additional fields in addition to password for protected posts, see the steps below: \/****************** custom protected post form *****\/ add_filter( &#8216;the_password_form&#8217;, &#8216;custom_password_form&#8217; ); function custom_password_form() { global $post; $label = &#8216;pwbox-&#8216;.( empty( $post-&gt;ID ) ? rand() : $post-&gt;ID ); $a = &#8216;&lt;div class=&#8221;clearfix&#8221;&gt;&lt;form id=&#8221;password_form&#8221; class=&#8221;protected-post-form&#8221; action=&#8221;&#8216;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1,2],"tags":[41,40],"class_list":["post-147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web","category-wordpress","tag-extend","tag-protected-post"],"acf":[],"_links":{"self":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}