making your posts password-protected by default - 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.

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

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

Related

How to update post author after update post

I'd like to update post_author after update post by getting current user ID, but when I use this function wordpress created new post with correct author by copied current post
function change_author () {
if ( ! wp_is_post_revision( $post_id ) ){
$post= array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
wp_update_post( $post );
}
}
add_action('save_post', 'change_author');
I'd use the wp_insert_post_data hook instead of save_post. Rather than updating the post after it's saved, it's probably better to change the data before it gets inserted into the database as part of the save post action. Here's what I'd do:
function change_author ( $data ) {
if ( ! wp_is_post_revision( $data['ID'] ) ){
$data['post_author'] = get_current_user_id();
}
return $data;
}
add_filter( 'wp_insert_post_data', 'change_author', 10, 1 );

Wordpress - Make it so that non-admin users can't see posts with a specific custom post status

What hooks would I use for the functions file in order to make it so that all non-admin users can't see all posts with a specific custom post_status in the wp-admin back-end. BUT it is still able to be queried and looped through the WordPress post loop?
With pre_get_posts you should be able to get started (to hide posts from the admin screen). You may also want to check the post type, etc.
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
To disallow users to edit posts with that specific status, you should do:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);

Disable visual editor based on role?

I want to disable visual editor form 'author' role form my wordpress site.
Author only see text editor when write a post.
You can use the user_can_richedit filter to tell WP to disable rich editor for author only through this code (put this inside your functions.php file):
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
add_filter( 'user_can_richedit' , '__return_false', 50 );
}
I make following function it's work.
function cp_disable_editor()
{
if( current_user_can( 'author' ) )
{
$user_ID = get_current_user_id();
update_user_meta( $user_ID, 'rich_editing', false );
}
}
add_action('admin_init','cp_disable_editor');

Wordpress update button works but not publish button

I asked the programmer to have the title of every post automatically be the date selected and the place selected (to allow users to save some time). When I click 'publish' the title of the post is the date twice (ie; 03-26-15 03-26-15). Then the 'publish' button turns into an 'update' button. And then when I click 'update' the title will be correct (ie; 03-26-15 London). I'm trying to figure out how to get it this way after clicking 'publish' the first time. The programmer disappeared and I can't figure it out. Any help would be great.
function post_updated( $post_id ) {
global $post_type;
if ($post_type == 'place') {
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$postTitle.=get_the_date( 'm/d/Y', $post_id );
$postTitle.=" ".get_the_title(get_post_meta($post_id, 'place_locale', true) ) ;
$post['ID'] = $post_id;
$post['post_title'] = $postTitle;
remove_action( 'save_post', 'post_updated' );
wp_update_post($post);
add_action( 'save_post', 'post_updated' );
}
}
}
add_action( 'save_post', 'post_updated' );
The .= operator appends, therefor your titles are being duplicated.
Simply change it to = and you should be fine.
Are you sure this is the only function? It seems to only have "update post" code.
Post creation is handled with wp_insert_post something like:
$my_post = array(
'post_title' => $title,
'post_content' => "",
'post_status' => "pending",
'post_author' => wp_get_current_user()->ID
);
$post_id = wp_insert_post( $my_post );
Old question, but this may help who got into this question:
There are some problems with the code:
global $post_type is not defined.
As mentioned by #dojs, you should use = operator instead of .= to define the variable first.
get_the_title() function only accepts post_id or post object as the param.
Since you're dealing with a custom post type of place, it's better to use a post type specific hook instead of the general and crowded save_post hook.
After these modifications, the final code could be something like this:
function tst_post_updated( $post_id )
{
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$post_title = get_the_date( 'm/d/Y', $post_id );
$post_title .= " " . get_post_meta($post_id, 'place_locale', true);
$post_title = sanitize_title($post_title);
$post['ID'] = $post_id;
$post['post_title'] = $post_title;
remove_action( 'save_post_place', 'tst_post_updated' );
wp_update_post($post);
add_action( 'save_post_place', 'tst_post_updated' );
}
}
add_action( 'save_post_place', 'tst_post_updated' );

can i call function in my wordpress plugin when sending publish post in every plugin or user?

i want to call my function when user or admin or another plugin sending publish post.
any hook exist for this ?
i try some hook like publish_post.
but this work just if i put my code in that plugin which sending post.
i want my plugin Independent and when sending post in any plugin or admin or every where .... , my function in my plugin do something.
this is for SEO reason and its hard to say why i want do this.
some code i write
function post_published_notification($post) {
$ID= $post->ID;
$my_post = array(
'ID' => $ID,
'post_title' => "anything"
);
wp_update_post( $my_post );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
There's are hook called save_post - do_action( 'save_post', $post_ID, $post, $update );. You could check if $post->post_status is set to published and maybe keep your own record of whether it's had the published status before.
Example on how to use it:
function post_published_notification( $post_ID, $post, $update ) {
if ( $post->post_status == 'publish' && ! get_post_meta( $post_ID, 'published_notification_set', true ) ) {
update_post_meta( $post_ID, 'published_notification_set', 1 );
}
}
add_action( 'save_post', 'post_published_notification', 10, 3 );

Resources