0
votes

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):

Usage that should work on Production (but don't because of 500 error):

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;

}
2

2 Answers

0
votes

I realized that I needed to check if Visual Composer was installed instead of just assuming WPBMap::addAllMappedShortcodes();

My adjusted code looks like:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
    WPBMap::addAllMappedShortcodes();
}
0
votes

Try updating the first line in get_content_by_slug() to this:

if ( class_exists( 'WPBMap' ) ) {
    WPBMap::addAllMappedShortcodes();
}

My guess is you have the WPBMap library or plugin locally, but it's not available in the production sites.