Support » Plugin: Instant Articles for WP » Modifying the output by filters

  • Hi,

    In the readme file it says “There are a number of filters available in the plugin for modifying the output.” Where is the docs about the filters, possibilities, limitations etc?

    Ex. Im trying to add a video as cover instead of a featured image, and I would like to read about how I can to that, without the need to do a closer look in plugin core files to find my answers.

    Without proper documentations and examples(I can read about FB IA guidelines at FB, but plugin documentation), the use of this plugin is almost wasted, as In many cases you will need to add custom content along with the default content block.

    https://wordpress.org/plugins/fb-instant-articles/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Same here. I guess they didn’t had time yet to create a full documentation on this.

    I’m checking the plugin code looking for filters. There is one right before the featured image is returned called instant_articles_featured_image that I guess would allow to change that output. Haven’t tested it yet.

    Also there is a get_cover_media function that seems to exist exactly to give the possibility of inserting a video as cover, but I doesn’t get called from anywhere on the code :/

    Just started working with this, I’ll post an update if I discover anything else.

    Besides the lack of documentation, the plugin seems really good and complete.

    The instant_articles_featured_image filter works ok to customize the image header.

    Something like this:

    function set_cover($cover_media,$post_id) {
    $image_data = array (
    			'src' => YOUR_IMAGE_URL,
    			'caption' => 'YOUR_CAPTION',
    		);
    return $image_data;
    }
    add_filter('instant_articles_featured_image','set_cover',100,9);

    Still working on getting a video there. Doesn’t work if you enter a video url in the array, because it will result in a <img> with the video on the source.

    I´v figured out that cover_media function doesn’t handle video. I´m doing some testing on extending the following code in function to_instant_article()

    $cover = $this->get_the_featured_image();
    		if ( $cover['src'] ) {
    			$image = Image::create()->withURL( $cover['src'] );
    			if ( isset( $cover['caption'] ) && strlen( $cover['caption'] ) > 0 ) {
    				$image->withCaption(
    				    Caption::create()->withTitle( $cover['caption'] )
    				);
    			}
    
    			$header->withCover( $image );

    to do a check if my custom video is set, otherwise return back to featured image.

    Note: The only way i´v seen video support in the plugin is via a shortcode, instant_articles_shortcode_handler_video

    I managed to do just that yesterday (couldn’t post it then)

    This requires to modify the plugin files, which is not the best thing because on the next update they will be replaced. So very careful when that happens.

    The file to change is class-instant-articles-post.php in the plugins folder.

    First we have to add the Video class at the top of the file (where all the other classes are called). So, we have to add this:

    use Facebook\InstantArticles\Elements\Video;

    Then, we go to the to_instant_article function, in find the part of the code that André B. just posted.

    $cover = $this->get_the_featured_image();
    		if ( $cover['src'] ) {
    			$image = Image::create()->withURL( $cover['src'] );
    			if ( isset( $cover['caption'] ) && strlen( $cover['caption'] ) > 0 ) {
    				$image->withCaption(
    				    Caption::create()->withTitle( $cover['caption'] )
    				);
    			}
    
    			$header->withCover( $image );
    }

    And we replace it with this:

    $cover = $this->get_the_featured_image();
    		if ( $cover['src'] ) {
    			if ($cover['type'] == 'video') {
    				//$image = $cover['src'];
    				$image = Video::create()->withURL( $cover['src'] )->enableComments()->enableLike();
    			} else {
    				$image = Image::create()->withURL( $cover['src'] );
    			}
    			if ( isset( $cover['caption'] ) && strlen( $cover['caption'] ) > 0 ) {
    				$image->withCaption(
    				    Caption::create()->withTitle( $cover['caption'] )
    				);
    			}
    
    			$header->withCover( $image );
    		}

    So, what it does is give you the option to add a video as cover. I added the enableComments and enableLike options, you can remove that if you don’t want it.

    But this is not enought, since the plugin will still only use the featured image as cover. That makes makes sense, since WordPress doesn’t have the option to use a video as thumbnail, so I will asume that you have some custom code to add that in your post.

    So, in functions.php of our theme, we add this:

    function set_cover($cover_media,$post_id) {
    		// Cover image
      		$cover = HERE YOU HAVE TO GET YOU ATTACHMENT OBJECT, IMAGE OR VIDEO
    
    		if (strpos($cover['mime_type'], 'image') !== false) {
    			$url = $cover['url'];
    			$type = 'image';
    		} else if (strpos($cover['mime_type'], 'video') !== false) {
    			$url = $cover['url'];
    			$type = 'video';
    		}
    		$image_data = array (
    			'type' => $type,
    			'src' => $url,
    			'caption' => ''
    		);
    		return $image_data;
    	}
    	add_filter('instant_articles_featured_image','set_cover',100,9);

    Here we hook on the ‘instant_articles_featured_image’ filter that the plugin has, and we create our own $image_data array. You will notice a “type” field there. This doesn’t existe in the plugin’s array. This is added to detect whether we are using an image or a video.

    Hope this helps.

    It would be cool there could be somehting like that in a future plugin update, so we can hook to the instant_articles_featured_image filter and just output a video

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Modifying the output by filters’ is closed to new replies.