0

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;
5
  • 1
    You have listed 3 lines of PHP code, not error messages. What are the error messages you are receiving? Sep 7, 2013 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 Sep 7, 2013 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. Sep 7, 2013 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. Sep 7, 2013 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. Sep 7, 2013 at 14:07

2 Answers 2

1

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;
}
0
-1

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

require (ABSPATH . WPINC . '/pluggable.php');
4
  • This will break all other plugins which are trying to predefine pluggable functions. Never ever do that.
    – fuxia
    Dec 26, 2013 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!
    – Dr.Hariri
    Dec 26, 2013 at 1:05
  • 1
    Please ignore the Codex. There is a lot of misinformation.
    – fuxia
    Dec 26, 2013 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 :(.
    – Dr.Hariri
    Dec 26, 2013 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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