Include custom fields in feed
-
How can one include custom fields in the feed? In particular I want to include a video frame from youtube that is being added to the article as a custom field.
-
You should be able to use the
instant_articles_after_transform_posthook to manipulate the article after the basic transformation is made using the main post body.Something like:
use Facebook\InstantArticles\Elements\Video; add_action( 'instant_articles_after_transform_post', function ($ia_post) { $instant_article = $ia_post->instant_article; $post_id = $ia_post->get_the_id(); $video_url = get_post_meta( $post_id, 'video_url', true ); $instant_article->addChild( Video::create()->withURL($video_url) ); } );Does that work?
Please refer to the Instant Articles SDK documentation:
https://github.com/facebook/facebook-instant-articles-sdk-php/blob/master/docs/QuickStart.md
Hi, thanks for the response, but I’m a bit confused. Isn’t this example from a different plugin than the one I’m referring to?
Hi,
No, that should be right. This is how you do it on v2.0+
There was a big rewrite of the plugin on this version.
Thanks a lot diegoquinteiro! I have to admit I was wrong, I updated the plugin and the above code worked perfectly.
But, is there a way to add a child to the head of the children array?
I tried to use the ‘instant_articles_before_transform_post’ action but it didn’t worked.
I’m just trying to place the video on top of the content if that is possible.
I tried to change the children array but it is set as private.Oh forgot, problem solved!
How would you add a full YouTube Embed tag? Is there documentation for Video::create() that shows how this can be done? Or can you add it as a paragraph?
Cheers
Hey xscv2314 how did you add the video to the top of the content?
Does a YouTube URL work or do you need a link to an .mp4 file?Cheers
Also wondering how one might use
addChildto add nodes beforethe_content. xscv2314 – did you get it working?I was able to add the full Youtube tag with something like this using the SocialEmbed class instead.
use Facebook\InstantArticles\Elements\SocialEmbed; add_action( 'instant_articles_after_transform_post', function ($ia_post) { $instant_article = $ia_post->instant_article; $post_id = $ia_post->get_the_id(); $iframe_string = get_post_meta( $post_id, 'video_embed_code', true ); preg_match('/src="([^"]+)"/', $iframe_string, $match); $video_url = $match[1]; $instant_article->addChild( SocialEmbed::create()->withSource($video_url) ); } );However, I’m still trying to get the video to appear before the content.
Thanks jamestylerpatton, let me know if you find a way to get the video above the content.
jkoon
I was able to place the video above the content, but I had to edit one of the plugin files to get it to work, so it’s only a temporary fix until I find a better way.In the file, fb-instant-articles/class-instant-articles-post.php, I added:
do_action( 'instant_articles_before_article_content', $this );
on line 674, right after
$document = apply_filters( 'instant_articles_parsed_document', $document );.Also, you have to specify the width and the height when adding the video, so my full functions.php code to add the video is:
use Facebook\InstantArticles\Elements\SocialEmbed; function incl_video($ia_post) { $instant_article = $ia_post->instant_article; $post_id = $ia_post->get_the_id(); $iframe_string = get_post_meta( $post_id, 'video_embed_code', true ); if($iframe_string){ preg_match('/src="([^"]+)"/', $iframe_string, $match); $video_url = $match[1]; $instant_article->addChild( SocialEmbed::create()->withSource($video_url)->withWidth(640)->withHeight(390) ); } } add_action( 'instant_articles_before_article_content', 'incl_video' );hello all, i’m trying to achieve something similar, but when i place code like
instant_article->addChild(Paragraph::create())in functions.php, something breaks (ie: stuck loading indicator in the insant article metabox). – is functions.php the place where this code should go? and is there something more to this than adding
use Facebook\InstantArticles\Elements\SocialEmbed?
custom video field not display
The topic ‘Include custom fields in feed’ is closed to new replies.