$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>shortcode - Why 'do_shortcode' doesn't work in a REST request?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

shortcode - Why 'do_shortcode' doesn't work in a REST request?

matteradmin9PV0评论

Why do_shortcode doesn't work in a REST call ?

In the example below, do_shortcode exists, but the shortcode isn't interpreted.

Route declaration

/*
       Adding a route with callback is interpret_shortcode function
*/
add_action( 'rest_api_init', function ()
{
       register_rest_route(
              'test',
              '/someContent',
              array(
                 'methods' => 'GET',
                 'callback' => 'interpret_shortcode'
              )
       );
} );

Shortcode call

function interpret_shortcode( $data )
{
   $content;

   if ( function_exists( 'do_shortcode' ) )  
   {
           $content = do_shortcode("[et_pb_text] abc [/et_pb_text]");
   } 
   else {
          $content = "do_shortcode is missing";
   }

   return array('content'=>$content); //Output "[et_pb_text] abc [/et_pb_text]"
}

In a REST call, are we in the same situation as the one describe here about Ajax request ?

Why do_shortcode doesn't work in a REST call ?

In the example below, do_shortcode exists, but the shortcode isn't interpreted.

Route declaration

/*
       Adding a route with callback is interpret_shortcode function
*/
add_action( 'rest_api_init', function ()
{
       register_rest_route(
              'test',
              '/someContent',
              array(
                 'methods' => 'GET',
                 'callback' => 'interpret_shortcode'
              )
       );
} );

Shortcode call

function interpret_shortcode( $data )
{
   $content;

   if ( function_exists( 'do_shortcode' ) )  
   {
           $content = do_shortcode("[et_pb_text] abc [/et_pb_text]");
   } 
   else {
          $content = "do_shortcode is missing";
   }

   return array('content'=>$content); //Output "[et_pb_text] abc [/et_pb_text]"
}

In a REST call, are we in the same situation as the one describe here about Ajax request ?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Apr 27, 2016 at 13:10 Jordane CUREJordane CURE 111 silver badge5 bronze badges 8
  • You have an extra space in the do_shortcode inside the function_exists. Is that a typo in your question or is it your actual code? – RRikesh Commented Apr 27, 2016 at 13:45
  • OMG - so easy to fix ! – Jordane CURE Commented Apr 27, 2016 at 13:50
  • So i edit the question a bit because i'm stuck in the next step – Jordane CURE Commented Apr 27, 2016 at 13:54
  • 1 Please clarify your problem. Does is it work now or is the problem still there? – flomei Commented Apr 27, 2016 at 14:14
  • @RRikesh help, but the probleme still there, do_shortcode doesn't do shortcode. – Jordane CURE Commented Apr 27, 2016 at 14:19
 |  Show 3 more comments

2 Answers 2

Reset to default 1

Solution

I find a workaround for this issue. I post it for posterity.

This is an issue, several issue, between divi and REST Api.

In the exemple bellow, register_rest_field is used instead of register_rest_route, but the fix is valid for both method.

This solution could not be very futureproof. But, at least, their is no modification inside Divi Builder or Rest API.

/*
     Edit Rest call
*/
add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'duo_get_divi_content',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function duo_get_divi_content( $object, $field_name, $request )
{
   //Set is_singular to true to ovoid "read more issue"
   //Issue come from is_singular () in divi-builder.php line 73
   global $wp_query;
   $wp_query->is_singular = true;


   //Set divi shortcode
   //The 2 function bellow are define in 'init' but they are call in 'wp'
   //REST Api exit after 'parse_request' hook, it's before 'wp' so divi's shortcode are not set
   et_builder_init_global_settings ();
   et_builder_add_main_elements ();


   //Define $post, if not defined, divi will not add outter_content and inner_content warper
   //Issue come from get_the_ID() in divi-builder.php line 69
   global $post;
   $post = get_post ($object['id']);

   //Apply the_content's filter, one of them interpret shortcodes
   $output =  apply_filters( 'the_content', $post->post_content );

   return $output;
}

Availability of shortcodes defined by plugins at any point of the code is undefined expect for (and even that is a maybe) singular content.

In most cases outside of that context you better call the function implementing it with the applicable parameters instead of "parsing" the shortcode.

Post a comment

comment list (0)

  1. No comments so far