WordPress

Uzyskaj bieżącą tablicę użytkowników z ciągiem postów

  • 28 kwietnia, 2014
  • 3 min read
Uzyskaj bieżącą tablicę użytkowników z ciągiem postów


Twój kod zawiera wiele błędów. Na przykład wewnątrz powinieneś zdefiniować plik global $current_user, nie na zewnątrz. Inny błąd, którego używasz $query_vals zmienną poza funkcją, w której ta zmienna nie jest zdefiniowana. W każdym razie użyłbym tej funkcji wp_get_current_user() które nie muszą być wywoływane przed hakiem akcji init.

Na przykład, jeśli zamierzasz użyć ciągu POST jako danych dla 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;
}

Aby pomóc ci w bardziej konkretny sposób, powinieneś nam powiedzieć, gdzie zamierzasz użyć ciągu POST i kontekstu.

Podstawowy przykład wysyłania danych użytkownika do serwera za pomocą WP HTTP API (Wywołaj wpse_send_user_data, gdy potrzebujesz uzyskać treść odpowiedzi po wysłaniu danych użytkownika do usługi strony trzeciej):

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( ' $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;
}

Wolę używać WP HTTP API, ale jeśli chcesz użyć cURL:

function wpse_send_user_data() {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"
    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;
}


Źródło

Warto przeczytać!  Jak wykonać kopię zapasową witryny WordPress przed migracją