Title: Get current user array with post string
Last modified: August 21, 2016

---

# Get current user array with post string

 *  [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/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!

Viewing 15 replies - 16 through 30 (of 41 total)

[←](https://wordpress.org/support/topic/get-current-user-array-with-post-string/?output_format=md)
[1](https://wordpress.org/support/topic/get-current-user-array-with-post-string/?output_format=md)
2 [3](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/3/?output_format=md)

 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858153)
 * Since one WP function can’t do both as it seems, then I need to somehow use get_currentuserinfo
   to get the user firstname, lastname, login, and email from the wp_users table
   and wp_get_current_user to get the other four fields – address, city, state and
   zip from the wp_usermeta table.
 * That’s the WP code challenge to overcome.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858155)
 * Right, all the data cannot be gathered by a single function, but that should 
   not be an impediment. All the data can still be collected into a single array
   from which you can construct the POST string. Forgive my bluntness, but the only
   impediment is your PHP skills 🙂 — no offense intended, just stating a fact.
 * If you are truly getting an undefined **function** error for `$WP_vals`, you 
   function declarations are still messed up, since `$WP_Vals` is **variable**. 
   Assuming you meant undefined variable error, this is likely a scope issue where
   the array was not properly returned from the function.
 * This shouldn’t be that big of an issue, I’m sorry you are struggling with it 
   so much. The good part is I expect you are learning a lot from the experience.
   One problem is apparently returning the fully defined array from a function. 
   Another is the loop building the POST string has a minor flaw, depending on which
   version we’re looking at.
 * Let’s discard the proper data-returned-from-functions issue by discarding function
   declarations (the word ‘function’ should not appear anywhere in your code), there
   is little point since the function would only be called once for any particular
   user. The only advantage is it organizes code a little better, at the price of
   more syntax rules to follow.
 * Using the code I provided last (no other code required) outside of any function
   should result in a usable data array. All that remains would be to construct 
   the POST string. Depending on how you plan to send the POST request, there is
   either no need to build a string, or there is an easier way than a `foreach(){}`
   loop.
 * The `HttpRequest` method takes an associative array, no need to build a string.
   The `cURL` method does require a string, which is easily built like so:
    `$postdata
   = http_build_query( $WP_vals );` This builds an URL encoded string, each key=
   value pair from an associative array separated by the ‘&’ character.
 * FYI, a typical POST string would never have newlines in it, in any case they 
   would get URL encoded into `%0A`. Just focus on getting WP data assigned to the`
   $WP_vals` array as is, don’t worry about ‘=’ and ‘&’ nor newlines, they are either
   not required or handled by `http_build_query()`.
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858158)
 * No offense taken. I’m a php newb. Using your code:
 *     ```
       //Get WP user data/meta data and add it to the $WP_vals array
   
       global $current_user;
       get_currentuserinfo();
       $WP_vals = array(
          'username' => $current_user->data->user_login,
          'email' => $current_user->data->user_email,
       );
   
       $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 ];
   
       // Generate the POST string
       $postdata = '';
       $postdata = http_build_query( $WP_vals );
       urlencode_deep( $query_vals , $WP_vals );
       ```
   
 * Because get_currentuserinfo isn’t defined as a function, php is throwing a fatal
   error – Call to undefined function get_currentuserinfo.
 * After it’s defined as a function, shouldn’t the global current user go under 
   the function?
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858159)
 * After defining get_currentuserinfo as a function, the code now runs through all
   of the array lines until hitting line 40 which starts the post string you suggested
   and errors with:
 * Notice: Undefined variable: WP_vals on line 40
 * Warning: http_build_query() [function.http-build-query]: Parameter 1 expected
   to be Array or Object. Incorrect value given.
    Call to undefined function urlencode_deep().
   < I got that out of the codex.
 * Man… getting closer.
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858161)
 * Again, I’ve gotten through all syntax errors and get a Status: fail.
    I think
   this is because the post string doesn’t understand the coding for the values.
 * Do we need some type of variable set for the values? It failed when the code 
   for the WP_vals array was set to [0], then I changed it to [\n] and it failed
   also.
 * how can I state a variable that tells the post string that [0] = ‘ ‘ ?
    Like 
   this: `$WP_vals array($value [0] = ' ');` Or no?
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858162)
 * Still getting a fail status and the code now looks like this:
 *     ```
       //Get WP user data/meta data and add it to the $WP_vals array
   
       global $current_user;
       function get_currentuserinfo(){
       $WP_vals = array(
          'username' => $current_user->data->user_login,
          'email' => $current_user->data->user_email,
       );
   
       $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'];
   
       }	
   
       // Generate the POST string
       $postdata = '';
       foreach($query_vals as $key => $value) {
       	$postdata .= $key.'='.urlencode($value).'&';
       $WP_vals = array();
       http_build_query($WP_vals, $key='', $urlencode=true);
       }
       ```
   
 * I don’t think the post string knows the values of the first two fields in WP_vals–
   user_login, user_email. Could that be it?
 * Could it be that there isn’t enough defined objects for the post string? You 
   know, there’s not a variable set for the custom fields in meta data. Could that
   be a problem?
 * I read these two things on a site that apply to WP 3.3 and later:
    If you have
   a variable key or a key with dashes, there are prettier aliases which you can
   use:
 *     ```
       if ( $current_user->has_prop( 'my-field' ) ) )
       	echo '<p>
         ' . $current_user->get( 'my-field' ) . '
       </p>';
       ```
   
 * And to generate a post string:
 *     ```
       foreach ( get_user_meta( $user->ID ) as $key => $values ) {
       	var_dump( $key, $values );
       }
       ```
   
 * Code rewrite?
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858163)
 * After reading up on curl, I’m wondering if that code needs a few tweaks. Here’s
   my existing code:
 *     ```
       // Chop of the trailing ampersand
       $postdata = rtrim($postdata, '&');
   
       // create a new cURL resource
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, 'https://secureserver.com/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;
       ```
   
 * And I read this today online as a curl example:
 *     ```
       // A very simple PHP example that sends a HTTP POST to a remote site
       $ch = curl_init();
   
       curl_setopt($ch, CURLOPT_URL,"http://example.com/feed.rss");
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=value1&postvar2=value2");
   
       // in real life you should use something like:
       // curl_setopt($ch, CURLOPT_POSTFIELDS,
       //          http_build_query(array('postvar1' => 'value1')));
   
       // receive server response ...
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   
       $server_output = curl_exec ($ch);
   
       curl_close ($ch);
   
       // further processing ....
       if ($server_output == "OK") { ... } else { ... }
       ```
   
 * Thoughts?
 *  [xennex81](https://wordpress.org/support/users/xennex81/)
 * (@xennex81)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858164)
 * Dude, are you even a programmer?
 * You don’t seem to be actually understanding or implementing the changes that 
   [@bcworkz](https://wordpress.org/support/users/bcworkz/) offers.
 * You seem to be just randomly changing things.
 * You are coding like I would if I hadn’t slept for 15 days while hanging upside
   down with my hands tied behind my back working the keyboard with my teeth.
 * This is evident from the first response you give as you avoid [@bcworkz](https://wordpress.org/support/users/bcworkz/)’
   s questions and suggestions by claiming that they are not valid because you have
   since changed the code.
 * This isn’t going to work out for you, and I suggest you leave programming to 
   someone else (not me :p).
 * Even if you asked someone to write this code for you on this forum or elsewhere,
   you’d be done in 20 minutes instead of spending another 3 weeks debugging this….
 * Just my 2 cents.
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858165)
 * Dude, I’m not a coder, but have been tagged with this responsibility to get this
   done. And guess what? Regular php coders that don’t code for WP can’t figure 
   this one out and none of the coding tips I’ve been given here have worked either,
   even though I’ve made all of the changes and tested every one of them.
 * So what would be your advise? Make a change and wait for 24 hours for someone
   to respond just to find that the next coding suggestion doesn’t work either?
 * You know NOTHING about troubleshooting methods I can see. A good troubleshooter
   looks at problems in a logical manner and attempts to solve a problem considering
   ALL aspects. That’s not you it’s clear.
 * You’re someone who likes to come to conclusions instantly without all of the 
   facts. NO attention to detail.
 * This is a balancing act Slick. You have to get the WP code right, but you can’t
   ignore the post string requirements. So F off and don’t add anything further 
   to this post if it’s not constructive di&^head.
 *  [xennex81](https://wordpress.org/support/users/xennex81/)
 * (@xennex81)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858166)
 * Yeah, real smart.
 * Go ahead and fix your code then.
 * You have been given all the help you needed already 6 posts ago.
 * Clearly a case of someone overestimating his ability. And being arrogant about
   it too.
 * You’re right. I have no business here. Just wanted to point out the obvious.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858167)
 * C’mon you guys, that’s enough. You’ve both stated your positions, further dialog
   and name calling is not adding anything meaningful.
 * Even though xennex81’s posts are inappropriately confrontational, he does make
   a fair point. If you just need this done, save yourself a lot of time and trouble
   and find someone that knows what they are doing and will write code for pay. 
   The so called “regular” coders you know either are misrepresenting themselves
   or know PHP but nothing of WordPress, this really isn’t that complicated. In 
   order to not take away from people that code for pay, I do not write any significant
   code for people here.
 * I am willing to take the time to help people learn if that is their desire. Unfortunately,
   I pretty much stick to a daily response regime, anything more frequent would 
   take too much time away from the rest of my life and becomes too much like an
   unpaid consultant. Note that I will not accept pay for more frequent advice nor
   coding work, that is not why I help out here.
 * So if your goal is ultimately to learn, albeit at a very slow metered pace, we
   can continue. It does no good to declare a function definition for `get_currentuserinfo()`
   in responce to a undefined function message, you cannot possibly define the function
   in a useful manner. The problem is the page wp-includes/pluggable.php which contains
   the real function declaration is not loading properly. Until this can be resolved,
   there is little point in discussing the remainder of your code, as it is based
   on fallacious deductions.
 * Just where is this code residing? What action is causing it to run? The WP environment
   is not loading properly if calling `get_currentuserinfo()` yields an undefined
   function message.
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858168)
 * All points well taken. With regard to me and this code, I’m invested now and 
   I don’t quit when things take time or get tough. The file is in the wp-includes
   folder.
 * And the file is called by a javascript onclick function. Now it makes sense, 
   I don’t remember why I took it out, but at one point many coded changes ago, 
   I did have this line in the file.
 * `require (ABSPATH . WPINC . 'pluggable.php');`
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858169)
 * Even better, a big thanks to a fellow native Arkansan, Justyn Hornor for these
   code declarations, so WP knows where its files are and I’m now using this:
 *     ```
       For example, add the sub-folder to the $location variable:
       $location = $_SERVER['DOCUMENT_ROOT'] . '/your-sub-folder';
       */
   
       $location = $_SERVER['DOCUMENT_ROOT'] . '/public_html';
   
       include ($location . '/wp-config.php');
       include ($location . '/wp-load.php');
       include ($location . '/wp-includes/pluggable.php');
   
       global $wpdb;
   
       if( !isset($wpdb) )
       {
       include ($location . '/wp-config.php');
       include ($location . '/wp-includes/wp-db.php');
       }
       ```
   
 * Justyn’s complete file can be seen here > [http://goo.gl/zJCVvF](http://goo.gl/zJCVvF)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858174)
 * I’m glad that’s out of the way 🙂 In way of a disclaimer in case anyone else 
   is following along (hard to believe, but one never knows), including files this
   way is fine if this code is for use on your own site. No one should use techniques
   like this in developing plugins or themes to be distributed to others because
   WordPress can be installed anywhere and the wp_contents folder can be moved somewhere
   completely different relative to the other folders, so simplistic path references
   will fail for many installations. Attempting such references in code destined
   for the WordPress repository will be summarily rejected.
 * If you organize your code page as follows, without declaring any new functions,
   everything should work once you iron out any syntax bugs.
 * Include all required code pages as needed.
 * Call the WP functions to get user and usermeta values that are needed for the
   cURL request and place the values into the `$WP_vals` array, as suggested previously.
 * Use `http_build_query()` to convert the `$WP_vals` array into a cURL compatible
   fields string.
 * Do the other cURL setup steps as required, then execute cURL and receive a response.
 * Do something based on the response.
 *  Thread Starter [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * (@headmonkey)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/2/#post-4858175)
 * I’ve declared no functions now in my code, have all of the suggested code changes
   in place and now something different has happened. Instead of a success or fail
   status, I see no response at all.
 * Starting with the WP code, here’s my current code:
 *     ```
       //Set some WP file locations so it can access them
   
       $location = $_SERVER['DOCUMENT_ROOT'];
   
       include ($location . '/wp-config.php');
       include ($location . '/wp-load.php');
       include ($location . '/wp-includes/formatting.php');
       include ($location . '/wp-includes/pluggable.php');
   
       global $wpdb;
   
       if( !isset($wpdb) )
       {
       include ($location . '/wp-config.php');
       include ($location . '/wp-includes/wp-db.php');
       }
       //Get WP user data/meta data and add it to the $WP_vals array
   
       global $current_user;
       get_currentuserinfo();
       $WP_vals = array(
          'username' => $current_user->data->user_login,
          'email' => $current_user->data->user_email,
   
       );
   
       $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];
   
       // Generate the POST string
   
       $postdata = '';
       foreach($query_vals as $key => $value) {
       	$postdata .= $key.'='.urlencode($value).'&';
       }
       	$postdata = http_build_query( $WP_vals );
   
       // Chop of the trailing ampersand
       $postdata = rtrim($postdata, '&');
   
       // create a new cURL resource
   
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, 'https://secureserver.com/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;
       ```
   
 * From what I’ve been reading on cURL, is it possible that the CURLOPT_POSTFIELDS
   line of code needs tweaking? I think curl doesn’t know how to process the WP_vals
   array. Your thoughts?

Viewing 15 replies - 16 through 30 (of 41 total)

[←](https://wordpress.org/support/topic/get-current-user-array-with-post-string/?output_format=md)
[1](https://wordpress.org/support/topic/get-current-user-array-with-post-string/?output_format=md)
2 [3](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/3/?output_format=md)

The topic ‘Get current user array with post string’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 41 replies
 * 3 participants
 * Last reply from: [NetMonkey](https://wordpress.org/support/users/headmonkey/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/get-current-user-array-with-post-string/page/3/#post-4858200)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
