Khandaker Toihidul Islam
Front-end Developer
React Js | Javascript | HTML | CSS | Bootstrap | Wordpress | PHP | Mysql

Get data from API in WordPress

We have to add the bellow codes to function.php in theme

function schoolList() {
	$request = wp_remote_get( 'http://example.com/api/schools' );
	if( is_wp_error( $request ) ) {
		return false;
	}
	$body = wp_remote_retrieve_body( $request );
	$data = json_decode( $body );
	$html = '';
	if( ! empty( $data ) ) {
		foreach ($data as $key => $row) {
			$html .= "<h6> $row</h6>";
		}
		
	}
	return $html;

}

add_shortcode('schools', 'schoolList');

Then need to add the shortcode into any page or post.

[schools]

You may also like...

Leave a Reply