{"id":3381,"date":"2026-03-30T16:29:10","date_gmt":"2026-03-30T14:29:10","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3381"},"modified":"2026-03-30T16:31:31","modified_gmt":"2026-03-30T14:31:31","slug":"how-to-link-automatically-youtube-videos","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=3381","title":{"rendered":"Automatically link youtube videos"},"content":{"rendered":"\n<p>You could try with a php function the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nfunction getYouTubeVideosByTag($tag, $maxResults = 6) {\n    $apiKey = '&#91;your youtube API key]';\n    $channelId = '&#91;your-youtube-channel-ID';\n\n    \/\/ Forza formato hashtag\n    $query = urlencode('#' . $tag);\n\n    $url = \"https:\/\/www.googleapis.com\/youtube\/v3\/search?part=snippet&amp;type=video&amp;order=date&amp;maxResults=$maxResults&amp;q=$query&amp;channelId=$channelId&amp;key=$apiKey\";\n\n    $response = file_get_contents($url);\n\n    if ($response === FALSE) {\n        return &#91;];\n    }\n\n    $data = json_decode($response, true);\n\n    $videos = &#91;];\n\n    if (!empty($data&#91;'items'])) {\n        foreach ($data&#91;'items'] as $item) {\n            if (!isset($item&#91;'id']&#91;'videoId'])) continue;\n\n            $videos&#91;] = &#91;\n                'title' =&gt; $item&#91;'snippet']&#91;'title'],\n                'videoId' =&gt; $item&#91;'id']&#91;'videoId'],\n                'description' =&gt; $item&#91;'snippet']&#91;'description'],\n                \/\/ 'thumbnail' =&gt; $item&#91;'snippet']&#91;'thumbnails']&#91;'medium']&#91;'url']\n            ];\n        }\n    }\n\n    return $videos;\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>Obviously you have to link this function in every web page you want links your videos, and in these pages you can call that function by a code like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nif (!empty($youtubetag)) {\n\n    if (is_array($youtubetag)) {\n        $query = implode(' OR ', array_map(fn($t) =&gt; '#' . $t, $youtubetag));\n        $videos = getYouTubeVideos($query, 5);\n    } else {\n        $videos = getYouTubeVideosByTag($youtubetag, 3);\n    }\n\n    if (!empty($videos)) {\n        ?&gt;\n\n        &lt;section class=\"youtube-videos\"&gt;\n        &lt;h2&gt;\ud83c\udf99\ufe0f Our podcast (YouTube)&lt;\/h2&gt;\n\n            &lt;ul&gt;\n                &lt;?php foreach ($videos as $video): ?&gt;\n                    &lt;li class=\"podcast\"&gt;\ud83c\udf99\ufe0f\n                        &lt;a href=\"https:\/\/www.youtube.com\/watch?v=&lt;?= $video&#91;'videoId'] ?&gt;\"&gt;&lt;strong&gt;&lt;?= htmlspecialchars($video&#91;'title']) ?&gt;&lt;\/strong&gt;&lt;\/a&gt;:\n                       &lt;?= htmlspecialchars(substr($video&#91;'description'], 0, 120), ENT_QUOTES | ENT_HTML5, 'UTF-8', false) ?&gt;\n                    &lt;\/li&gt;\n                &lt;?php endforeach; ?&gt;\n            &lt;\/ul&gt;\n        &lt;\/section&gt;\n\n        &lt;?php\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>Finally, you should define, possibly in the head of each php page, where you want to link youtube videos, the <strong>variable<\/strong> $youtubetag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$youtubetag=\"my tag\";\nor \n$youtubetag=\"&#91;'mytag1', 'mytag2']\";<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You could try with a php function the following: Obviously you have to link this function in every web page you want links your videos, and in these pages you can call that function by a code like this: Finally, you should define, possibly in the head of each php page, where you want to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=3381\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;Automatically link youtube videos&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[885,751,869,566],"tags":[1001,180,1000],"class_list":["post-3381","post","type-post","status-publish","format-standard","hentry","category-code","category-php","category-social","category-webmaster","tag-automatic-links","tag-tags","tag-youtube"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3381"}],"version-history":[{"count":4,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3381\/revisions"}],"predecessor-version":[{"id":3385,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3381\/revisions\/3385"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}