• I try to go my first steps with WordPress & php. I need to add a new user and if I do not try to wrap some parts in a function, all went fine and smooth. But in this version, no user is created. Would anyone please be so kind to point me the way?

    function create_terminal_user() {
            if ( !function_exists( 'wp_create_user' ) ) {
                require_once ABSPATH . WPINC . '/user.php'; //For wp_create_user and wp_delete_user() function
            }
            $user_data = array(
            'ID' => '', // automatically created
            'user_pass' => '',
            'user_login' => $terminal_name,
            'user_nicename' => $terminal_name,
            'user_email' => '',
            'display_name' => $terminal_name,
            'nickname' => $terminal_name,
            'first_name' => $location,
            'last_name' => '',
            'user_url' => '',
            'description'   =>  $description,
            'user_registered' => '', // leave empty for auto
            'role' => 'subscriber' // administrator, editor, subscriber, author, etc
            );
            $user_id = wp_insert_user( $user_data ); // finaly creat the user
        }
        
        function create_category() {
            if ( !function_exists( 'wp_insert_category' ) ) {
            }
            $cat_data = array(
                'cat_name' => $terminal_name,
                'category_description' => $description,
                'category_nicename' => '',
                'category_parent' => '',
                'taxonomy' => 'category'
            );
            $create_category = wp_insert_category($cat_data);
        }
        
        add_filter('frm_validate_entry', 'validate_my_form', 20, 2);
        function validate_my_form($errors, $values){
            if($values['form_id'] == 5){    //If we have the right form - ID 5 is our form ID for the setup
                $terminal_name = $values['item_meta'][21];  // ID 21 = entry with Terminal Name
                $description = $values['item_meta'][28];    // ID 27 = entry with description
                $location = $values['item_meta'][29];   // ID 29 = entry with location
                $terminal_name_old = FrmProEntriesController::get_field_value_shortcode(array('field_id' => 21, 'user_id' => 'current'));
        
        
                if ( username_exists( $terminal_name ) ) {  //check if name is free
                    $errors['my_error'] = $terminal_name.': registered';
                } else {
                    add_action( 'admin_init', 'create_terminal_user' );
                    add_action( 'admin_init', 'create_category' );
                }
        
                
                //$errors['my_error'] = $terminal_name_old;
            }
            return $errors;
        }

    I believe I did not understand the function() and some restriction – did I?
    Thank you, I am just on my way to learn 😉

    Michael

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, you are apparently unfamiliar with variable scope. You cannot generally assign a value to a variable in one function and be able to use that same variable’s value in a different function. All variables declared within a function are by default local in scope. They cannot be used outside of the function.

    You can enlarge their scope by declaring them global. In PHP, this must be done both where they are assigned and again where they are used. At the beginning of each function add a line similar to
    global $terminal_name, $location, $description;

    There are probably other variables, this is just an example. Global variable names like $location are a poor choice due to risk of name collisions with other code. It’s best to prefix names with some unique initials. I often use something like $bcw_location.

    Note that use of globals is considered poor practice by many coders. If you need a lot of globals, you likely have the wrong approach. Excessive use does start to eat up some memory. However, you’ll eventually notice that WP makes extensive use of globals.

    For the sake of simplicity, you may want to place all the data into an array structure. Assign the entire array to a single global variable.

    More reading: https://www.php.net/manual/en/language.variables.scope.php

    Thread Starter mickna

    (@mickna)

    Hello bcworkz,

    thank you! Normally I am aware of local and global variables. And to be honest, I didn’t see the problem till your line global $terminal_name, $location, $description;

    Perfekt! This helped me out. I don’t like global variables as you also said. so I go back to have all in one function and that’s it.

    Thank you,
    Michael

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Try to add new user in WordPress via function’ is closed to new replies.