How to delete taxonomy term when a wordpress user is removed? - wordpress

i need to Delete wp_terms -> name field when a user is removed .
my requirement is admin needs to assign posts to specific users, that is admin need to add a post to user1 ,but user2 should not see that.for this i created a custom post and add taxonomy for that.And the terms are users Usernames .so i need to list the usernames of the users as terms in the taxonomy.when a new user is registered his username should updated in the wp_terms table also,so i will get the usernames as terms. this is working well ,now i need to delete the term from taxonomy when the user is removed.
i stucked here .please suggest some solution for this
what i have done is
add_action( 'delete_user', 'yg_user_delete', 10, 1 );
function yg_user_delete( $user_id ) {
$user_info = get_userdata($user_id); $user_name = $user_info->user_login;
print_r($user_info);
wp_delete_term( $user_name, 'user1', array() );
}

There are a few issues with your code.
The main issue is wp_delete_term requires a term_id not a term_name.
You can use wp_get_object_terms to get the term_id for the user.
I haven't tested this code but it should point you in the right direction.
function delete_user_terms( $user_id ) {
$taxonomy = 'user1'; // Change this.
$terms = wp_get_object_terms( $user_id, $taxonomy ); // Get the terms for this user.
if ( is_wp_error( $terms ) ) {
return; // Taxonomy does not exist.
}
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy ); // This requires Term ID. Not term name.
}
}
add_action( 'delete_user', 'delete_user_terms', 10, 1 );

i got the solution
add_action( 'delete_user', 'yg_user_delete', 10, 1 );
function yg_user_delete( $user_id ) {
$user_info = get_userdata($user_id);
$user_name = $user_info->user_login;
wp_delete_term( get_term_by('name', $user_name, 'user1')->term_id, 'user1', array() );
}

Related

WooCommerce auto assign shipping class based on user role [WooCommerce Product Vendors]

I'm stuck on a task where I need to make WooCommerce automatically apply a shipping class to products uploaded by "Vendor Admins" user role (as set by WooCommerce Product Vendors). I tried to adapt a code snippet I fond here, but apparently this isn't for my usage case. I appreciate your help & comments!
`
if( isset($_GET['update_products']) && is_wc_product_vendors_admin_vendor() ){
add_action( 'init', 'add_shipping_terms_on_all_products' );
}
function add_shipping_terms_on_all_products(){
global $wpdb;
// Here you need to edit the slug_to_edit with your custom slug
$shipping_terms = get_term_by( 'slug', 'old-books', 'product_shipping_class' );
// If can't find the terms, return
if( empty($shipping_terms) ){
return;
}
// Request all product
$products = $wpdb->get_results( "
SELECT p.ID as ID
FROM wp_posts AS p
WHERE p.post_status = 'publish'
AND p.post_type = 'product'
" );
foreach($products as $_product){
$product = new WC_Product($_product->ID);
$product_shipping_class = $product->get_shipping_class();
if( !empty($product_shipping_class) ){
continue;
}
wp_set_post_terms( $product->id, array( $shipping_terms->term_id ), 'product_shipping_class' );
}
}
`
I tried to adapt the snippet and unfortunately nothing happened. I don't know how to proceed.

WooCommerce tag returning customer

In our store, during checkout for every customer we create account automatically if it doesn’t already exist with same email. Now I’m looking for a way to somehow display label tag or little notice on orders list if it comes from already existing customer (returning) because these orders are handled a little differently.
Does anybody have an idea how to do that?
Thanks in advance
this snippet adds a column to orders list page in woocommerce backend and checks if customer is returning or not
add_filter( 'manage_shop_order_posts_columns', 'shalior_wc_set_custom_edit_post_columns',99,1 );
function shalior_wc_set_custom_edit_post_columns($columns) {
$columns['is-returning'] = __( 'Is returning?', 'your_text_domain' );
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'shalior_wc_is_returning', 99, 2 );
function shalior_wc_is_returning( $column, $post_id ) {
switch ( $column ) {
case 'is-returning':
$order = new WC_Order( $post_id );
$user_id = $order->get_user_id();
$orders_count = wc_get_customer_order_count( $user_id );
echo $orders_count > 1 ? "Yes" : "NO" ;
break;
}
}

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

I want to create two new user type in WordPress

In WordPress, I want to create two register user type: 1.teacher and 2. student.. means register as teacher and register as a student.
teacher registration is free and it posts his video, text etc..
student registration is not free but it's 6-month and 12-month subscription when a student is subscribed for this then student show the teacher's all post.
Can you please suggest me in this how to create this type of registration and membership...
thanks in advance
I think you can do it using add_role function.
There are three parameters in add_role function.
add_role( $role, $display_name, $capabilities );
$role: Unique name of the role.
$display_name: The name to be displayed in WordPress Admin Panel.
$capabilities: Privileges that one can access.
Complete list of all capabilities can be found here.
Step 2 : add user roles dropdown in registration form
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
global $wp_roles;
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
}
//2. Add validation.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( '<strong>ERROR</strong>: You must include a role.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}
Finally How to check if a user is in a specific role ?
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//The user has the "author" role
}

making your posts password-protected by default

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

Resources