-
-
Save ivandoric/089443880872f1b81ae74f2502e41a1c to your computer and use it in GitHub Desktop.
<?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', | |
]); | |
}); |
@hamzu06 I have no idea man, sorry.
Thank You @ivandoric, my issue is resolved.
Thank You @ivandoric
@theahmedoff No problem! :)
custom post type shows field values on all posts url but shows null values on single post url.
Hey I wanna thank you for this snippet. It really helped me man.
I would want to ask you if there is a way to have an offset or a pagination inside the rest-api ? I want to add another filter like "?page=1,2,3" etc
Thank you.
Hi,
I tried the way you coded the endpoint, but for some reason my endpoint is not working.
When I go to the endpoints I'm directed towards the homepage of the site.
Can you help? Thanks in advance
Pastebin: https://pastebin.com/LeeEnMvb
@Esiarp Maybe you need to change the permalinks in WP admin. To use something like /postname/ instead of /?p=123
@ivandoric That was exactly the issue! I changed it to "Day and name" and works perfectly. Thank you!
@ivandoric First of all thank you to share this content for us. But, I have some problems at this code. I don't know where can I find the endpoints for the wordpress article. For example I want to get post category but I don't know the endpoint for category like get_the_post_thumbnail_url. Please help us.
Thank you for code again
does simply running the code add it as a plug in? that is the part I cannot figure out still lol
can i use ?slug= or /id like>> http://site/wp-json/wp/v2/posts/id or ?slug= with this plugin ? No . and why i cant:)
Thank you for this code,
I want to send push notification to the mobile app when I publish (Custom post type) POST in WordPress,
I have Java spring Rest API that sends a notification, I want to integrate it with the custom post, How can I do this can you guide me?
Thank you
Custom Post Plugin Code
`<?php
/*
Plugin Name: Send Notification CPT
Description: Declares a plugin that will create a custom post type to send notifications.
Version: 1.0
*/
if( ! defined('ABSPATH')) {
exit;
}
// Register Custom Post Type Send Notifiction
function create_sendnotifiction_cpt() {
}
add_action( 'init', 'create_sendnotifiction_cpt', 0 );
///
function my_plugin_rest_route_for_term( $route, $term ) {
if ( $term->taxonomy === 'genre' ) {
$route = 'http://192.168.100.24:8181/notification/topic' . $term->term_id;
}
}
add_filter( 'rest_route_for_term', 'my_plugin_rest_route_for_term', 10, 2 );
//
function rewrite_tuts_flush(){
}
?>`