<?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', | |
]); | |
}); |
This comment has been minimized.
This comment has been minimized.
@kingkarki Sure, it's here: https://www.youtube.com/watch?v=C2twS9ArdCI |
This comment has been minimized.
This comment has been minimized.
Thank you for your code. |
This comment has been minimized.
This comment has been minimized.
I can not find the Custom API plugin in wordpress plugins, from where i can download it? |
This comment has been minimized.
This comment has been minimized.
Did you watch the video? There is no Custom API plugin. You create it yourself. |
This comment has been minimized.
This comment has been minimized.
When i call a ACF field in this its show result as |
This comment has been minimized.
This comment has been minimized.
@banna360 Are you sure it should just be url? Check the API response. Maybe it's something like |
This comment has been minimized.
This comment has been minimized.
Thank you for this code, Thank you
`<?php if( ! defined('ABSPATH')) { // Register Custom Post Type Send Notifiction
} /// function my_plugin_rest_route_for_term( $route, $term ) {
} // function rewrite_tuts_flush(){
} ?>` |
This comment has been minimized.
This comment has been minimized.
@hamzu06 I have no idea man, sorry. |
This comment has been minimized.
This comment has been minimized.
Thank You @ivandoric, my issue is resolved. |
This comment has been minimized.
This comment has been minimized.
Thank You @ivandoric |
This comment has been minimized.
This comment has been minimized.
@theahmedoff No problem! :) |
This comment has been minimized.
Thank you for the simple example. Can you post your youtube video link on the comment, please?