我正在使用受密码保护的页面,使用 WordPress,可见性选项,受密码保护。这是一个通用的密码。但我有一份请求表,供人们请求查看受保护的页面。
对于请求表,我使用“contact form7”插件。这样我们就可以自动回复用户这是您的密码。
但我还需要密码在一天内过期。当人们需要查看该页面时,密码过期后,他们必须请求新密码。
我已尝试以下代码来获取具有到期时间的唯一密码。
add_action( 'wpcf7_mail_sent', 'custom_generate_password', 10, 1 );
function custom_generate_password( $contact_form ) {
// Get the ID of the contact form that triggered the action hook
$form_id = $contact_form->id();
// Check if the contact form ID matches the one you want to use
if ( $form_id == 123 ) { // Replace 123 with the ID of your contact form
// Get the submitted form data
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
// Get the user's email address from the form submission
$user_email = $posted_data['your-email'];
// Generate a unique password
$new_password = wp_generate_password( 12, false );
// Set the expiry time for the password (30 minutes from now)
$expiry_time = time() + ( 30 * 60 );
// Save the password and expiry time to the user's session
session_start();
$_SESSION['password'] = $new_password;
$_SESSION['expiry_time'] = $expiry_time;
// Get the URL of the password-protected page
$page_url = get_permalink( $page_id );
// Create the email message
$to = $user_email;
$subject = 'Your password for the protected page';
$message = 'Your password is: ' . $new_password . '\n\n';
$message .= 'This password will expire at: ' . date( 'Y-m-d H:i:s', $expiry_time ) . '\n\n';
$message .= 'To access the protected page, please go to: ' . $page_url;
// Send the email
wp_mail( $to, $subject, $message );
}
}
}
它正在工作,我的意思是每次我提交表单时它都会给出带有到期日期的密码。
但是相同的密码在 WordPress 受保护的页面上不起作用。
我也尝试了很多插件,密码保护,PPWP,WP Members,限制用户访问,简单会员等,但仍然没有得到解决。