admin管理员组文章数量:1278985
Originally, I have it this way, every time a user signs up using the contact form 7 it will give the user a subscriber role allowing it to view the private page. The problem with this one is it keeps creating a new user under the users profile in the admin dashboard and emails the user their own Wordpress username and password and I don't want that. I just want a simple view allowing the users in the contact form 7 database to view that private page. I'm really out of options, can anyone suggest another alternative?
add_action ('admin_init','add_sub_caps');
function add_sub_caps() {
global $wp_roles;
$role = get_role('subscriber');
$role->add_cap('read_private_pages');
}
Originally, I have it this way, every time a user signs up using the contact form 7 it will give the user a subscriber role allowing it to view the private page. The problem with this one is it keeps creating a new user under the users profile in the admin dashboard and emails the user their own Wordpress username and password and I don't want that. I just want a simple view allowing the users in the contact form 7 database to view that private page. I'm really out of options, can anyone suggest another alternative?
add_action ('admin_init','add_sub_caps');
function add_sub_caps() {
global $wp_roles;
$role = get_role('subscriber');
$role->add_cap('read_private_pages');
}
Share
Improve this question
edited Apr 28, 2021 at 20:22
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Apr 28, 2021 at 17:59
NatzinNatzin
1
4
- If you don't want user accounts how do you want your users to sign in? Or is filling in the form the sign-in? i.e. get CF7 to set a cookie once they've signed up which grants access to the page? Or make it a password-protected page and email them a password with the form? (With a bit of work you could make this a unique per-user password.) Or stick with what you've got but filter out subscribers from the dashboard user view by default? – Rup Commented Apr 28, 2021 at 19:55
- Just filling in the form is enough, they don't need to sign in. I just want the specific page to be viewable only for users who filled in the form. The form actually redirects you to the that specific page. The subscribers are already filtered the problem is they have to take another step tologin in Wordpress just to access the private page. – Natzin Commented Apr 28, 2021 at 21:00
- You could make the form set an authentication cookie as well as creating the user and sending a password reset? Although then there's no need for your users to enter a real email address. – Rup Commented Apr 28, 2021 at 21:11
- If you need to give them access immediately after the form submission, and only that one time, then you could simply use a unique nonce which you test on the template_redirection hook. Else if you need to give them access beyond the initial submission session then you will need to implement @Rup cookie suggestion. – Aurovrata Commented May 2, 2021 at 16:43
1 Answer
Reset to default 0Sure, there is a way to solve this.
You need to save the submitted data into a transient and access it from the redirected page where you can confirm the visitor has actually filled in the form. No need for a private page, but rather use a custom page template to view your restricted page and check if the user has access rights at the top of the page template, else redirect them.
You can do this by,
Step 1: identify the current submission (this works even for non-logged in users.) using a WP nonce as a hidden field in your form, as well as place a js script to redirect to your page once a successful submission event is fired using an anonymous function hooked on the cf7 do_shortcode_tag
display filter,
add_filter('wpcf7_form_hidden_fields','add_hidden_nonce');
function add_hidden_nonce($fields){
$nonce = wp_create_nonce('cf7-redirect-id');
$fields['redirect_nonce'] = $nonce;
/*
hook the shortcode tag filter using an anonymous function
in order to use the same nonce and place a redirect js event
script at the end of the form.
*/
add_filter('do_shortcode_tag', function($output, $tag, $attrs) use ($nonce){
//check this is your form, assuming form id = 1, replace it with your id.
if($tag != "contact-form-7" || $attrs['id']!= 1) return $output;
$script = '<script>'.PHP_EOL;
$script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
//add your redirect page url with the nonce as an attribute.
$script .= ' location = "http://example/submitted/?cf7="'.$nonce.';'.PHP_EOL;
$script .= ' }'.PHP_EOL;
$script .= '</script>'.PHP_EOL;
return $output.PHP_EOL.$script;
},10,3);
return $fields;
}
Step 2: hook the cf7 action 'wpcf7_mail_sent' once the submission has passed the validation/and email is sent.
add_action('wpcf7_mail_sent', 'save_posted_data_into_transient');
function save_posted_data_into_transient(){
if(isset($_POST['redirect_nonce'])){ //save the data.
//save the posted data from the form. Note if you have submitted file fields you will also need to store the $_FILES array.
set_transient('_cf7_data_'.$_POST['redirect_nonce'], $_POST, 5*60); //5 min expiration.
}
}
Step 3: on your redirect custom page, you can now access your stored transient data, place this at the top of your custom page template,
<?php
if( isset($_GET['cf7']) ){
$transient = '_cf7_data_'.$_GET['cf7'];
$data = get_transient($transient);
//$data['my-text-field']....
}
?>
if the transient is missing, invalid, or the cf7 nonce attribute is missing, then simply redirect to a page informing the user they have to fill in the form to access the page.
本文标签: Is there a way for users only saved in the contact form 7 database to view a wordpress private page
版权声明:本文标题:Is there a way for users only saved in the contact form 7 database to view a wordpress private page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741302416a2371167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论