0

I have been using WordPress for the past two years and I always used plugins for additional functionality. But now I want to learn more about WordPress. I want to learn how to change things using html and how to use a json URL to call WordPress get_userdata from the database but I don't know how.

My question is:

Can I do that? If so, how can i use a json URL in order to get user information in wordpress? ( If needed I have SSL certificate)

Any answers will be helpful.

I already checked this but I don't understand how this happened

Using WordPress User Details outside Directory

I am creating an android app using android studio

I wanted to create login screen on my Android app so users would be able to login or sign up from android app

  • 1
    Please share an example of the URL structure that would suit makemedroid requirements. – Sagive SEO Jan 21 '16 at 13:51
  • like twitter or facebook uses – M ASED AHMED Jan 30 '17 at 16:57
1

It's possible to write your own endpoing using add_rewrite_rule().

In this case, we'll register the endpoint http://example.com/api/user_data/{id}.

Then check to make sure the ID is numeric. If it is, lookup the user by id and output the JSON response.

    URL
    http://example.com/api/user_data/1

    JSON
    {
      "success": true,
      "data": {
        "id": 1,
        "display_name": "your_username",
        "user_registered": "2015-11-12 05:00:00",
        "first": "First",
        "last": "Last Name"
      }
    }

PHP

Put this in functions.php or in a plugin then refresh your permalinks.

if ( ! class_exists( 'JSONEndpoint_UserData' ) ):
    /**
     * The code that registers the endpoint and handles the result
     */
    class JSONEndpoint_UserData {

        const ENDPOINT_NAME       = 'api/user_data'; // endpoint to capture
        const ENDPOINT_QUERY_NAME = '__api_user_data'; // turns to param

        // WordPress hooks
        public function run() {
            add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
            add_action( 'parse_request', array( $this, 'sniff_requests' ), 0 );
            add_action( 'init', array( $this, 'add_endpoint' ), 0 );
        }

        // Add public query vars
        public function add_query_vars( $vars ) {
            $vars[] = static::ENDPOINT_QUERY_NAME;
            $vars[] = 'id';

            return $vars;
        }

        // Add API Endpoint
        public function add_endpoint() {
            add_rewrite_rule( '^' . static::ENDPOINT_NAME . '/([^/]+)/?$', 'index.php?' . static::ENDPOINT_QUERY_NAME . '=1&id=$matches[1]', 'top' );
// --->
            flush_rewrite_rules( true ); //// <---------- REMOVE THIS WHEN DONE TESTING
// --->
        }

        // Sniff Requests
        public function sniff_requests( $wp_query ) {

            global $wp;

            if ( isset( $wp->query_vars[ static::ENDPOINT_QUERY_NAME ] ) ) {
                $this->handle_request(); // handle it
            }
        }

        // Handle Requests
        protected function handle_request() {
            global $wp;

            // we only deal with number$
            $id = is_numeric( $wp->query_vars[ 'id' ] ) ? absint( $wp->query_vars[ 'id' ] ) : false;

            if ( ! is_numeric( $id ) || ! $user = get_user_by( 'id', $id ) ) {
                wp_send_json_error( array( 'message' => 'Invalid User ID' ) );
            }

            // ALLOWING ACCESS FROM ANYWHERE --- WE MIGHT WANT TO RESTRICT THE PLACES THAT CAN USE THIS
            header( "Access-Control-Allow-Origin: *" );

            // prep the response
            $data = array(
                'id'              => $user->ID,
                'display_name'    => $user->data->display_name,
                'user_registered' => $user->data->user_registered, 
                'first'           => $user->first_name,
                'last'            => $user->last_name,
            );

            // write the response
            wp_send_json_success( $data );

            die(); // just in case
        }
    }

    $ep = new JSONEndpoint_UserData();
    $ep->run();

endif; // JSONEndpoint_UserData
  • Sorry I can't understand how it works (I am new at learning).How to get api and where to put the code? – M ASED AHMED Jan 22 '16 at 14:09
  • This converts a url to user data as JSON. The code needs to run every time WP loads in order to work so putting it in a plugin is good or functions.php of your theme. – jgraup Jan 22 '16 at 14:46
  • +1 But how can I get api key and how it works on json URL ? I think I understand how it goes (1st add the php code on functions.php then add the JSON where I wanted to call WordPress user data right?) But how to get my api key? @jgraup – M ASED AHMED Jan 22 '16 at 20:19
  • Sorry, this is pretty bare bones. You don't add JSON anywhere, this essentially creates your own API route on your website. So when you access the URL you get JSON data back. You would need to add your own logic and authentication -- but that's beyond the scope of what you originally asked. Unfortunately I don't have anymore time to dive into this further. – jgraup Jan 22 '16 at 21:51
  • i have learned to create app with android studio so can you help me connect my wordpress DB in android app – M ASED AHMED Jan 30 '17 at 16:59

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.