0

I am quite new to wordpress and I am on my path to create my first plugin. I want to add a feature to my plugin, that I can read queried data from the wordpress db. I have already written a function to process the user data from wordpress, and echo them as json.

function process_data($type, $sdate, $edate) { $data = array(); global $wpdb; if($type == 'overview' || $type == 'users') { $data[date('Y-m-d', strtotime($sdate))]['users'] = 0; $daily_users = $wpdb->get_results("SELECT COUNT(*) as cnt, DATE(user_registered) dte FROM $wpdb->users WHERE user_registered>='".date('Y-m-d', strtotime($sdate))."' AND user_registered<='".date('Y-m-d', strtotime($edate))."' GROUP BY dte"); foreach($daily_users as $value) { $date = str_replace('-', '', $value->dte); $data[$date]['users'] = $value->cnt; } } echo json_encode($data); } 

However, how to read them with an external application. Any ideas how to call this function?

I would appreciate your answer!

2
  • 1
    If you want to enable open access from any application, I would suggest to work with Custom Feeds instead. But be aware of what kind of data you are offering, this could turn into a huge security flaw. Commented May 22, 2014 at 14:49
  • @mathielo Thx for your answer! I will also add a key to identify at my application. However, I was wondering how to call this function and display the data in the browser? How to see the json object? Commented May 22, 2014 at 14:54

1 Answer 1

1

Ideally you would want to create a custom WordPress API endpoint if you would like to access this data from an external application.

Example:

add_action('rest_api_init', 'my_rest_routes_init_7464'); function my_rest_routes_init_7464() { register_rest_route('my-custom-endpoint/v1','/get-db-data', array( 'methods' => 'GET, POST', 'callback' => 'process_data' )); } 

Then you would need to change your function to:

function process_data(WP_REST_Request $request) { if(isset($request)) { if(isset($request['type']) && isset($request['sdate']) && isset($request['edate'])) { $type = sanitize_text_field($request['type']); $sdate = sanitize_text_field($request['sdate']); $edate = sanitize_text_field($request['edate']); $data = array(); global $wpdb; if($type == 'overview' || $type == 'users') { $data[date('Y-m-d', strtotime($sdate))]['users'] = 0; $daily_users = $wpdb->get_results("SELECT COUNT(*) as cnt, DATE(user_registered) dte FROM $wpdb->users WHERE user_registered>='".date('Y-m-d', strtotime($sdate))."' AND user_registered<='".date('Y-m-d', strtotime($edate))."' GROUP BY dte"); foreach($daily_users as $value) { $date = str_replace('-', '', $value->dte); $data[$date]['users'] = $value->cnt; } } echo json_encode($data); die(); } } else { echo json_encode(array("error"=>"no request data")); } } 

The simply call your custom end point http://yourdomain.com/my-custom-endpoint/v1/get-db-data/ from your external function using either GET or POST data for type, sdate and edate.

Caveat: You should consider creating a "secret token" that you can use to check requests and block requests that do not contain the secret token.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.