0

I need to get 10 current user fields and pass the field values to a post string. The post string is expecting certain label names for each value.

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. I read a post today from 2011 that says since WordPress 3.3 (current version now is 3.9), that the way custom user fields are called is different. The post > http://goo.gl/x5deHi says the way to call custom WP fields like 5 of mine are, is like this:

    if ( $current_user->has_prop( 'my-field' ) ) )
echo '<p>
    ' . $current_user->get( 'my-field' ) . '
    </p>';`

So this is the code I have right now:

    //Get values for these 10 user fields 
    function wp_get_current_user() {
    $current_user = wp_get_current_user();

    $current_user_info = array(
    'firstname' . $current_user->user_firstname =>' ',
    'lastname' .  $current_user->user_lastname =>' ',
    if ( $current_user->has_prop( 'mepr-address-one' ) ) )
    ' . $current_user->get( 'mepr-address-one' ) .  => ' ',
    if ( $current_user->has_prop( 'mepr-address-city' ) ) )
    ' . $current_user->get( 'mepr-address-city' ) .  => ' ',
    if ( $current_user->has_prop( 'mepr-address-state' ) ) )
    ' . $current_user->get( 'mepr-address-state' ) .  => ' ',
    if ( $current_user->has_prop( 'mepr-address-zip' ) ) )
    ' . $current_user->get( 'mepr-address-zip' ) .  => ' ',
    if ( $current_user->has_prop( 'mepr-address-country' ) ) )
    ' . $current_user->get( 'mepr-address-country' ) .  => ' ',   
    'email' . $current_user->user_email =>' ',
    'username' . $current_user->user_login =>' ',
    'password' . $current_user->user_pass =>' '

    );
    }
    // Generate the POST string
    // These last three lines aren't posting my get user values
    $postdata = '';
    foreach($query_vals as $key => $value){ 
$postdata .= $key.'='.urlencode($n=$value).'&';
    }

And the post string needs to meet the parameters described in the post that is this:

    foreach ( get_user_meta( $user->ID ) as $key => $values ) {
var_dump( $key, $values );
    }

But being a php newb, I'm not sure of the coding syntax. Don't I have to define has_prop before I can use it?

  • Two problems I can see quickly. Your global should be inside your function, not outside, and Function should be function – Pieter Goosen Apr 28 '14 at 18:23
0

Your code has a lot of errors. For example, inside you should define the global $current_user, not outside. Other error, you use $query_vals variable outside the function where that variable is not defined. Anyway, I would use the function wp_get_current_user() which have not to be called before init action hook.

For example, if you are going to use the POST string as data for javascript:

add_action( 'wp_enqueue_scripts', 'wpse_scripts' );

function wpse_scripts() {

      $user_fields = wpse_get_current_user_info();
      $postdata = http_build_query( $user_fields );

      wp_localize_script( 'my_script', 'my_script_data', $postdata );

}

function wpse_get_current_user_info() {
      $current_user = wp_get_current_user();
      $current_user_info = array(
                    'firstname' => $current_user->user_firstname,
                    'lastname'  => $current_user->user_lastname,
                    //Add the rest of info you need
                    //In the forman key => value
                   );
      return $current_user_info;
}

In order to help you in a more specific way you should tell us where are you going to use the POST string and the context.

Basic example to send the user data to a server using WP HTTP API (Call wpse_send_user_data when you need to get the response body after sending the user data to the third party service):

function wpse_send_user_data() {

      //You should check here if the request should be done
      //if( $some_control == true ) return;

      $uerdata = wpse_get_current_user_info();

      $args = array( 'method' => 'POST', 'body' => $uerdata );
      $request = wp_remote_request( 'http://www.example.com', $args )

      $response = wp_remote_retrieve_body($request);

      return $response;

}

function wpse_get_current_user_info() {
      $current_user = wp_get_current_user();
      $current_user_info = array(
                    'firstname' => $current_user->user_firstname,
                    'lastname'  => $current_user->user_lastname,
                    //Add the rest of info you need
                    //In the forman key => value
                   );
      return $current_user_info;
}

I prefer use WP HTTP API but if you want to use cURL:

function wpse_send_user_data() {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/");
    curl_setopt($ch, CURLOPT_POST, 1);

    // Get userinfo array and set as POST fields
    $uerdata = wpse_get_current_user_info();
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $uerdata ));

    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);

    curl_close ($ch);

    // further processing ....
    if ($server_output == "OK") { ... } else { ... }

}

function wpse_get_current_user_info() {
      $current_user = wp_get_current_user();
      $current_user_info = array(
                    'firstname' => $current_user->user_firstname,
                    'lastname'  => $current_user->user_lastname,
                    //Add the rest of info you need
                    //In the forman key => value
                   );
      return $current_user_info;
}
  • The purpose of the whole file is to generate a simple xml load string to securely register the new user via SSL at a third-party content provider website, so the user does not have to completely re-enter all of their user information again at that site and to eliminate the possibility of data mismatches. – NetMonkey Apr 28 '14 at 19:05
  • I'm trying to help a long time friend who paid some developers a lot of $ and got a crap, incomplete WP site. This is one of the last pieces of code needed. – NetMonkey Apr 28 '14 at 19:10
  • The original file had 16 static data fields in the array that looked like this: 'firstname' => 'Mary', 'lastname' => 'Smith', and the POST code looked like this: $postdata = ''; foreach($query_vals as $key => $value) { $postdata .= $key.'='.urlencode($value).'&'; } to automate things, I have to format the array and postdata code so the post string complies to process the now variable user data. – NetMonkey Apr 28 '14 at 19:37
  • BTW, the third-party website is G-rated. The originating site is a consumer discounts membership website. – NetMonkey Apr 28 '14 at 22:16
  • Once you have the array with the user info, http_build_query will generate the URL-encoded query string, so you don't need the foreach loop, just be sure to pass the array with the correct key => value pairs. How/where do you send the user data to the third party website? – cybmeta Apr 29 '14 at 8:36

Your Answer

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

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