wordpress - 默认情况下使您的帖子受密码保护

标签 wordpress function passwords categories

我最初希望能够用密码保护一个类别。至少我希望它受到密码保护,但最好是用户名和密码登录。由于我几天来都没有成功找到解决方案,所以我现在不得不使用 WordPress 的内置帖子密码保护。

我遇到的问题是我将通过电子邮件发帖,为了使这些帖子受密码保护,我需要登录 Wordpress,然后手动选择受密码保护并在仪表板中输入密码。

我希望能够默认使用相同的密码对特定类别中出现的所有帖子进行密码保护。无需登录 Wordpress 并手动选择密码保护。

我知道有一个函数<?php post_password_required( $post ); ?>我需要使用它,但我不确定如何实现它或在哪里实现。

最佳答案

基于this WordPress StackExchange answer 。仅使用常规仪表板进行测试。通过电子邮件发布必须经过测试,但我想在这种发布中会调用 Hook 。

add_action( 'save_post', 'wpse51363_save_post' );

function wpse51363_save_post( $post_id ) {

    //Check it's not an auto save routine
     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
          return;

    //Check it's not an auto save routine
     if ( wp_is_post_revision( $post_id ) ) 
          return;

    //Perform permission checks! For example:
    if ( !current_user_can( 'edit_post', $post_id ) ) 
          return;

    $term_list = wp_get_post_terms(
        $post_id, 
        'category', 
        array( 'fields' => 'slugs' ) 
    );

    if( in_array ( 'the-category-slug', $term_list ) )
    {
        // Unhook this function so it doesn't loop infinitely
        remove_action( 'save_post', 'wpse51363_save_post' );

        // Call wp_update_post update, which calls save_post again. 
        wp_update_post( array( 
            'ID' => $post_id,
            'post_password' => 'default-password' ) 
        );

        // Re-hook this function
        add_action( 'save_post', 'wpse51363_save_post' );
    }
}

关于wordpress - 默认情况下使您的帖子受密码保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9302876/

相关文章:

wordpress - 上传后无法访问analytics.txt,返回404

php - 如果打开时出现这样的php文件,如何解决?

c++ - 将数组传递给函数时遇到问题

bash - 我如何使用Expect通过ssh连接到系统并更改主机系统的密码?

ios - 如何保护html5音频链接免受下载

javascript - "console.log(phi(tableFor("花生牙", JOURNAL)))"有什么作用?

c - 如何将 "return"数组从函数到 c 中的 main

C# 通过 Process 的 StandardInput 传递密码

security - 在 Spring Security 3.1 中,StandardPasswordEncoder 最适合用于加盐密码的用途是什么?

javascript - WooCommerce:停止滚动到结帐页面上的错误消息