Ecommerce Shopify WordPress Discussion

WordPress shortcode should return Instagram, YouTube, and Twitch posts sorted by date

I am trying to make a shortcode that takes my Twitch, YouTube and Instagram posts all in one place and sorts them by the date posted, but for some reason it doesn't work. I will comment out the credentials, but I can ensure that the problem lies elsewhere, because I have tested them individually and gotten them to work individually. <?php /* Plugin Name: Social media crawler Description: Version: 1.0 Author: License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Domain Path: /languages */ function fetch_youtube_videos($atts) { // Extract shortcode attributes $atts = shortcode_atts(array( 'channel_id' => '***', // Default channel ID (if any) 'max_results' => 5, // Default number of videos to show 'date' => '2010-01-01', // Default starting date in yyyy-mm-dd format ), $atts); $api_key = '***'; $publishedAfter = $atts['date'] . 'T00:00:00Z'; $api_url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' . $atts['channel_id'] . '&maxResults=' . $atts['max_results'] . '&publishedAfter=' . $publishedAfter . '&order=date&type=video&key=' . $api_key; // Make the API request $response = wp_remote_get($api_url); // Check for error in the response if (is_wp_error($response)) { return []; // Return an empty array in case of error } // Decode the response $body = wp_remote_retrieve_body($response); $data = json_decode($body); // Check if data items exist if (!isset($data->items)) { return []; // Return an empty array if no videos are found } // Initialize the posts array $posts = []; // Loop through each video and append to the posts array foreach ($data->items as $item) { $title = $item->snippet->title; $video_id = $item->id->videoId; $video_url = 'https://www.youtube.com/watch?v=' . $video_id; $embed_url = 'https://www.youtube.com/embed/' . $video_id; // URL for embedding $publishedAt = $item->snippet->publishedAt; // Video publication date // Construct the HTML content for the video $html = '<div class="youtube-video">' . '<iframe width="560" height="315" src="' . esc_url($embed_url) . '" title="' . esc_attr($title) . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>' . '<p><a href="' . esc_url($video_url) . '" target="_blank">' . esc_html($title) . '</a></p>' . '</div>'; // Add to the posts array $posts[] = [ 'date' => $publishedAt, 'html' => $html ]; } // Return the array of posts return $posts; } function fetch_instagram_posts($atts) { // Extract shortcode attributes $atts = shortcode_atts([ 'limit' => 10, // Default number of Instagram posts to show ], $atts); $fields = "id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username"; $token = "*"; $limit = $atts['limit']; $json_feed_url = "https://graph.instagram.com/me/media?fields={$fields}&access_token={$token}&limit={$limit}"; $json_feed = @file_get_contents($json_feed_url); if ($json_feed === FALSE) { return []; // Return an empty array in case of error } $contents = json_decode($json_feed, true); $posts = []; foreach ($contents["data"] as $post) { $username = isset($post["username"]) ? $post["username"] : ""; $caption = isset($post["caption"]) ? $post["caption"] : ""; $media_url = isset($post["media_url"]) ? $post["media_url"] : ""; $permalink = isset($post["permalink"]) ? $post["permalink"] : ""; $media_type = isset($post["media_type"]) ? $post["media_type"] : ""; $timestamp = isset($post["timestamp"]) ? $post["timestamp"] : ""; // Construct the HTML content for the post $html = "<div class='ig_post_container'><div>"; if ($media_type == "VIDEO") { $html .= "<video controls style='width:100%; display: block !important;'><source src='{$media_url}' type='video/mp4'>Your browser does not support the video tag.</video>"; } else { $html .= "<img src='{$media_url}' />"; } $html .= "</div><div class='ig_post_details'><div><strong>@{$username}</strong> {$caption}</div><div class='ig_view_link'><a href='{$permalink}' target='_blank'>View on Instagram</a></div></div></div>"; // Add to the posts array $posts[] = [ 'date' => $timestamp, 'html' => $html ]; } // Return the array of posts return $posts; } function fetch_twitch_videos($atts) { // Extract shortcode attributes $atts = shortcode_atts([ 'date' => '1970-01-01', // Use a very old date as default to ensure all clips are shown unless specified ], $atts); $clientID = '*'; $clientSecret = '*'; $tokenUrl = 'https://id.twitch.tv/oauth2/token'; // Parameters for the token request $params = [ 'client_id' => $clientID, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials', ]; // Initialize cURL session for the token request $ch = curl_init($tokenUrl); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the token request $response = curl_exec($ch); if (curl_errno($ch) || ($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) !== 200) { curl_close($ch); return []; // Return an empty array in case of error } curl_close($ch); $tokenData = json_decode($response, true); if (!isset($tokenData['access_token'])) { return []; // Return an empty array if token not obtained } // Use the obtained access token to make the actual API request $accessToken = $tokenData['access_token']; $broadcasterUserId = '*'; // Example broadcaster's user ID $videoClipsUrl = "https://api.twitch.tv/helix/clips?broadcaster_id=$broadcasterUserId"; $headers = [ 'Authorization: Bearer ' . $accessToken, 'Client-Id: ' . $clientID, ]; // Initialize cURL session for the API request $ch = curl_init($videoClipsUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the API request $response = curl_exec($ch); if (curl_errno($ch) || ($httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) !== 200) { curl_close($ch); return []; // Return an empty array if the request failed } curl_close($ch); $videoClipsData = json_decode($response, true); if (!isset($videoClipsData['data']) || empty($videoClipsData['data'])) { return []; // Return an empty array if no videos are found } // Prepare posts array $posts = array_map(function ($clip) { $clipDate = $clip['created_at']; $html = '<iframe src="' . $clip['embed_url'] . '&parent=*" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>'; return [ 'date' => $clipDate, 'html' => $html, ]; }, $videoClipsData['data']); // Filter posts based on the 'date' attribute $minDateTimestamp = strtotime($atts['date']); $filteredPosts = array_filter($posts, function ($post) use ($minDateTimestamp) { return strtotime($post['date']) >= $minDateTimestamp; }); return array_values($filteredPosts); // Re-index the array and return } function social_wall_shortcode($atts) { ob_start(); // Assuming fetching functions have been modified to return arrays $youtubePosts = fetch_youtube_videos($atts); $instagramPosts = my_instagram_feed_shortcode($atts); $twitchPosts = display_twitch_videos($atts); // Aggregate posts $allPosts = array_merge($youtubePosts, $instagramPosts, $twitchPosts); // Sort posts by date usort($allPosts, function($a, $b) { return strtotime($a['date']) <=> strtotime($b['date']); }); // Generate HTML output echo '<div class="social-wall-container">'; foreach ($allPosts as $post) { echo $post['html']; // Each post's HTML content } echo '</div>'; $content = ob_get_clean(); return $content; } // Register the modified shortcode with WordPress add_shortcode('social_wall', 'social_wall_shortcode');
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.