Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to write a plugin. I have added an extra field in a registration form using the plugin successfully. I would like to check whether the value in the extra field matches any username already registered.

When I try to validate using the predefined usermeta functions. I get the below errors:

require( 'C:\wamp\www\cpa\wp-load.php' )
require_once( 'C:\wamp\www\cpa\wp-config.php' )
require_once( 'C:\wamp\www\cpa\wp-settings.php' )

I am able to do this in a theme template. But I would like to know how to access all the predefined functions of WordPress inside a plugin like we do in theme files.

Code:

//1. Add a new form element...

$referral_username = $_REQUEST['referral'];

add_action('register_form','myplugin_register_form');
function myplugin_register_form (){
    $referral_username = ( isset( $_POST['referral_username'] ) ) ? $_POST['referral_username']: '';
    ?>
    <p>
        <label for="referral_username"><?php _e('Referral Username','mydomain') ?><br />
            <input type="text" name="referral_username" id="referral_username" class="input" size="20"value="<?php echo $referral_username = $_REQUEST['referral']; ?>" size="25" />



            </label>
    </p>
    <?php
}

//2. Add validation. In this case, we make sure referral_username is required.
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {

    if ( empty( $_POST['referral_username'] ) )
        $errors->add( 'referral_username_error', __('<strong>ERROR</strong>: You must include a Valid Referral User Name. Else leave it blank.','mydomain') );

    return $errors;
}

//3. Finally, save our extra registration user meta.
add_action('user_register', 'myplugin_user_register');
function myplugin_user_register ($user_id) {
    if ( isset( $_POST['referral_username'] ) )
        update_user_meta($user_id, 'referral_username', $_POST['referral_username']);
}


//Test Usermeta

$user = get_userdatabylogin('vijay');
echo $user->ID; // prints the id of the user;
share|improve this question
1  
You have listed 3 lines of PHP code, not error messages. What are the error messages you are receiving? –  Charles Clarkson Sep 7 '13 at 13:07
    
@CharlesClarkson Fatal error: Call to undefined function get_userdata() in C:\wamp\www\wordpress\wp-content\plugins\testplugin\testplugin.php on line 99 –  Vijay Rajasekaran Sep 7 '13 at 13:29
    
Code i am trying to add in the plugin file: $user = get_userdatabylogin('vijay'); echo $user->ID; // prints the id of the user; When i add the above code in the theme files, it works. –  Vijay Rajasekaran Sep 7 '13 at 13:31
    
We will need to see the code that is producing the error. If it is in a function, show the whole function. If it is not in a function show lines from about 80 to 110 so we can see the code that surrounds the error as well as the line that reports the error. Edit your question with the code. Do not add it in a comment. All lines of code have to be indented 4 spaces (or a tab) to display correctly. –  Charles Clarkson Sep 7 '13 at 13:34
    
@CharlesClarkson I have added the code in the question. Please look at the last two lines of code. I am trying to print the id of the user. It works when used in the theme files but not inside the plugin. Thanks. –  Vijay Rajasekaran Sep 7 '13 at 14:07
add comment

2 Answers

Please look at the last two lines of code. I am trying to print the id of the user. It works when used in the theme files but not inside the plugin.

//Test Usermeta

$user = get_userdatabylogin('vijay');
echo $user->ID; // prints the id of the user;

In PHP, you cannot (normally) execute a user function before it has been defined.

The plugin is executing the get_userdatabylogin() code before get_userdatabylogin() has been defined as a function.

The theme is executing the get_userdatabylogin() code after get_userdatabylogin() has been defined as a function.

Consult the Plugin API (or a WP Hooks database or search the WordPress code) to find the correct action name. Since you already know the code works in the theme, you should be able to use the after_setup_theme action to run your code.

add_action( 'after_setup_theme', 'test_user_meta' );
/**
 * Test User meta.
 */
function test_user_meta() {
    $user = get_userdatabylogin('vijay');
    echo $user->ID; // prints the id of the user;
}
share|improve this answer
    
Thank you so much. I will try it out. –  Vijay Rajasekaran Sep 7 '13 at 15:07
add comment

Try adding this to the top of the plugin file and re-test:

require (ABSPATH . WPINC . '/pluggable.php');
share|improve this answer
    
This will break all other plugins which are trying to predefine pluggable functions. Never ever do that. –  toscho Dec 26 '13 at 1:00
    
@toscho That's strange, I faced a similar issue with a plugin and after doing some research this is the solution that was suggested. I think it was on the Codex page too. So, it's not really the best way to do it! –  A.Hariri Dec 26 '13 at 1:05
    
Please ignore the Codex. There is a lot of misinformation. –  toscho Dec 26 '13 at 1:13
1  
@toscho Thanks for the alert! Too sad since it's the first reference for people like me with hardly any coding skills :(. –  A.Hariri Dec 26 '13 at 15:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.