WordPress文章密码保护Ajax设置

WordPress文章密码保护Ajax设置

作者 : 资源客 发布时间: 2019-11-24

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

add_action('wp_ajax_do_post_password', 'do_x_post_password_cb');
add_action('wp_ajax_nopriv_do_post_password', 'do_x_post_password_cb');
function do_x_post_password_cb() {
    require_once( ABSPATH . 'wp-includes/class-phpass.php' );
    $wp_hasher = new PasswordHash(8, true);
    // 10 天
    setcookie( 'wp-postpass_' . COOKIEHASH, $wp_hasher->HashPassword( stripslashes( $_POST['pass'] ) ), time() + 864000, COOKIEPATH );
 
    $_COOKIE['wp-postpass_' . COOKIEHASH] = $wp_hasher->HashPassword( stripslashes( $_POST['pass'] ) );
    $q = new WP_Query( "p={$_POST['pid']}" );
    if ( $q->have_posts() ) : while( $q->have_posts() ) : $q->the_post();
        $error = false;
        if ( post_password_required() ) {
            $error = true;
        }
        ob_start();
        echo '<a href="'; the_permalink(); echo '">';
        the_title();
        echo '</a>';
        $title = ob_get_clean();
        @ob_end_flush();
        ob_start();
        the_content();
        $content = ob_get_clean();
        @ob_end_flush();
    endwhile; endif;
    wp_reset_postdata();
    $return = array( 'title' => $title, 'content' => $content, 'error' => '' );
    if ($error)
        $return['error'] = '密码不正确';
    die( json_encode( $return ) );
}

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

jQuery(document).ready( function($) {
    $('.post-password-required').on( 'submit', 'form[action$="postpass"]', function( ev ) {
    ev.preventDefault();
    var id = $(this).find('label').attr('for').replace('pwbox-', ''),
        ajaxurl = barley.ajaxurl,
        loading = '';
        $(this).find('input[type="submit"]').css({
            'background-image': 'url('+loading+')',
            'background-position': '92% 50%',
            'background-repeat': 'no-repeat',
            'padding-right': '25px'
        }).attr('disabled','disabled');
        $.post( ajaxurl, {
            action: 'do_post_password',
            pass: $(this).find('input[name="post_password"]').val(),
            pid: id
        }, function( response ) {
            if ( response.error != '' ) {
                response.content = '<p class="error" style="background:#fcc;padding:10px;">'+ response.error+'</p>' + response.content;
            } else {
                $('#post-'+id).find('.posttitle').html( response.title );
            }
            $('#post-'+id).find('.postcontent').html( response.content );
        }, 'json' );
    });
});

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress文章密码保护Ajax设置