-
-
Save rfos/630cc9e0a1dc7a4c3966ae7140296bf9 to your computer and use it in GitHub Desktop.
WordPress Rest API Custom Endpoints Video Tutorials Notes - Check out the videos: https://www.youtube.com/watch?v=C2twS9ArdCI and https://www.youtube.com/watch?v=76sJL9fd12Y
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Custom API | |
* Plugin URI: http://chrushingit.com | |
* Description: Crushing it! | |
* Version: 1.0 | |
* Author: Art Vandelay | |
* Author URI: http://watch-learn.com | |
*/ | |
function wl_posts() { | |
$args = [ | |
'numberposts' => 99999, | |
'post_type' => 'post' | |
]; | |
$posts = get_posts($args); | |
$data = []; | |
$i = 0; | |
foreach($posts as $post) { | |
$data[$i]['id'] = $post->ID; | |
$data[$i]['title'] = $post->post_title; | |
$data[$i]['content'] = $post->post_content; | |
$data[$i]['slug'] = $post->post_name; | |
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail'); | |
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium'); | |
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large'); | |
$i++; | |
} | |
return $data; | |
} | |
function wl_post( $slug ) { | |
$args = [ | |
'name' => $slug['slug'], | |
'post_type' => 'post' | |
]; | |
$post = get_posts($args); | |
$data['id'] = $post[0]->ID; | |
$data['title'] = $post[0]->post_title; | |
$data['content'] = $post[0]->post_content; | |
$data['slug'] = $post[0]->post_name; | |
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail'); | |
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium'); | |
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large'); | |
return $data; | |
} | |
// Used in this video https://www.youtube.com/watch?v=76sJL9fd12Y | |
function wl_products() { | |
$args = [ | |
'numberposts' => 99999, | |
'post_type' => 'products' | |
]; | |
$posts = get_posts($args); | |
$data = []; | |
$i = 0; | |
foreach($posts as $post) { | |
$data[$i]['id'] = $post->ID; | |
$data[$i]['title'] = $post->post_title; | |
$data[$i]['slug'] = $post->post_name; | |
$data[$i]['price'] = get_field('price', $post->ID); | |
$data[$i]['delivery'] = get_field('delivery', $post->ID); | |
$i++; | |
} | |
return $data; | |
} | |
add_action('rest_api_init', function() { | |
register_rest_route('wl/v1', 'posts', [ | |
'methods' => 'GET', | |
'callback' => 'wl_posts', | |
]); | |
register_rest_route( 'wl/v1', 'posts/(?P<slug>[a-zA-Z0-9-]+)', array( | |
'methods' => 'GET', | |
'callback' => 'wl_post', | |
) ); | |
// Used in this video: https://www.youtube.com/watch?v=76sJL9fd12Y | |
register_rest_route('wl/v1', 'products', [ | |
'methods' => 'GET', | |
'callback' => 'wl_products', | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment