How to upload image to WordPress Database from mobile app?




Geniuswp show

Summary: This article will help to understand How to upload image to WordPress Database from mobile app?, along with a fully working code (a function to upload an image to WP Database)If you are developing a mobile App using WordPress as a backend then you need to make use of WP Rest API.The WP REST API provides API endpoints for WordPress data types thatallow developers to interact with sitesremotely by sending and receivingJSON(JavaScript Object Notation) objects.REST API Handbook by WordPressPage Contents How to upload image to WordPress Database from mobile app?Few dependencies need to be included above this function to work How to upload image to WordPress Database from mobile app?media_handle_upload( ‘image’, 0 );https://codex.wordpress.org/Function_Reference/media_handle_upload‘image’ – It has all image data to be uploaded which comes from the $_FILES request0 – set the second argument to zero so that the image is not associated or assigned to any post instead the image will be independently saved to the database.Few dependencies need to be included above this function to work require_once( ABSPATH . ‘wp-admin/includes/image.php’ );require_once( ABSPATH . ‘wp-admin/includes/file.php’ );require_once( ABSPATH . ‘wp-admin/includes/media.php’ );Check under Examples on https://codex.wordpress.org/Function_Reference/media_handle_upload For proper use of it.Technically there is no difference at all if you need to upload an image coming via a Form on the mobile app or from simple web app front end Form, in both the case image upload is taken care the same way on the PHP side.Full working code (function) to update user profile with profile picture & other user details like ‘first_name’, ‘last_name’ ,’company’, ‘job_title’, ‘interest’,’linkedin’, ‘twitter’, ‘facebook’// update user profile with profile picture & other user details like// 'first_name', 'last_name' ,'company', 'job_title', // 'interest','linkedin', 'twitter', 'facebook'public function update_user_profile( WP_REST_Request $request ) { // default response to send if no other response is generated. $response = array( 'Status' => 2, 'Response' => array(), 'Error' => 'Nothing To Process' ); // We are using JSON Web Token (JWT) - // The reason why JWT is used is to prove // that the sent data was // actually created by an authentic source. // get authorization code send in header $access_token = $request->get_header( 'Authorization' ); if( empty($access_token) || ( isset($access_token) && !$this->verify_token($access_token)) ) { $response = array('Status' => 2, 'Response' => array(), 'Error' => 'cant verify access token'); wp_send_json($response); } // $this->email - it... You are listening to the topic about "How to upload image to WordPress Database from mobile app?", if you want to read the full article, please visit https://geniuswp.com or the link in the description.