How to output cmb2 wysiwyg by using timber

I am using cmb2 to create the custom post type, and I have a custom post type called legacy_cycle, which contains several wysiwyg fileds.

Than I am using Timber as theme tool to display the input. However, the wysiwyg field could not work properly. It is fine if it only contains text or image, but it only output the following information when I insert a youtube video in the wysiwyg editor under the custom post type, but the output from the native wordpress editor is fine.

Read More
 " https://www.youtube.com/watch?v=MS91knuzoOA"

I tried to use post.get_filed(‘my_wysiwig’), but it did not work.

I am wondering how I could output the field correctly? much appreciated!

Related posts

3 comments

  1. So, I figured it out through this post..Applying oembed filters to WYSIWYG field

    My solution by using timber/twig is to get the data in the single.php, and here is the code:

    $post_meta = get_post_meta(get_the_ID(),'my_wysiswg', true);
    $post_meta = $wp_embed->autoembed( $post_meta );
    $post_meta = $wp_embed->run_shortcode( $post_meta );
    $post_meta = do_shortcode( $post_meta );
    $post_meta = wpautop( $post_meta );
    $post->my_wysiswyg = $post_meta;
    

    Then I can print the video in the single-custom-post-type.twig by using {{post.my_wysiswyg}}

  2. It looks like you just need to process shortcodes in that field. This should convert those into actual YouTube videos:

    {{ post.get_field('my_wysiswyg') | shortcodes }}
    
  3. I use CMB2 extensively with Timber and my usual approach is to extend TimberPost and add in methods for getting meta data. For a wysiwyg field, something like this:

    class CustomPost extends TimberPost {
    
      public function my_wysiswg(){
    
        $metadata = get_post_meta($this->ID, 'my_wysiswg', true);
    
        if ($metadata){
          return apply_filters('the_content', $metadata);
        }
    
      }
    
    }
    

    You can specify which class Timber will use with the second parameter of Timber::get_posts

Comments are closed.