WordPress教程

WordPress文章密码保护Ajax设置

阿里云

WordPress 中对于文章有一个密码访问的功能,后台发布文章时填写密码即可设置。在 WordPress 中 Ajax 的应用越来越多,要做到密码文章查看无刷新应用,请看下面的代码:

  1. add_action('wp_ajax_do_post_password', 'do_x_post_password_cb');
  2. add_action('wp_ajax_nopriv_do_post_password', 'do_x_post_password_cb');
  3. function do_x_post_password_cb() {
  4.     require_once( ABSPATH . 'wp-includes/class-phpass.php' );
  5.     $wp_hasher = new PasswordHash(8, true);
  6.     // 10 天
  7.     setcookie( 'wp-postpass_' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $_POST['pass'] ) ), time() + 864000, COOKIEPATH );
  8.  
  9.     $_COOKIE['wp-postpass_' . COOKIEHASH] = $wp_hasher->HashPassword( stripslashes( $_POST['pass'] ) );
  10.     $q = new WP_Query( "p={$_POST['pid']}" );
  11.     if ( $q->have_posts() ) : while( $q->have_posts() ) : $q->the_post();
  12.         $error = false;
  13.         if ( post_password_required() ) {
  14.             $error = true;
  15.         }
  16.         ob_start();
  17.         echo '<a href="'; the_permalink(); echo '">';
  18.         the_title();
  19.         echo '</a>';
  20.         $title = ob_get_clean();
  21.         @ob_end_flush();
  22.         ob_start();
  23.         the_content();
  24.         $content = ob_get_clean();
  25.         @ob_end_flush();
  26.     endwhile; endif;
  27.     wp_reset_postdata();
  28.     $return = array( 'title' => $title, 'content' => $content, 'error' => '' );
  29.     if ($error)
  30.         $return['error'] = '密码不正确';
  31.     die( json_encode( $return ) );
  32. }
也想出现在这里?联系我们
创客主机

实现功能所必须的 php,加载到你的 functions.php 中。这里设置了 10 天的 cookie,输入正确密码后 10 天内不需要再次输入,可根据你自己的需要更改。

  1. jQuery(document).ready( function($) {
  2.     $('.post-password-required').on( 'submit', 'form[action$="postpass"]', function( ev ) {
  3.     ev.preventDefault();
  4.     var id = $(this).find('label').attr('for').replace('pwbox-', ''),
  5.         ajaxurl = barley.ajaxurl,
  6.         loading = '';
  7.         $(this).find('input[type="submit"]').css({
  8.             'background-image': 'url('+loading+')',
  9.             'background-position': '92% 50%',
  10.             'background-repeat': 'no-repeat',
  11.             'padding-right': '25px'
  12.         }).attr('disabled','disabled');
  13.         $.post( ajaxurl, {
  14.             action: 'do_post_password',
  15.             pass: $(this).find('input[name="post_password"]').val(),
  16.             pid: id
  17.         }, function( response ) {
  18.             if ( response.error != '' ) {
  19.                 response.content = '<p class="error" style="background:#fcc;padding:10px;">'+ response.error+'</p>' + response.content;
  20.             } else {
  21.                 $('#post-'+id).find('.posttitle').HTML( response.title );
  22.             }
  23.             $('#post-'+id).find('.postcontent').html( response.content );
  24.         }, 'json' );
  25.     });
  26. });

WordPress 文章密码保护 Ajax 设置

已有 365 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!