Skip to content

Instantly share code, notes, and snippets.

@woraperth
woraperth / wp-get-youtube-thumb.php
Last active August 3, 2022 20:39
[WordPress] Get Youtube video thumbnail from URL & Set as Featured Image
// Get Youtube URL
$yturl = 'https://www.youtube.com/watch?v=HhAKNSsb4t4';
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $yturl, $matches);
$video_id = $matches[1];
// Get Thumbnail
$file_headers = get_headers( 'http://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg' );
$is_404 = $file_headers[0] == 'HTTP/1.0 404 Not Found' || false !== strpos( $file_headers[0], '404 Not Found' );
$video_thumbnail_url = $is_404 ? 'http://img.youtube.com/vi/' . $youtube_id . '/maxresdefault.jpg' : 'http://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg';
@philipnewcomer
philipnewcomer / generate-unique-username.php
Last active February 11, 2023 19:05
Generate a unique username in WordPress
<?php
/**
* Recursive function to generate a unique username.
*
* If the username already exists, will add a numerical suffix which will increase until a unique username is found.
*
* @param string $username
*
* @return string The unique username.
*/