Created
April 28, 2019 15:14
-
-
Save oterox/bf867de427ad4531567ed509a9131ce3 to your computer and use it in GitHub Desktop.
WP Rest API cpt
This file contains 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
/** | |
* Add REST API support to an already registered post type. | |
*/ | |
add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 ); | |
function my_post_type_args( $args, $post_type ) { | |
if ( 'destination' === $post_type ) { | |
$args['show_in_rest'] = true; | |
// Optionally customize the rest_base or rest_controller_class | |
$args['rest_base'] = 'destinations'; | |
$args['rest_controller_class'] = 'WP_REST_Posts_Controller'; | |
} | |
return $args; | |
} | |
function bce_get_destinations_items() { | |
global $sitepress; | |
//switch to the english language | |
$sitepress->switch_lang('es'); | |
$args = array ( | |
'post_status' => 'publish', | |
'post_type' => array('destination'), | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC', | |
'suppress_filters' => false | |
); | |
$items = array(); | |
if ( $posts = get_posts( $args ) ) { | |
foreach ( $posts as $post ) { | |
$items[] = array( | |
'id' => $post->ID, | |
'destino' => $post->post_title, | |
); | |
} | |
} | |
$sitepress->switch_lang(ICL_LANGUAGE_CODE); | |
return $items; | |
} | |
add_action( 'rest_api_init', 'pluginname_register_api_endpoints' ); | |
function pluginname_register_api_endpoints() { | |
register_rest_route( 'bce/v3', '/destinations', array( | |
'methods' => 'GET', | |
'callback' => 'bce_get_destinations_items', | |
) ); | |
} | |
add_action( 'woocommerce_api_loaded', function(){ | |
include_once( 'class-wc-api-custom.php' ); | |
}); | |
add_filter( 'woocommerce_api_classes', function( $classes ){ | |
$classes[] = 'WC_API_Custom'; | |
return $classes; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment