$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
try that
//CyberPanel API {“status”: 0, “createStatus”: 0, “error_message”: “‘firstName’”}
This is the error Im getting.
Missing parameter firstName and lastName
...
$data = array(
"serverUserName"=> "admin",
"controller"=>"submitUserCreation",
"firstName"=>"Usman",
"lastName"=>"Nasir",
"email"=>[email protected]",
"userName"=>"usmannasir",
"password"=> "cyberpanel",
"websitesLimit"=>5,
"selectedACL"=> "user"
);
...
I see. So how do get those parameters, when there is not a form field for that? I must add the form field for it?
You can. Use
- https://woosuite.com/plugins/user-registration-for-woocommerce/ or
- WP User Frontend – Registration, User Profile, Membership, Content Restriction, User Directory, and Frontend Post Submission Plugin – WordPress plugin | WordPress.org or
something similar or your own plugin or just functions.php
Is there anyway to add code to make the first and last name go from like user ++?
You can also customize wc registration form programmatically but you will be responsible for updating it and ensuring there are no bugs or conflicts
<?php
// Display a first name in Registration
add_action( 'woocommerce_register_form_start', 'ql_display_account_registration_field' );
add_action( 'woocommerce_edit_account_form_start', 'ql_display_account_registration_field' );
function ql_display_account_registration_field() {
$user = wp_get_current_user();
$value = isset($_POST['billing_first_name']) ? esc_attr($_POST['billing_first_name']) : $user->billing_first_name;
?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_first_name"><?php _e( 'First Name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" maxlength="6" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php echo $value ?>" />
</p>
<div class="clear"></div>
<?php
}
// registration Field validation
add_filter( 'woocommerce_registration_errors', 'ql_account_registration_field_validation', 10, 3 );
function ql_account_registration_field_validation( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: first name is required!', 'woocommerce' ) ); }
return $errors;
}
// Save registration Field value
add_action( 'woocommerce_created_customer', 'ql_save_account_registration_field' );
function ql_save_account_registration_field( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
}
// Save Field value in Edit account
add_action( 'woocommerce_save_account_details', 'ql_save_my_account_billing_first_name', 10, 1 );
function ql_save_my_account_billing_first_name( $user_id ) {
if( isset( $_POST['billing_first_name'] ) ){
update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
}
// Display field in admin user billing fields section
add_filter( 'woocommerce_customer_meta_fields', 'ql_admin_user_custom_billing_field', 10, 1 );
function ql_admin_user_custom_billing_field(
$args ) { $args['billing']['fields']['billing_first_name'] = array(
'label' => __( 'first name', 'woocommerce' ),
'description' => '',
'custom_attributes' => array('maxlength' => 6),
);
return $args;
}
- billing_first_name
- billing_last_name
//CyberPanel API {“status”: 0, “error_message”: “Expecting ‘:’ delimiter: line 4 column 13 (char 79)”}
Is the error I ran into just now. I got the first name and last name user fields working.
JSON error it has a colon in array. change that to an arrow => on line 4
Gotcha okay thanks!
//CyberPanel API {“status”: 0, “createStatus”: 0, “error_message”: “Invalid email address.”}
“serverUserName”: “admin”,
“controller”: “submitUserCreation”,
“firstName”: “Firstname”,
“lastName”: “Lastname”,
“email”: “EmailAddress”,
“userName”: “Username”,
“password”: “Password”,
“websitesLimit”:1,
“selectedACL”: “user”
}',
I changed the variable name in the Email Address section to EmailAddress aswell.
Check if its a valid email address before submission
THe woocomerse plugin already checks if valid.
I added this just in case
<?php
function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if(!checkemail("[email protected]")){
echo "Invalid email address.";
}
else{
echo "Valid email address.";
}
?>
and its still throwing me the same error.
Show me how you get $str
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?php esc_html_e( 'EmailAddress', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
</p>
It worked. But its putting the name of the string in the script. How do I replace the strings with variables from input?
What do you mean?
If you want to place different values in your array you need to capture the following inputs:
- custom first name input
- custom second name input
- email input
- password input
e.g.
$firstName = $_POST['billing_first_name'];
then use it like this
...
$data = array(
"serverUserName"=> "admin",
"controller"=>"submitUserCreation",
"firstName"=>$firstName,
"lastName"=>"Nasir",
"email"=>[email protected]",
"userName"=>"usmannasir",
"password"=> "cyberpanel",
"websitesLimit"=>5,
"selectedACL"=> "user"
);
...
Those are the variables I asssume. So firstName. etc. I have tried
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://cp.supernovadatacentre.com/cloudAPI/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"serverUserName": "admin",
"controller": "submitUserCreation",
"firstName": 'firstName',
"lastName": 'lastname',
"email": '$email',
"userName": "Username",
"password": 'password',
"websitesLimit":1,
"selectedACL": "user"
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic secret'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
All of the above


