• Hi

    I came across this api for WordPress and it has a rest api endpoint for returning a rendered shortcode. https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/shortcodes/render/

    Is there a way to achieve the same response parameters not using this api? as I have a self hosted site and cannot use this api.

    I cant seem to find the repository or any code to investigate how they go about this. I am interested in returning a shortcode and the scripts and styles used.

    any help would be great

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s a way, but I don’t think there is an equivalent API route in self-hosted WP. The way then is to create your own route/endpoint. Your added callback for the endpoint you create could take the passed shortcode and render it with do_shortcode(). Incorporate the returned shortcode render into a response object to be fully RESTful, though you could in reality return any sort of JSON string you want provided your app knows how to handle it.

    Thread Starter coxy17

    (@coxy17)

    Ok great thanks. I tried returning the shortcode using do_shortcode() on a custom endpoint but the output does not include the enqueued css/js. Just the html. Also i have no idea how to find out what css/js is required/linked to the shortcode. Any ideas?

    Moderator bcworkz

    (@bcworkz)

    If there is no inline script or CSS in the output, the shortcode is assuming whatever its parent theme or plugin enqueues for all pages has what it needs. The same would presumably already be on any other page using the shortcode obtained through an API. But if you’re using shortcode output outside of a page context, you’d need to load for yourself whatever JS/CSS the plugin/theme would have normally enqueued for pages.

    To determine what had been enqueued, search the source code of the parent theme/plugin for occurrences of wp_enqueue_script() and wp_enqueue_style(). You might be able to instead examine a page’s source HTML for scripts or styles loaded from the parent theme’s/plugin’s folder structure. This isn’t always so straight forward due to script loaders and page optimizations.

    Thread Starter coxy17

    (@coxy17)

    Great thanks for the explanation. I was just curious how the guys who built the api endpoint would go about it. I guess i can look up the wp_enqueue_script() etc in the plugins source code, i guess this could get messy?. I just assumed there might have been a clean way of grabbing the CSS/JS.

    Moderator bcworkz

    (@bcworkz)

    The API devs were already aware of their CSS/JS dependencies. Rooting out what those are after the fact can be challenging in complex plugins. Learning what gets enqueued is the best way towards a working solution, but the enqueued resources may be far more code than is needed by the shortcode. Attempting to tweeze out what is actually needed could be a very tedious and time consuming task.

    I’m assuming the devs followed the recommended ways to load external resources by enqueuing. If they deviated, searching for enqueue code isn’t going to be successful.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get a rendered shortcode’ is closed to new replies.