Skip to content
This repository has been archived by the owner. It is now read-only.

Render shortcodes for visual composer #2578

Closed
bradmsmith opened this issue Jul 2, 2016 · 15 comments
Closed

Render shortcodes for visual composer #2578

bradmsmith opened this issue Jul 2, 2016 · 15 comments
Labels

Comments

@bradmsmith
Copy link

@bradmsmith bradmsmith commented Jul 2, 2016

I'm trying to understand why the Visual Composer wordpress plugin does not work with WP-API (it returns shortcodes unrendered) when it does work with the JSON-API plugin (it returns shortcodes rendered).

In other words, why does WP-API not render some short codes? And if the Visual Composer author needs to make a change in his plugin, why does it work fine with JSON-API?

screen shot 2016-07-02 at 2 18 00 am

@adyrcz
Copy link

@adyrcz adyrcz commented Aug 3, 2016

This is by no means meant to be an answer, but I'm running into the same issue and thinking out what we need to fix...

Content gets saved in the database with the VC shortcodes embedded, so when the API pulls the content from your database, its pulling the raw data which contains the shortcodes.

There needs to be a translation mechanism that would translate the [VC shortcodes] to html before delivering the payload.

@deonvisser
Copy link

@deonvisser deonvisser commented Aug 10, 2016

I agree with adyrcz. There is no translation of a shortcode to it's html equivalent. Shortcode translation and interpretation happens in the plugin(php) that provides the shortcode and not html.

@BE-Webdesign
Copy link
Member

@BE-Webdesign BE-Webdesign commented Sep 8, 2016

Hi @bradmsmith,

Sorry for the delay in response and thank you for using the WP REST API. Shortcode rendering is supported in the WordPress REST API, but for whatever reason in VC their shortcodes are not rendering. I would suggest taking this issue up with Visual Composer to see why this is not working with the API. I am going to close out this issue but if you get more information about the problem post here and I will re open the ticket!

Thank you.

@bradmsmith
Copy link
Author

@bradmsmith bradmsmith commented Sep 9, 2016

Solved

To register the VC shortcodes the following must be called:

WPBMap::addAllMappedShortcodes();

Here is an example how to render the VC shortcodes on the content of a post:

add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'compasshb_do_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function compasshb_do_shortcodes( $object, $field_name, $request )
{
   WPBMap::addAllMappedShortcodes(); // This does all the work

   global $post;
   $post = get_post ($object['id']);
   $output['rendered'] = apply_filters( 'the_content', $post->post_content );

   return $output;
}

Reference: CompassHB/web#67 (comment)

@pourri-dixit
Copy link

@pourri-dixit pourri-dixit commented Oct 5, 2016

yep! cool, my problem is resolved

@ddhara
Copy link

@ddhara ddhara commented Mar 20, 2017

Hi, bradmsmith

Can you please tell me that in which file can I put your code? I am new for wordpress

@codesharpdev
Copy link

@codesharpdev codesharpdev commented Mar 27, 2017

Hello @ddhara ,

Just add the following code to File: wp-includes/rest-api.php at the bottom

/**
 * Modify REST API content for pages to force
 * shortcodes to render since Visual Composer does not
 * do this
 */
add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'compasshb_do_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function compasshb_do_shortcodes( $object, $field_name, $request )
{
   WPBMap::addAllMappedShortcodes(); // This does all the work

   global $post;
   $post = get_post ($object['id']);
   $output['rendered'] = apply_filters( 'the_content', $post->post_content );

   return $output;
}
@kadamwhite
Copy link
Contributor

@kadamwhite kadamwhite commented Mar 27, 2017

@ddhara @codesharpdev please never edit WordPress core files directly: this will cause your site to break every time you upgrade WordPress, and could potentially cause more serious errors. I would instead recommend putting this into a PHP file in the directory wp-content/mu-plugins/, or making it into a regular plugin of its own.

@kadamwhite
Copy link
Contributor

@kadamwhite kadamwhite commented Mar 27, 2017

(But as noted above I would also open an issue with the plugin developer to ask them to support the rest api)

@MrVibe
Copy link

@MrVibe MrVibe commented Apr 17, 2017

This most likely would not work with VC latest version. As I am not sure how you'll get the list of scripts which are dynamically included in VC as VC loads scripts at wp_enqueue_scripts runs at template_redirect. So by this approach you're only getting the HTML but not the associated scripts and styles.

@puntowebapp
Copy link

@puntowebapp puntowebapp commented Apr 27, 2017

I have the same Problem.... How to fix this?? I use this plugin https://it.wordpress.org/plugins/json-rest-api/
schermata 2017-04-27 alle 17 43 08

@jezmck
Copy link

@jezmck jezmck commented May 5, 2017

@puntowebapp Did you see that you sholdn't really be using the plugin anymore?

Notice: This is the deprecated Version 1 of the WP REST API. It’s no longer supported beyond security fixes. Please consider WP REST API v2 for your website...

@igalencar
Copy link

@igalencar igalencar commented Mar 16, 2018

IM using rest api native to wordpress and i till cannot get visual composer shortcodes rendered

@ghost
Copy link

@ghost ghost commented Jul 21, 2018

@bradmsmith , It doesn't work at all .. i need to render the page content any recommendations ?

@nessunluogo
Copy link

@nessunluogo nessunluogo commented Jul 23, 2018

Hi all! I need to get this working on the post-type "Events" brought by The Events Calendar plugin.
It uses WP REST API V1 and I'm not sure if Brad's code doesn't work becouse of this or becouse of post-type. Any help?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet