Skip to content

Instantly share code, notes, and snippets.

@frontycore
Created June 29, 2023 22:27
Show Gist options
  • Save frontycore/7655a25314a512bd911cfd9b89139c39 to your computer and use it in GitHub Desktop.
Save frontycore/7655a25314a512bd911cfd9b89139c39 to your computer and use it in GitHub Desktop.
Unlock WP post with fobidden wp-login.php due to security
<?php
/**
* Change the password form action URL.
*/
add_filter('the_password_form', function($form, $post) {
$url = add_query_arg('unlock', 'post', get_permalink($post->ID));
$form = preg_replace('/action="([^\"]+)"/', 'action="' . $url . '"', $form);
return $form;
}, 10, 2);
/**
* Process password to unlock the post.
* @see wp-login.php:722
*/
add_action('init', function() {
if (!isset($_GET['unlock']) || $_GET['unlock'] !== 'post') return;
if (!array_key_exists('post_password', $_POST)) {
wp_safe_redirect(wp_get_referer());
exit;
}
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash(8, true);
$expire = apply_filters('post_password_expires', time() + 10 * DAY_IN_SECONDS);
$referer = wp_get_referer();
if ($referer) $secure = ('https' === parse_url($referer, PHP_URL_SCHEME));
else $secure = false;
setcookie('wp-postpass_' . COOKIEHASH, $hasher->HashPassword(wp_unslash($_POST['post_password'])), $expire, COOKIEPATH, COOKIE_DOMAIN, $secure);
wp_safe_redirect(wp_get_referer());
exit;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment