• I was trying to create a custom role using this line of code. But the problem was when I created the role it had no name. Then when I go to edit it I get directed to an error page saying “The requested role to edit does not exist.” I am unable to delete the role. I just want to get rid of the role I created. I was able to get the code to run write but this code gave me the broken role. I am using a form plugin that fill out custom field from the front end so when you hit submit it runs the code below.

    
    function wpufe_update_post_price( $post_id ) 
    {
    	$post_title = get_post_meta( $post_id, 'registration_name', true );
    	$role = get_post_meta( $post_id, 'registration_name', true );
    	$email = get_post_meta( $post_id, 'email', true );
    	$m_password = get_post_meta( $post_id, 'manager_password', true );
    	$s_password = get_post_meta( $post_id, 'server_password', true );
    	$k_password = get_post_meta( $post_id, 'kitchen_password', true );
    	add_role( $role, 'test', get_role( 'restaurant_template' )->capabilities );
    	wp_create_user( 'manager', $m_password);
    	wp_create_user( 'server', $s_password, $email);
    	wp_create_user( 'kitchen', $k_password, $email);
    	$my_args = array
    		(
                'ID'           => $post_id,
                'post_title'   => $post_title,
            );
    	wp_update_post( $my_args );
    }
    
    add_action( 'wpuf_add_post_after_insert', 'wpufe_update_post_price' );
    
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure it’s possible to create a role with no name. Its data may be corrupted such that the related UI does not function correctly, but AFAIK there has to be a name. Anyway, you could use some custom PHP code to get the “wp_user_roles” option. Var_dump the returned array and determine the array key used for the offending role (the role’s actual name slug). Unset the offending array element, then save the remaining array back to the option.

    I recommend backing up the options table, or at least that one option before making such a low level change. Don’t attempt to directly alter the option value through phpMyAdmin, the data structure used is generally too finicky to allow for successful manual editing.

    A bit off topic, but it doesn’t seem correct to create specifically named roles and users every time the “wpufe_update_post_price” fires. Addition of the same role and users will be attempted any time any post’s price is updated, when the addition only needs to be done once. Adding persistent data like roles and users is typically done only once when the related plugin is activated. Even then, it’s good practice to verify such role and users do not exist before trying to add them.

    Thread Starter lex713

    (@lex713)

    @bcworkz so I should make a custom code that gets an array of all the roles finds the ones I want to delete and delete them thru custom code. Also the code above isn’t finished for each role or user it will be name manager, name sever, etc.

    Moderator bcworkz

    (@bcworkz)

    That’s what I suggest. The code ought to be somewhere you can manually control. I like to use a custom page template tied to a single page for things like that. Name the template file following the formats 1-3 outlined in the Theme Handbook. You can start with a copy of your theme’s general singular templates (files 4-6 in the above link). Make it a private page to keep search bots from trying to use it.

    I figured it is a work in progress, that’s fine, but I still question if you’d want to create new roles and users every time a post’s price is updated, regardless of their names. If that’s really what you want, so be it, it’s your site, not mine ??

    Thread Starter lex713

    (@lex713)

    @bcworkz So i got it working and deleted the roles I wanted. Then I went back edited the code a little and most of the time I get a role that has a display name but still sometimes when I use the code below I still get a blank display name. I am also unable to delete the new roles I made.

    
    $role = get_post_meta( $post_id, 'registration_name', true );
    $name = get_post_meta( $post_id, 'registration_name', true );
    $name = ucfirst($name);
    $role .= '_role';
    $role = lcfirst($role);
    	
    add_role( $role, __( $name ), get_role ( 'restaurant_template' )->capabilities );
    

    As before I solved the deleting of the old roles using

    
    $roles = wp_roles()->get_names();
    
    foreach( $roles as $role ) {
    if($role = 'role to delete')
        echo translate_user_role( $role );
        remove_role($role);
    }
    

    But I cant get it to work this time. The roles will not delete anymore. I have tried replacing remove_role($role) with remove_role(‘role to delete’) but it will not work. I have no idea what happened but all I called to create a role was add role.

    Thread Starter lex713

    (@lex713)

    @bcworkz i figured out that if I create a form and have the restaurant name be something that has a space in it like “big boy” then it won’t work as a display name. If it is “bigboy” it will work. I don’t know why. But I tried to add the code below to get rid of spaces but still leaves me with a blank role name.

    
    $name = str_replace(' ', '', $name);
    
    Moderator bcworkz

    (@bcworkz)

    What does translate_user_role() do? I think it’s the source of your trouble. This cannot work: __( $name ). The translation scheme only works on fixed strings, not variables. And the translations are pre-compiled, you cannot translate on the fly like I think you are trying to do.

    Thread Starter lex713

    (@lex713)

    @bcworkz so translate_user_role is only called to echo the name. I don’t believe it does anything to the variable. But I’ll delete it and try. I’m also going to re write it as add_role( $role, ( $name ), get_role ( 'restaurant_template' )->capabilities );

    Thread Starter lex713

    (@lex713)

    @bcworkz Doesn’t delete the roles. I don’t know what happened to create these broken rules.

    
    function delete_role(){
    	
    $roles = wp_roles()->get_names();
    
    foreach( $roles as $role ) 
    	{
    		if ($role = 'test') 
    		{
    			  	remove_role($role);	
    		}
    	}
    }
    add_action('init', 'delete_role');
    
    • This reply was modified 4 years, 2 months ago by lex713.
    Moderator bcworkz

    (@bcworkz)

    Maybe there’s an odd character in the name that’s causing the usual removal method to fail? IDK. Try the low level approach I initially suggested:

    …get the “wp_user_roles” option. Var_dump the returned array and determine the array key used for the offending role (the role’s actual name slug). Unset the offending array element, then save the remaining array back to the option.

    If you want to reset all roles back to default, you could get a copy of the option value from a clean installation and copy it to your site through two instances of phpMyAdmin. One for the clean site and one for your site.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom Role Broken’ is closed to new replies.