Last active
November 5, 2025 14:54
-
-
Save modemlooper/63609246f82a9372bb4ce890625741d0 to your computer and use it in GitHub Desktop.
Dynamic iframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 1. Add custom query variables to make them publicly available. | |
| */ | |
| function appp_query_vars( $vars ) { | |
| // Variable name changed to 'id' | |
| $vars[] = 'id'; | |
| return $vars; | |
| } | |
| add_filter( 'query_vars', 'appp_query_vars' ); | |
| /** | |
| * 2. Create the rewrite rule to map the URL to the query variable. | |
| * The rule maps: yoursite.com/video-embed/?id=1234 | |
| * to: index.php?id=something | |
| */ | |
| function appp_rewrite_rule() { | |
| add_rewrite_rule( | |
| // The regex pattern for the public URL structure | |
| '^video-embed/([^/]*)/?$', | |
| // The internal query string maps to the new variable | |
| 'index.php?id=$matches[1]', | |
| 'top' | |
| ); | |
| // NOTE: You must flush permalinks after adding this code! | |
| } | |
| add_action( 'init', 'appp_rewrite_rule' ); | |
| /** | |
| * 3. Handle the request and load the custom PHP content/template. | |
| */ | |
| function appp_template_loader( $template ) { | |
| // Check if the 'youtube_iframe' variable (which holds the video ID) is set | |
| $video_id = get_query_var( 'id' ); | |
| if ( $video_id ) { | |
| // Sanitize the ID to ensure it's safe for use in the URL | |
| $safe_video_id = esc_attr( $video_id ); | |
| // Generate the dynamic content | |
| ?> | |
| <!DOCTYPE html> | |
| <html <?php language_attributes(); ?>> | |
| <head> | |
| <meta charset="<?php bloginfo( 'charset' ); ?>"> | |
| <title></title> | |
| <?php wp_head(); ?> | |
| </head> | |
| <body <?php body_class(); ?>> | |
| <div style="width: 100%; margin: 0 auto; aspect-ratio: 16 / 9;"> | |
| <iframe | |
| width="100%" | |
| height="100%" | |
| src="https://www.youtube.com/embed/<?php echo $safe_video_id; ?>" | |
| frameborder="0" | |
| allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" | |
| allowfullscreen | |
| title="Dynamic Video Embed" | |
| ></iframe> | |
| </div> | |
| <?php wp_footer(); ?> | |
| </body> | |
| </html> | |
| <?php | |
| exit(); // Stop further WordPress execution | |
| } | |
| return $template; // Return the default template if the variable isn't set | |
| } | |
| add_filter( 'template_redirect', 'appp_template_loader' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment