$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - Add wordpress user database on android app|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Add wordpress user database on android app

matteradmin8PV0评论

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

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

Share Improve this question edited Dec 23, 2018 at 0:46 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 21, 2016 at 13:11 M ASED AHMEDM ASED AHMED 17 bronze badges 2
  • 1 Please share an example of the URL structure that would suit makemedroid requirements. – Sagive Commented Jan 21, 2016 at 13:51
  • like twitter or facebook uses – M ASED AHMED Commented Jan 30, 2017 at 16:57
Add a comment  | 

1 Answer 1

Reset to default 1

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

In this case, we'll register the endpoint http://example/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/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
Post a comment

comment list (0)

  1. No comments so far