0

I'm using a plugin Profile Builder (http://wordpress.org/plugins/profile-builder/) to manage my front-end registration. It works fine, and it warns when a user with a certain username already exists. That would be the usual scenario that you want...

BUT I want to modifiy the action to - that when a user already exists - I want to UPDATE some things for that user instead of inserting a new user. How could I achieve that without having to modify the plugin?

What I'm really asking if there are any specific hook or filter that I can use for this purpose that is in the core of Wordpress? (or is it only plugin-specific)

Code-snippet: (save_user_fields is only a function that handles saving of specific custom fields. The rest is taken care of by the plugin Profile Builder)

function save_user_fields($user_id) {
   if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

    //Save some extra fields when adding new user (frontend-registration)
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser'){        
    if (isset($_POST['adress'])) {
    $adress = $_POST['adress'];
    update_user_meta($user_id, 'adress', $adress);                
    }    
    if (isset($_POST['homephone'])) {
    $homephone = $_POST['homephone'];
    update_user_meta($user_id, 'homephone', $homephone);                
    } 
    if (isset($_POST['cellphone'])) {
    $cellphone = $_POST['cellphone'];
    update_user_meta($user_id, 'cellphone', $cellphone);
    }
    if (isset($_POST['postnr'])) {
    $postnr = $_POST['postnr'];
    update_user_meta($user_id, 'postnr', $postnr);
    }
    if (isset($_POST['ort'])) {
    $ort = $_POST['ort'];
    update_user_meta($user_id, 'ort', $ort);
    }     
    if (isset($_POST['description'])) {
    $description = $_POST['description'];
    update_user_meta($user_id, 'description', $description);
    }           
}

//Hooks for user (Update/add)
add_action('personal_options_update', 'save_user_fields');
add_action('edit_user_profile_update', 'save_user_fields');
add_action('user_register', 'save_user_fields'); //When adding new users


/* Profile Builder EXTRA Fields front-registration */
add_filter('extraRegistrationField', 'list_courses');

//List courses for registration
function list_courses($user_id) {    
    //Get all extra user-fields and return content output
    $current_user = new WP_User($user_id);
    $content = get_user_fields($current_user, true, '');    

    $content .= '<p class="form-homephonee"><label for="homephone">Hemtelefon</label><input id="homephone" class="regular-text" name="homephone" value="" type="text"></p>';        
    $content .= '<p class="form-cellhpone"><label for="cellphone">Mobil</label><input id="cellphone" class="regular-text" name="cellphone" value="" type="text"></p>';
    $content .= '<p class="form-adress"><label for="adress">Adress</label><input type="text" name="adress" id="adress" value="" class="regular-text" /></p>';
    $content .= '<p class="form-postnr"><label for="postnr">Postnr</label><input id="postnr" class="regular-text" name="postnr" value="" type="text"></p>';
    $content .= '<p class="form-ort"><label for="ort">Ort</label><input id="ort" class="regular-text" name="ort" value="" type="text"></p>';
    $content .= '<p class="form-description"><label for="description">Övrigt</label><textarea name="description" id="description" class="description"></textarea></p>';

    return $content;
}

When posting this code I thought it was all clear to me, but no...

I thought I could use http://codex.wordpress.org/Function_Reference/username_exists before saving the actual fields in the function save_user_fields but it didn't work, because the function save_user_fields() wasn't called before the actual error-handling was done by the plugin.

I've discovered that with this code:

function save_user_fields($user_id) {
    if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

    die("wordpress is ded for now");
....

The code didn't "die" and WP returned "User with username already exists"...

I've also tried with several hooks... (before the hooks created above)

function myplugin_check_fields() {
    echo 'hello';
    die();
}
add_action('register_post', 'myplugin_check_fields');

No death here either ;-)

I tried with hook "register_post", "register_form", but I can't figure out what to use? I basically want a hook to execute before the plugin does and when the form is submitted. register_post seemed the most plausible hook to use.

  • As-written, this question is specific to the Plugin code, and not specific to WordPress. Please edit your question to include the relevant code that you want to modify. – Chip Bennett Nov 18 '13 at 19:35
  • Ok, I see your point. I'm actually asking if there are any specific hook or filter that I can use for this purpose that is in core WP – bestprogrammerintheworld Nov 18 '13 at 19:40
  • We would still need to see the code in question. It's presumably using some sort of hook. It may be the right hook, and just needs a different callback, or it may be the incorrect hook entirely. – Chip Bennett Nov 18 '13 at 19:48
  • Ok, wait a moment and I will try to supply the relevant code... – bestprogrammerintheworld Nov 18 '13 at 19:53
  • 1
    I've think I've figured it out now. Sometimes it help to just post a quesiton... I could use codex.wordpress.org/Function_Reference/username_exists before saving the actual fields in the function save_user_fields (I think?) – bestprogrammerintheworld Nov 18 '13 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.