The story is that I created a custom API route /wp-post-modal/v1/any-post-type
that pulls posts of any post type. Locally it works perfectly (MAMP), but on multiple production servers (different environments) it returns a 500 error.
API Route that works on local:
API Route Examples on Production (show that API route does exist):
- https://wp-post-modal.allureprojects.com/wp-json/wp-post-modal/v1
- https://spedform.com/wp-json/wp-post-modal/v1
Usage that should work on Production (but don't because of 500 error):
- https://wp-post-modal.allureprojects.com/wp-json/wp-post-modal/v1/any-post-type?slug=modal-content
- https://spedform.com/wp-json/wp-post-modal/v1/any-post-type?slug=privary-policy
I checked the nginx error logs on the server and they are empty.
Code for custom API route:
/**
* Register API Route: Query Any Post Type
*/
public function any_post_api_route() {
register_rest_route( $this->plugin_name . '/v1', '/any-post-type/', array(
'methods' => 'GET',
'callback' => array( $this, 'get_content_by_slug' ),
'args' => array(
'slug' => array(
'required' => false
)
)
) );
}
/**
*
* Get content by slug
*
* @param WP_REST_Request $request
*
* @return WP_REST_Response
*/
public function get_content_by_slug( WP_REST_Request $request ) {
WPBMap::addAllMappedShortcodes();
// get slug from request
$slug = $request['slug'];
// get title by slug
$return = get_page_by_path( $slug, ARRAY_A, array( 'page', 'post' ) );
// render shortcodes from Visual Composer
$return['post_content'] = apply_filters( 'the_content', $return['post_content'] );
$response = new WP_REST_Response( $return );
return $response;
}