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);
}
...
Related
I'm using WC Vendors and have output the taxonomy named "location" on the products using wp_term_checklist per their suggestion. I've got it saving to the product, but it's not saving the checkbox selection on the front-end.
This is the code I've added to the product-edit.php template
$args = array(
'descendants_and_self' => 0,
'selected_cats' => false,
'popular_cats' => false,
'walker' => null,
'taxonomy' => 'location',
'checked_ontop' => false
);
wp_terms_checklist( $my_postid, $args );
$post_to_edit = array(
'ID' => $my_postid,
'tax_input' => array( 'location' => array($_POST['tax_input']['location']) )
);
$pid = wp_update_post($post_to_edit);
if ( isset($_POST['tax_input']['location']) && is_array( $_POST['tax_input']['location'] ) ) {
$location = array_map( 'intval', $_POST['tax_input']['location'] );
$location = array_unique( $location );
wp_set_post_terms($pid, $location, 'location');
}
This is their code where they use a multi-select, but we need checkboxes:
https://gist.github.com/digitalchild/128033d2d41f682acd4387b595d4f607
We have a custom terms checklist called wcv_terms_checklist() in our form helper. It didn't support custom taxonomies but I have modified it to support that now. You can use the following code from v1.7.10 and above to get working custom terms checklists on our front end.
// Output the location checklist
function form_location( $object_id ) {
$args = array(
'taxonomy' => 'location',
);
$field = array(
'id' => 'product_loc_list',
'label' => __( 'Location', 'wcvendors-pro' ),
'class' => 'product_cat_checklist',
);
WCVendors_Pro_Form_Helper::wcv_terms_checklist( $object_id, $args, $field );
}
add_action( 'wcv_after_product_details', 'form_location' );
// Save the terms
function save_location( $post_id ){
if ( isset( $_POST[ 'location' ] ) && is_array( $_POST[ 'location' ] ) ) {
$location = array_map( 'intval', $_POST[ 'location' ] );
$location = array_unique( $location );
wp_set_post_terms( $post_id, $location, 'location' );
}
}
add_action( 'wcv_save_product', 'save_location' );
Gist available here - https://gist.github.com/digitalchild/09e15649425845fef0b8d3af75c79dd1
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.
I want to retrieve the first topic in a lesson in learndash and redirect it through it. But for some reason am not sure how to do it.
I checked the API and doesn't see any appropriate filter/hook for it.
Here is my code:
function redirect_to_first_topic() {
// We only want to do this for Topics. But the below code can be adapted to work for Lessons
global $post;
$post_id = $post->ID;
if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
$progress = learndash_get_course_progress( null, $post_id );
$link = $progress['next'];
$parent_lesson_id = learndash_get_setting( $post, 'topic' );
$parent_lesson = get_post( $parent_lesson_id );
var_dump($parent_lesson);
}
// Always return $link
// return $link;
}
add_action('wp', 'redirect_to_first_topic');
So basically what am doing here is getting the parent which is the lesson.
A bit late, but mb it will help someone else. You can see how to get 1. lesson of a course and go deeper if needed fe. first topic of first lesson and so on :
add_action( 'template_redirect', 'course_overview_redirect' );
function course_overview_redirect() {
global $post;
if ( ! empty( $post ) && $post->post_type == 'sfwd-courses' && ! empty( $post->ID ) ) {
$lessons = learndash_get_course_lessons_list( $post->ID );
if ( ! empty( $lessons ) ) {
$lesson = array_shift( $lessons );
if ( ! empty( $lesson ) ) {
$url = get_permalink( $lesson[ "post" ]->ID );
if ( ! empty( $url ) ) {
wp_redirect( esc_url( $url ) ); // And redirect if needed
exit;
}
}
}
}
}
You can add a button like this;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sfwd-lessons', // you can change here to find topics : 'sfwd-topics'
'suppress_filters' => true,
'fields' => 'ids',
'orderby' => 'menu_order', // ordering by menu_order will show lesson list, in their order in course.
'order' => 'ASC',
// this meta query is used if you want to search a lesson under a course, or if you search for a topic which is in a course but not under a lesson
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $course_id, // this is id of your course
)
// if your topic is under a lesson than you should add lesson meta query
// your meta query should be changed like below
/*
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'course_id',
'value' => $course_id,
),
array(
'key' => 'lesson_id',
'value' => $lesson_id,
),
),
*/
),
);
$all_lessons = get_posts($args);
$first_lesson = $all_lessons[0]; // taking directly first element of array
$button_html = '' . __( 'Start the course', 'custom-text' ) . '';
for now i am making posts and products display as draft through it post and product will not display in frontend but is there a way to completely restirct user while he upload content.
add_action('init', 'make_editor_post_drafted');
function make_editor_post_drafted(){
$user = wp_get_current_user();
if ( in_array( 'editor', (array) $user->roles ) ) {
$user_ID = get_current_user_id();
$users_query = new WP_Query( array(
'post_type' => array('product','post'),
'meta_value' => $user_ID,
) );
if ( $users_query->have_posts() ):
while ( $users_query->have_posts() ): $users_query->the_post();
wp_update_post( array('ID' => get_the_id() , 'post_status' => 'draft' ));
endwhile;
endif;
} //end if
} //end function
I am trying to display the orders using the query->set() with my customer metadata. The metadata - _admin_name is stored in the wp_postmeta, and I want the admins only can view their own orders which the admin_name same with their user name.
How how can I use query->set() with my metadata?
Here is my code:
function mypo_parse_query_useronly( $wp_query ) {
global $post;
if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {
add_action( 'views_edit-post', 'child_remove_some_post_views' );
global $current_user;
$userName = $current_user->user_login;
$meta_query_args = array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
);
//trying use this code
// $query = new WP_Query( $meta_query_args );
$wp_query->set( 'author', $current_user->id );
}
}
add_filter('parse_query', 'mypo_parse_query_useronly' );
//don't display the order whish is not own by current user.
function child_remove_some_post_views( $views ) {
//header('Location: '.$newURL);
unset($views['all']);
unset($views['wc-processing']);
unset($views['wc-on-hold']);
unset($views['wc-completed']);
unset($views['pending']);
return $views;
}
Have so way to do this?
Thanks.
<?php
function mypo_parse_query_useronly( $wp_query ) {
global $post;
if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {
add_action( 'views_edit-post', 'child_remove_some_post_views' );
global $current_user;
$userName = $current_user->user_login;
$meta_query_args = array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
);
//trying use this code
// $query = new WP_Query( $meta_query_args );
$wp_query->set( 'author', $current_user->id );
$wp_query->set( 'meta_query', array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
));
}
}
add_filter('parse_query', 'mypo_parse_query_useronly' );
//don't display the order whish is not own by current user.
function child_remove_some_post_views( $views ) {
//header('Location: '.$newURL);
unset($views['all']);
unset($views['wc-processing']);
unset($views['wc-on-hold']);
unset($views['wc-completed']);
unset($views['pending']);
return $views;
}