我最初希望能够用密码保护一个类别。至少我希望它受到密码保护,但最好是用户名和密码登录。由于我几天来都没有成功找到解决方案,所以我现在不得不使用 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/