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

发布于 2025-01-06 00:09:53 字数 350 浏览 0 评论 0原文

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

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

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

我知道有一个函数 我需要使用它,但我不确定如何实现它或在哪里实现。

I originally wanted to be able to password protect a category. At the very least I wanted it to be password-protected, but preferably a username and password login. Since I have been unsuccessful for days at finding a solution I have now resorted to using WordPress's built-in password protection for posts.

The issue I am having is I will be posting via e-mail and in order to have these posts password-protected I need to login to Wordpress and then manually select password-protected and enter a password in the dashboard.

I would like to be able to have all posts that appear in a specific category be password-protected with the same password by default. Eliminating having to log in to Wordpress and manually select password protect.

I know there is a function <?php post_password_required( $post ); ?> that I need to use but I am not sure how to implement it or where.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2025-01-13 00:09:53
    add_filter( 'wp_insert_post_data', function( $data, $postarr ){
    if ( 'book' == $data['post_type'] && 'auto-draft' == $data['post_status'] ) {
        $data['post_password'] = wp_generate_password();
    }
    return $data;
}, '99', 2 );
    add_filter( 'wp_insert_post_data', function( $data, $postarr ){
    if ( 'book' == $data['post_type'] && 'auto-draft' == $data['post_status'] ) {
        $data['post_password'] = wp_generate_password();
    }
    return $data;
}, '99', 2 );
习惯成性 2025-01-13 00:09:53

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

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' );
    }
}

Based on this WordPress StackExchange answer. Tested only with a regular dashboard. Publishing via email has to be tested, but I suppose the hook gets called in this kind of posting.

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' );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文