Skip to content

Instantly share code, notes, and snippets.

@nfriedly
Created September 19, 2018 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfriedly/e50be5bfb4487a3024299c65e4e7403f to your computer and use it in GitHub Desktop.
Save nfriedly/e50be5bfb4487a3024299c65e4e7403f to your computer and use it in GitHub Desktop.
Plugin to format WordPress posts created byt the DsgnWrks Instagram Importer
<?php
/*
Plugin Name: DsgnWrks Instagram Importer Hooks
Plugin URI: http://dsgnwrks.pro/plugins/dsgnwrks-instagram-importer
Description: Improvements to the Instagram Importer
Author URI: http://www.nfriedly.com
Author: nfriedly
Version: 0.0.0
*/
// initially based on https://gist.github.com/jtsternberg/b7c3b5371c6f639693b8f086859ad129
function format_instagram_post_remove_initial_image( $content ) {
return preg_replace( '/<p><img[^>]+></p>/', '', $content, 1 );
}
function format_instagram_post_insert_video( $content ) {
$content = format_instagram_post_remove_initial_image( $content );
$video_src = get_post_meta( get_the_ID(), 'instagram_video_url_standard_resolution', true );
if ( $video_src ) {
$content = '[video src="' . esc_url_raw( $video_src ) . '" /]' . $content ;
}
return $content;
}
function format_instagram_post_insert_carousel( $content ) {
$content = format_instagram_post_remove_initial_image( $content );
$carousel = '';
$post_id = get_the_ID();
$videos = get_post_meta( $post_id, 'instagram_video_url_standard_resolution', false );
foreach ( $videos as $video_src ) {
$carousel .= '[video src="' . esc_url_raw( $video_src ) . '" /]';
}
$featured_image_id = get_post_thumbnail_id( $post_id );
$pictures = get_post_meta( $post_id, 'instagram_image_id', false );
foreach ( $pictures as $attach_id ) {
if ( $attach_id !== $featured_image_id ) { // because my template already displays the featured image on the post page
$img_element = wp_get_attachment_image( $attach_id, 'full', false, array(
'class' => 'insta-image'
) );
$carousel .= '<p>' . $img_element . '</p>';
}
}
$content = $carousel . $content;
return $content;
}
function format_instagram_post( $content ) {
$type = get_post_meta( get_the_ID(), 'instagram_type', true );
if ( $type == 'video' ) {
return format_instagram_post_insert_video( $content );
}
if ( $type == 'carousel' ) {
return format_instagram_post_insert_carousel( $content );
}
if ( $type == 'image' ) {
// the template I'm using automatically shows this as the featured image
// you may want to disable this if you have a different template
return format_instagram_post_remove_initial_image( $content );
}
}
add_filter( 'the_content', 'format_instagram_post', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment