Get current user array with post string
-
Hey all,
I’m recoding an associative array that used to post 16 static data fields. I just need to get 10 WP current user fields and pass the field data to a post string.
This php code allows user registration data to be passed to a third-party content provider website, so the WP user doesn’t have to go through the whole user signup process twice.
I first need the correct coding for the get current user array and then define the correct values for the post string. The post string is expecting certain label names for each value and will not except WP label names.
Example – post string expects ‘firstname’ => ‘value’, not ‘user_firstname’ => ‘value’. After days of fatal parse errors for syntax, now I just get a fail status. Either my array code or post code, (or both), is somehow flawed. This is what I have:
global $current_user; Function get_currentuserinfo(){ $current_user-> $label; $n="user_variable_{$type}_name"; ${$n} = true; //Get values for these 10 user fields $query_vals = array( 'user_firstname' . $current_user->firstname =>'n', 'user_lastname' . $current_user->lastname =>'n', 'mepr-address-one' . $current_user->address =>'n', 'mepr-address-city' . $current_user->city =>'n', 'mepr-address-state' . $current_user->state =>'n', 'mepr-address-zip' . $current_user->zip =>'n', 'mepr-address-country' . $current_user->country =>'n', 'user_email' . $current_user->email =>'n', 'user_login' . $current_user->username =>'n', 'user_pass' . $current_user->password =>'n' ); } // Generate the POST string // These 1st 3 lines did work when posting static data $postdata = ''; foreach($query_vals as $key => $value) { $postdata .= $key.'='.urlencode($value).'&'; //These last two lines aren't posting my get user data foreach($query_vals as $key => $value) $postdata .= $key.'='.urlencode($n=$value).'&'; }Php Wizards please help!
-
Functionworks OK, but for consistency, usefunction(all lower case)
$current_user-> $label;doesn’t do anything, syntax works, but proper form is$current_user->label;, still doesn’t do anything.
In$n="user_variable_{$type}_name";,$typeis undefined?
${$n}, rather unorthodox, not used elsewhere so superfluous?
urlencode($n=$value), assigns $value to $n, but why? Otherwise no different thanurlencode($value)
The functionget_currentuserinfo()is never called, so what’s the point?
global $current_user;needs to be declared inside ofget_currentuserinfo()or the array keys will not be expanded. The function should return something so the array values can be assigned when the function returns.
The foreach loops are nested so you end up with 110 POST entries.Hey bcworkz,
Thanks for your comments. Hacking this code is the most difficult and time consuming php hack I’ve ever done. My code doesn’t even look like the post currently.
I’ve done several code edits based on suggestions from across the internet, and it still doesn’t work!
Let me show you and everyone what the original file looked like that I’ve tested, which works (every time) and produces a (Status: ok) result.
But keep in mind, the file in this form does nothing that automates any process. You must retype all of the values over and over again to register every new user.
The whole purpose of my edited file is to automate a process for the user, providing a convenience so they don’t have to register twice for two sites. Here is the static array version.
line1 php
// Set the Query POST parameters $query_vals = array( 'api_username' => 'api-username-here', 'api_password' => 'password-here', 'api_key' => 'key-here', 'firstname' => 'Mary', 'lastname' => 'Tester', 'address' => '122 Anywhere Street', 'city' => 'Little Rock', 'state' => 'AR', 'zip' => '72223', 'country' => 'US', 'email' => '[email protected]', 'username' => 'MaryTester', 'password' => 'password', 'perkalert' => 0, 'offer_radius' => 20, 'send_welcome_email' => 1 ); // Generate the POST string $postdata = ''; foreach($query_vals as $key => $value) { $postdata .= $key.'='.urlencode($value).'&'; } // Chop of the trailing ampersand $postdata = rtrim($postdata, '&'); // create a new cURL resource $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://secureserver/register_member.xml'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); // Save response to a string $response = curl_exec($ch); curl_close($ch); $xml = simplexml_load_string($response); //var_dump($xml); echo "Status: ".$xml->status;[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
line 48
The first three (api fields) will always have the same value, as will the last three fields. So six fields can remain static, but the other 10 fields will always change and must be queried from the WP user DB.
Then the user data is held in an array along with the other static first six fields, then we generate a post string, connect via curl to the other server, save the response to a simplexml load string and get a response from the server of ok or fail and we’re done.
I’ve recoded the WP user array code five times based on suggestions and recoded the post string code maybe seven times and I still get a (fail) server message.
Step 1 is – I first need the correct code syntax to query WP for the user fields shown. BTW, the (mepr-fields) listed in my first post are correct, they are custom usermeta fields in the WP DB.
Should I be using – get_currentuser, or should I use wp_get_current_user? After seeing so many code changes, I don’t know how to code the WP user info needed for the array.
Moderator,
My apologies for my last post not containing my code by backticks or using the code button. It won’t happen again.
Today may be a breakthrough. I just read a post from 2011 online that said since WP 3.3, the way user data is called for custom meta fields like my mepr-fields, has changed. This and my post code could be the reason it’s not working as expected.
The post is here > http://goo.gl/x5deHi I’m just not sure of the code syntax now that I should use to call those custom fields… I’m also starting to think that maybe only the one array called query_vals should be used and the calls to WP_user should be added to that query somehow.
get_user_meta()andget_currentuserinfo()are both appropriate for getting user data from the default WP tables and any custom data stored in usermeta. Obviously the current user info does no good if the POST string being built is not for the current user.I would expect the original code should still work for you with the only alteration being the hard coded array values are assigned dynamically from data returned by the afore mentioned WP functions, for example:
$meta = get_user_meta( $user->ID ); $query_vals['firstname'] = $meta['first_name'][0];I used the array key form for brevity, but the full array declaration is OK too, a fragment of which might look like this:
'firstname' => $meta['first_name'][0],Some of the required data may not reside in usermeta, such as email and password, so different ways to extract the data may be required. Note that it is not possible to obtain a plaintext WP user password from the stored hash, and the hash is not by default a simple MD5 hash. Hopefully the password to send is unrelated to the WP password!
Thanks for all of your input. The password presents a new issue to overcome. I’m using a user registration form plugin that I believe I can get a hook into, to do something creative here.
Let me ask you this. If I create a second custom user password field in the DB, named anything_user_password and write data to both the WP standard password user meta and the custom and the same time, if my custom password field is set as just a text field with no other properties, shouldn’t I be able to retrieve the password from the custom password field?
Or, since the password as it relates to all of this is just to supply something for the other login, theoretically I could have one password I would give to the other site that was a constant, so if the user changed their password on site 1, it would never effect their login on site 2. Sounds reasonable to me.
And last, so you’d recommend I use get_currentuserinfo() to build the 4 standard fields in the array? Can it get all of my custom user data fields stored in usermeta?
If not, would it be best from the start to call the 4 standard user fields with get_currentuserinfo() and call the 6 custom fields (which would include the custom password field) with wp_get_current_user?
There’s any number of ways to manage passwords, either of your ideas will work, but are not the most secure way to manage passwords. IMO, at a minimum you should store the second site password encrypted with a 2 way cipher. Depending on how far you go with this, it can be very secure. This password does not have to match (should not match?) the WP password, but it’d be a good idea to change it anytime (or more often?) the user changes the WP password for any reason.
Don’t over think how you get user data, it’s not that big a deal.
get_currentuserinfo()will not get custom meta data, but it gets other information you probably need that is not usermeta, so in reality you will likely be using both functions to get everything you need.bcworkz,
I’ve been troubleshooting my code now for two weeks trying to get it to work. Now that I’ve worked through all of the syntax errors, I simply get a – Status: fail, response.
So I thought it would be smart to see where my code is failing and I began to look into the codex examples for all of the methods to get user info to create a test file.
None of the code I see works. I either get a syntax error, or a blank browser window. Could you please write a few lines of code I could use that would display in a browser window if the current user is logged in – username, user email, etc.? Thanks.
My most recent code that doesn’t produce syntax errors, but also produces a fail response, looks like this:
// Call WordPress function get_currentuserinfo() // For 4 standard WP fields and wp_get_current_user() // To call 4 custom WP my-user-fields /** * @example Safe usage: $current_user = wp_get_current_user(); * if ( !($current_user instanceof WP_User) ) * return; */ function get_currentuserinfo() { global $user_firstname, $user_lastname, $user_login, $user_email; get_currentuserinfo(); function wp_get_current_user() { $current_user = wp_get_current_user (); $myvar = ($current_user->has_prop('my-field')) . $current_user->get('my-field') . '\n'; $myvar = $WP_vals = array (); $WP_vals = array( echo 'firstname' . $current_user->user_firstname . "\n"; echo 'lastname' . $current_user->user_lastname . "\n"; echo 'username' . $current_user->user_login . "\n"; echo 'email' . $current_user->user_email . "\n"; echo 'address' . $current_user->mepr-address-one . '<br />'; echo 'city' . $current_user->mepr-address-city . '<br />'; echo 'state' . $current_user->mepr-address-state . '<br />'; echo 'zip' . $current_user->mepr-address-zip . '<br />'; ); // return $WP_vals; } }Actually, that was a mistake. That was my test code. The code I’m using is this:
// Call WordPress function get_currentuserinfo() // For 4 standard WP fields and wp_get_current_user() // To call 4 custom WP my-user-fields /** * @example Safe usage: $current_user = wp_get_current_user(); * if ( !($current_user instanceof WP_User) ) * return; */ function get_currentuserinfo() { global $user_firstname, $user_lastname, $user_login, $user_email; get_currentuserinfo(); function wp_get_current_user() { $current_user = wp_get_current_user (); $myvar = ($current_user->has_prop('my-field')) . $current_user->get('my-field') . '\n'; $myvar = $WP_vals = array (); $WP_vals = array( 'firstname' . $current_user->user_firstname . '\n', 'lastname' . $current_user->user_lastname . '\n', 'username' . $current_user->user_login . '\n', 'email' . $current_user->user_email . '\n', 'address' . $current_user->mepr-address-one . '\n', 'city' . $current_user->mepr-address-city . '\n', 'state' . $current_user->mepr-address-state . '\n', 'zip' . $current_user->mepr-address-zip . '\n' ); return $WP_vals; } }There are still a few problems in your code. To be apprised of most errors, define
WP_DEBUGastruein wp-config.php.You cannot redefine already defined functions as you do with
function get_currentuserinfo() {andfunction wp_get_current_user() {Even if the function was not already defined, you do not call the function within it’s own declaration like this:
function get_currentuserinfo() { get_currentuserinfo(); }as it will result in infinite recursion.
The way you define
$WP_valsarray is not that useful. You can only access values by index number, and the values are odd:echo $WP_vals[0]; //outputs "firstnameJohn\n" echo $WP_vals[1]; //outputs "lastnamenDoe\n"etc. Including newlines in values is valid but a problem if you do not want it, for example, it’s difficult to construct inline text because all the data have newlines between them
The user’s real name is John
Doe
and he is from Dallas
,TX
75001
. He can be reached…Better to not include newlines in data and output them when required.
I don’t know if you need all of those globals, you should avoid using globals without good reason, as a general rule. If you do decide to use
get_currentuserinfo(), you will needglobal $current_user;because that function puts it’s results in that global.wp_get_current_user()basically callsget_currentuserinfo(), but in addition it should be called from within an action callback, so I would suggest you simply usewp_get_current_user(). This function only provides the WP_User object, no metadata, so the only thing useful you can get is username and email. Do so like this:global $current_user; get_currentuserinfo(); $WP_vals = array( 'username' => $current_user->data->user_login, 'email' => $current_user->data->user_email, );To get usermeta and add it to the
$WP_valsarray, do this:$current_user = get_user_meta(get_current_user_id()); $WP_vals['firstname'] = $current_user['first_name'][0]; $WP_vals['lastname'] = $current_user['last_name'][0]; $WP_vals['address'] = $current_user['mepr-address-one'][0]; $WP_vals['city'] = $current_user['mepr-address-city'][0]; $WP_vals['state'] = $current_user['mepr-address-state'][0]; $WP_vals['zip'] = $current_user['mepr-address-zip'][0];You could put all of this in a function (uniquely named), in which case returning
$WP_valsmakes sense so your code can make use of the accumulated values. But just to see the content of$WP_vals, you could do this:echo 'All User Data:<br><pre>'; var_dump( $WP_vals ); echo '</pre>';BTW, it’s useful to
var_dump()arrays and objects returned by WP functions in order to determine just how to address individual values. For example, the need to use[0]was not immediately apparent, var_dumping made the need quite obvious.Thanks for all of your input and code. After getting the code to function without syntax errors, the last most important issue is to have post string code that the simplexml post string will accept.
Using your code examples again, I’m getting parse errors. The code is choking when it gets through the WP code and gets to the post string, because it doesn’t know what $WP_vals is, it throws a fatal error saying $WP_vals is an undefined function.
I feel I’m getting closer to coding this in a way WP needs, but not that the post string needs and that’s the challenge to overcome once the WP calls to the DB are correct.
This code:
foreach($WP_vals as $keys => $values) $postdata = ''; var_dump($keys, $values); $s = '&'; $postdata .= $keys. '=' . urlencode($values) . $s = '&';Is not working because it doesn’t what $WP_vals is…
And as I said, this has been so confusing to me because the code I had in place to get user data:
$WP_vals = array( 'firstname' . $current_user->user_firstname . '\n', 'lastname' . $current_user->user_lastname . '\n', 'username' . $current_user->user_login . '\n', 'email' . $current_user->user_email . '\n', 'address' . $current_user->mepr-address-one . '\n', 'city' . $current_user->mepr-address-city . '\n', 'state' . $current_user->mepr-address-state . '\n', 'zip' . $current_user->mepr-address-zip . '\n' );came right out of an example in the codex. I’ve been working on this starting on week three and can’t figure out the balancing act here between the right WP code and post string code.
I’m confused again, about the first code you suggested that makes a call for the username and email, why it has no code at the end of each line to store each value for the post string.
As the post string requires these field keys & values to complete a successful post. Now that I’ve been at this for over two weeks without success, I think the (two arrays) are creating most of the problem.
Wouldn’t it be smarter to first call the right WP functions, set any globals, use only the $query_vals array, set the static field keys and values, call the (8) WP user fields and define the keys and values in the same way, now only one post string is needed and it all should work (if) the WP code is in a compatible format for the post string.
Your thoughts?
I’ve come to believe the only way this file is going to function correctly, is to have compatible WP code that the post string can process to get a (success) result.
I know my WP code here is not correct, but the functions and variables have to be set in some way first:
error_reporting(E_ALL); ini_set('display_errors',true); // Set the Query POST parameters for both // static and WP user fields function get_currentuserinfo() { $current_user = wp_get_current_user(); $user = wp_get_current_user(); if ( $current_user-> has_prop('my-field')){ get ( $current_user('my-field') '\n'); $id = (isset($_GET['id'])) ? (int) $_GET['id'] : NULL; if (!$id) echo 'oops... invalid id received.'; $query_vals = array( 'api_username' => 'api username goes here', 'api_password' => 'password here', 'api_key' => 'key here', 'password' => 'user password', 'country' => 'US', 'perkalert' => 0, 'offer_radius' => 20, 'send_welcome_email' => 1, //Get WP usermeta and add it to the $query_vals array 'firstname' . $current_user->user_firstname . '\n', 'lastname' . $current_user->user_lastname . '\n', 'username' . $current_user->user_login . '\n', 'email' . $current_user->user_email . '\n', 'address' . $current_user->mepr-address-one . '\n', 'city' . $current_user->mepr-address-city . '\n', 'state' . $current_user->mepr-address-state . '\n', 'zip' . $current_user->mepr-address-zip . '\n' ); } } // Generate the POST string $postdata = ''; foreach($query_vals as $key => $value) { $postdata .= $key.'='.urlencode($value).'&'; } // Chop of the trailing ampersand $postdata = rtrim($postdata, '&');I believe once the functions and variables are set and the WP code is in a format the post string will accept (I’m getting syntax errors), that all will work correctly.
The challenge is the WP code. I changed your code format for the values being [0] because the array format for the xml file expects the 0’s to be no and 1’s as yes.
It seems there needs to be a variable set also so php can use => like the static part of the array without throwing parse errors.
And the ‘\n’ I would assume needs to be set as a variable also to work with the post string.
The topic ‘Get current user array with post string’ is closed to new replies.