I need to create a post after user registration with some roles in wordpress !
i used this code
add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$this_user_role = implode(', ', $user_roles );
if ($this_user_role == 'candidate') {
// Create a new post
$user_post = array(
'post_title' => $user_info->username,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'resume', // <- change to your cpt
'post_author' => '$Post_id',
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
but dosn't work ! :-(
anybody can help me ?
try this:
add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$has_candidate_role = in_array('candidate', $user_roles);
if ($has_candidate_role) {
// Create a new post
$user_post = array(
'post_title' => $user_info->username,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'resume', // <- change to your cpt
'post_author' => $user_id,
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
i can create the post with this code [after user registration]:
add_action('user_register', 'resume_create', 10, 1);
function resume_create($user_id)
{
// Get user info
$user_meta = get_userdata($user_name);
// Create a new post
$user_post = [
'post_title' => $_POST['slug'],
'post_type' => 'resume',
'post_status' => 'publish',
'post_author' => $post_ID,
];
// Insert the post into the database
$post_id = wp_insert_post($user_post);
}
But, not assign to curent user and special role.
Related
I have created a custom post similar to WordPress posts that I want to delete automatically every time (for example, every day or every week).
Is there a function for this?
I know that you can delete trash posts with the following function
define('EMPTY_TRASH_DAYS', 10 );
But what about custom created posts?
Thanks for your help
I think, this code will full fill your requirements.
// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';
// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
// Get all published posts of the custom post type
$current_date = current_time('Y-m-d');
$yesterday_date = date('Y-m-d', strtotime('-1 day', strtotime($current_date)));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
'after' => $yesterday_date,
'before' => $current_date,
'inclusive' => true
)
);
$my_posts = get_posts( $args );
// Loop through the posts and delete them
foreach ( $my_posts as $my_post ) {
wp_delete_post( $my_post->ID, true );
}
}
// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
wp_schedule_event( time(), 'twicedaily', 'delete_custom_post_type_posts_event' );
}
// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );```
You can delete published posts every week by setting up a wp_schedule_event
Hope this code helps
// Define the custom post type that you want to delete automatically
$custom_post_type = 'your_custom_post_type';
// Function to delete the custom post type posts
function delete_custom_post_type_posts() {
// Get all published posts of the custom post type
$current_date = current_time('Y-m-d');
$last_week_date = date('Y-m-d', strtotime('-1 week', strtotime($current_date)));
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
'after' => $last_week_date,
'before' => $current_date,
'inclusive' => true
)
);
$posts = get_posts( $args );
// Loop through the posts and delete them
foreach ( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
}
// Schedule the event to run every week
if ( ! wp_next_scheduled( 'delete_custom_post_type_posts_event' ) ) {
wp_schedule_event( time(), 'weekly', 'delete_custom_post_type_posts_event' );
}
// Hook the function to the scheduled event
add_action( 'delete_custom_post_type_posts_event', 'delete_custom_post_type_posts' );
here's my code to submit post from frontend using ACF, all of this are working fine in desktop but when I tried to post using phone the image appears in media library but not in CPT, does anyone here have any idea? thanks
$user_info = get_userdata(get_current_user_id());
$args = array(
'post_id' => 'new_post',
'field_groups' => array(570), //it's the ID of your custom field Form
'form' => false,
'new_post' => array(
'post_type' => 'team_member',
'post_status' => 'pending',
'post_title' => $user_info->user_login),
);
acf_form( $args );
Function.php
function my_pre_save_post( $post_id ) {
// bail early if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
$my_post = array(
'post_type' => 'team_member'
);
// insert the post
$post_id = wp_insert_post( $my_post );
}
I have the following working code to create a new CPT entry when users register, but now I need this to happen only if the user has a subscriber role. This registration is processed by WooCommerce. I have another form thru Theme My Login for another users role.
$current_user = wp_get_current_user();
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$user_info = get_userdata($user_id);
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
if ($post_ID) {
// update ACF fields
update_field('field_5e0f8b80071a9', $user_info->first_name, $post_ID);
update_field('field_5e107dcfddc77', $user_info->user_email, $post_ID);
}
}
I have tried with
foreach( $user->roles as $role ) {
if ( $role === 'subscriber' ) {
// code here
}
}
and
if(in_array( 'subscriber', (array) $current_user->roles ) ) {
// code here
}
with no success.
How can I detect the user role and only if it is subscriber have the wp_insert_post ?
UPDATE:
the actual function, still not working:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$user_info = get_userdata($user_id);
$user_roles = $user_info->roles; // array with the user's roles
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) {
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
if ($post_ID) {
// inserisco campi ACF
update_field('field_5e0f8b80071a9', $user_info->first_name, $post_ID);
update_field('field_5e107dcfddc77', $user_info->user_email, $post_ID);
}
}
}
If I comment if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) it works fine
I finally solved it by using 'woocommerce_checkout_subscription_created' instead of 'user_register'
You are saving the user info inside of the variable $user_info. In your foreach you are using the variable $user and in your if clause you are using $current_user. I think this is the reason it is not working.
I would suggest saving the roles of a user inside of a variable and use the in_array function. Your code can look like this:
...
$user_info = get_userdata($user_id);
$user_roles = $user_info->roles; // array with the user's roles
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) {
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
}
...
I am trying to copy certain fields from a events custom post type to a standard blog post whenever an event is created or edited. I now have it working to the point that when an event is updated or edited it will create a blog post but if I am adding a brand new event it adds 2 blog post before adding the correct post with content. The first post it creates is auto draft then it creates one with the title and author but no content. The 3rd post it creates is correct though. How can I prevent it from creating these extra post when I add a new event?
This is my code so far:
function create_event_post( $post_id ) {
// If this is just a revision, don't create a new post.
if ( wp_is_post_revision( $post_id ) )
return;
// Set the title, thumbnail id, author, and content variables
$post_title = get_the_title( $post_id );
$post_type = get_post_type($post_id);
$post_content = get_post_field('post_content', $post_id);
$thumbnail_id = get_post_thumbnail_id( $post_id );
$author_id = get_post_field ('post_author', $post_id);
$author_name = get_the_author_meta( 'display_name' , $author_id );
// Set the variables for the new post to the same as the event post
$new_post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'post'
);
// If the post is not "tribe_events", don't create a new post.
if ( "tribe_events" != $post_type )
return;
// Create the new post and retrieve the id of the new post
$new_post_id = wp_insert_post ( $new_post );
// Set the featured image for the new post to the same image as event post
set_post_thumbnail( $new_post_id, $thumbnail_id );
}
add_action( 'save_post', 'create_event_post' );
Got it working. Here is the final code if anyone wants to see how it works.
add_action( 'save_post', 'create_event_post' );
function create_event_post( $post_id ) {
// Set the title, thumbnail id, author, and content variables
$post_title = get_the_title( $post_id );
$post_type = get_post_type($post_id);
$post_content = get_post_field('post_content', $post_id);
$thumbnail_id = get_post_thumbnail_id( $post_id );
$author_id = get_post_field ('post_author', $post_id);
// If the post is not "tribe_events", don't create a new post.
if ( "tribe_events" != $post_type )
return;
$new_post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'post'
);
remove_action( 'save_post', 'create_event_post' );
$post_exists = get_page_by_title( $post_title, $output, "post" );
if ( !empty($post_exists) ) {
// Update post
$update_post = array(
'ID' => $post_exists->ID,
'post_title' => $post_title,
'post_content' => $post_content,
);
// Update the post into the database
wp_update_post( $update_post );
set_post_thumbnail( $post_exists->ID, $thumbnail_id );
}
else {
// Create the new post and retrieve the id of the new post
$new_post_id = wp_insert_post ( $new_post );
// Set the featured image for the new post to the same image as event post
set_post_thumbnail( $new_post_id, $thumbnail_id );
}
// Now hook the action
add_action( 'save_post', 'create_event_post' );
}
I have developed a small wordpress application where It has Institutes(wp user), Trainers(wp user), Trainees(wp user), courses(custom post) and notifications(custom post). All the application is working fine but If I delete an institute all the information belongs to that institute like Trainers, Trainee, notifications & courses should also be deleted. While I was creating the 'institute' user I am storing the all the related info like first name, last name in wordpress table 'wp_users' and 'institute name' is storing as 'user meta' with key 'inistitute_name' in 'wp_usermeta' table like below is my code:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $user_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'admin'//get_option('default_option')
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$insititute_name );
While creating Trainer (or) Trainee my code is like below:
$user_data = array(
'ID' => '',
'user_pass' => '',
'user_login' => $first_name,
'user_email' => $trainer_email,
'first_name' => $first_name,
'last_name' => $last_name,
'role' => 'trainer'
);
$random_password = wp_generate_password(8,false);
$user_id = wp_insert_user( $user_data );
update_user_meta( $user_id, 'inistitute_name',$this->institute_name[0] );
wp_set_password($random_password, $user_id);
While creating courses my code is like below:
$user_ID = get_current_user_id();
$institute_name = get_user_meta($user_ID, 'inistitute_name', true);
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_type' => "courses"
);
$id = wp_insert_post($post);
update_post_meta( $id, 'inistitute_name', $institute_name );
Suppose let us say if a institute 'abcd' is deleted then all the information like 'trainer', 'trainee', 'courses' & 'notifications' associated with that institute should also be deleted based on a 'user_meta' field. Is it possible to delete? (or) did I do any mistake? can anyone tell me what was I doing wrong?
Try this code:
$user_args = array(
'meta_key' => 'inistitute_name',
'meta_value' => 'your_value', //<-- replace it by your user_meta value.
'meta_compare' => '=',
'orderby' => 'ID',
'number' => -1
);
// Custom query.
$user_query = new WP_User_Query($user_args);
// Get query results.
$users = $user_query->get_results();
//print_r($users);
// Check for users
if (!empty($users))
{
// Loop over users.
foreach ($users as $user)
{
$user_id = $user->ID;
// Get each user's data.
$user_info = get_userdata($user->ID);
$user_display_name = $user_info->display_name;
$args = array(
'numberposts' => -1,
'post_type' => 'any', //<-- you can Specific type by array('post', 'my_custom_post')
'author' => $user_id
);
// get all posts by this user: posts, pages, attachments, etc..
$user_posts = get_posts($args);
if (!empty($user_posts))
{
// delete all the user posts
foreach ($user_posts as $user_post)
{
wp_delete_post($user_post->ID, true);
}
}
wp_delete_user( $user_id, $reassign );
}
}
else
{
//"no user found"
}
I haven't tested this code, out form my WP knowledge.
Please note before testing this code do take a Database backup.
I got the solution form wordpress.stackexchange.com link to that question is here
https://wordpress.stackexchange.com/questions/249435/how-to-delete-all-users-and-posts-based-on-user-meta/