Khandaker Toihidul Islam
Software Engineer
Backend : PHP | Mysql | Symfony | CakePhp | Wordpress | Drupal
Frontend : Node js | Vue js | React Js | Javascript | HTML | CSS | Bootstrap
Tools , Server : Nginx | Apache | Git | Jenkins | Jira | Asana | Silenium | Spira

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

Your email address will not be published. Required fields are marked *