Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
<?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',
]);
});
@kingkarki

This comment has been minimized.

Copy link

@kingkarki kingkarki commented Jan 17, 2020

Thank you for the simple example. Can you post your youtube video link on the comment, please?

@ivandoric

This comment has been minimized.

Copy link
Owner Author

@ivandoric ivandoric commented Jan 17, 2020

@yhatzis

This comment has been minimized.

Copy link

@yhatzis yhatzis commented May 16, 2020

Thank you for your code.
It solve many problems.
I'm looking how display categories in posts.
I'm trying adding this but it didn't work "$data[$i]['categories'] = $post->post_categories;"
I suppose because categories is an array...(?)
Also could you propose me how remove some tags (like ' ) from content?
Thank you in advance.

@jassim9797

This comment has been minimized.

Copy link

@jassim9797 jassim9797 commented May 28, 2020

I can not find the Custom API plugin in wordpress plugins, from where i can download it?

@ivandoric

This comment has been minimized.

Copy link
Owner Author

@ivandoric ivandoric commented May 28, 2020

@jassim9797

Did you watch the video? There is no Custom API plugin. You create it yourself.

@banna360

This comment has been minimized.

Copy link

@banna360 banna360 commented Aug 27, 2020

When i call a ACF field in this its show result as "url": null
$data[$i]['url'] = get_field( "url", $post->ID );

@ivandoric

This comment has been minimized.

Copy link
Owner Author

@ivandoric ivandoric commented Aug 30, 2020

@banna360 Are you sure it should just be url? Check the API response. Maybe it's something like $data[$i]['acf_fields']['url'] or something like that. I think ACF would wrap it's fields inside another array. Not 100% sure, but you can test it out.

@hamzu06

This comment has been minimized.

Copy link

@hamzu06 hamzu06 commented Sep 11, 2020

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() {

$labels = array(
	'name' => _x( 'Send Notifiction', 'Post Type General Name', 'textdomain' ),
	'singular_name' => _x( 'Send Notifiction', 'Post Type Singular Name', 'textdomain' ),
	'menu_name' => _x( 'Send Notifiction', 'Admin Menu text', 'textdomain' ),
	'name_admin_bar' => _x( 'Send Notifiction', 'Add New on Toolbar', 'textdomain' ),
	'archives' => __( 'Send Notifiction Archives', 'textdomain' ),
	'attributes' => __( 'Send Notifiction Attributes', 'textdomain' ),
	'parent_item_colon' => __( 'Parent Send Notifiction:', 'textdomain' ),
	'all_items' => __( 'All Send Notifiction', 'textdomain' ),
	'add_new_item' => __( 'Add New Send Notifiction', 'textdomain' ),
	'add_new' => __( 'Add New', 'textdomain' ),
	'new_item' => __( 'New Send Notifiction', 'textdomain' ),
	'edit_item' => __( 'Edit Send Notifiction', 'textdomain' ),
	'update_item' => __( 'Update Send Notifiction', 'textdomain' ),
	'view_item' => __( 'View Send Notifiction', 'textdomain' ),
	'view_items' => __( 'View Send Notifiction', 'textdomain' ),
	'search_items' => __( 'Search Send Notifiction', 'textdomain' ),
	'not_found' => __( 'Not found', 'textdomain' ),
	'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
	'featured_image' => __( 'Featured Image', 'textdomain' ),
	'set_featured_image' => __( 'Set featured image', 'textdomain' ),
	'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
	'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
	'insert_into_item' => __( 'Insert into Send Notifiction', 'textdomain' ),
	'uploaded_to_this_item' => __( 'Uploaded to this Send Notifiction', 'textdomain' ),
	'items_list' => __( 'Send Notifiction list', 'textdomain' ),
	'items_list_navigation' => __( 'Send Notifiction list navigation', 'textdomain' ),
	'filter_items_list' => __( 'Filter Send Notifiction list', 'textdomain' ),
);
$args = array(
	'label' => __( 'Send Notifiction', 'textdomain' ),
	'description' => __( '', 'textdomain' ),
	'labels' => $labels,
	'menu_icon' => 'dashicons-format-chat',
	'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'author', 'comments', 'trackbacks', 'page-attributes', 'post-formats', 'custom-fields'),
	'taxonomies' => array(),
	'public' => true,
	'show_ui' => true,
	'show_in_menu' => true,
	'menu_position' => 5,
	'show_in_admin_bar' => true,
	'show_in_nav_menus' => true,
	'can_export' => true,
	'has_archive' => true,
	'hierarchical' => false,
	'exclude_from_search' => false,
	'show_in_rest' => true,
	'publicly_queryable' => true,
	'capability_type' => 'post',
);
register_post_type( 'sendnotifiction', $args );

}
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;
}

return $route;

}
add_filter( 'rest_route_for_term', 'my_plugin_rest_route_for_term', 10, 2 );

//

function rewrite_tuts_flush(){

create_vehicle_details();
flush_rewrite_rules();

}

?>`

@ivandoric

This comment has been minimized.

Copy link
Owner Author

@ivandoric ivandoric commented Sep 13, 2020

@hamzu06 I have no idea man, sorry.

@hamzu06

This comment has been minimized.

Copy link

@hamzu06 hamzu06 commented Sep 14, 2020

Thank You @ivandoric, my issue is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.