Swapping src for href using php?

I have a custom field that I use for posting iframe content.

<?php echo get_post_meta( get_the_ID(), 'Embed_Code', true ) ?>

What I'm trying to do is grab the src of a <iframe> and swap it into a href. I've done this successfully with Jquery, but it's slowing down my page because:

-I have multiple tags doing this.

-It's also moving elements around the page and placing them back to where they're suppose to be, due to the iframe loading and then getting removed.

Here's an example

How would I do this with php, so I don't get loading problems, or slow down the page loading down?

*note what is being posted into the custom field is not in my control.

Thanks as always!

  • Tyler Postle

    Hey Corbin,

    hope you're doing well today and thanks for your question!

    Sorry, I'm not sure I completely understand what you're after here. I see the example you have and when the link is pressed it brings up the video. Whenever an iframe is placed on the page you want to swap it into a link instead? and you want to do this with php now instead of jquery to improve load times?

    This level of coding is a bit above my expertise, so I will flag SLS(coding experts) on this one for you.

    Look forward to hearing back! Any further information you can add will be helpful :slight_smile:

    Cheers,
    Tyler

  • aristath

    Hello there @Corbin, I hope you're well today!

    I have not tested this but it should work...
    First, add this function to your theme's functions.php file:

    function custom_url_from_ifame( $iframe ) {
    
    	// Convert to array
    	$iframe_properties = explode( $iframe, ' ' );
    
    	$url = false;
    	foreach ( $iframe_properties as $property ) {
    
    		if ( ! $url ) {
    			if ( 0 === strpos( $property, 'src' ) ) {
    				// Remove ' from the end
    				$url = rtrim( $property, '\'');
    				// Remove " from the end
    				$url = rtrim( $url, '"');
    				// Remove everything from the beginning before //
    				$url = substr( $url, '//' );
    			}
    		}
    	}
    
    	return $url;
    
    }

    Then, when you want to extrapolate the URL from an iframe you can do it like this:

    <?php echo custom_url_from_ifame( get_post_meta( get_the_ID(), 'Embed_Code', true ) ); ?>

    I hope that helps!

    Cheers,
    Ari.

    • Corbin

      Thanks ever so much for the help!

      I added the code you said and called the function between a p tag trying to fiddle with it to get to work. I first used this:

      <p><?php echo custom_url_from_ifame( get_post_meta( get_the_ID(), 'Embed_Code', true ) ); ?></p>

      I got an empty <p></p>

      Then I figured the custom field needed to be echoed and built a custom function that just echos the custom field:

      function custom_submit_iframe(){
      	echo get_post_meta( get_the_ID(), 'Embed_Code', true );
      }

      The called this in my template file:
      <?php echo custom_url_from_ifame( custom_submit_iframe() ); ?>

      And I got this warning:

      Warning: explode(): Empty delimiter in C:\xampp\htdocs\wordpress\wp-content\themes\reverie-child-master\functions.php on line 155

      Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wordpress\wp-content\themes\reverie-child-master\functions.php on line 158

      Thank you so much for your help again!

  • Vinod Dalvi

    Hi Corbin,

    Thank you for your reply.

    Try adding the following function in the functions.php file of your child theme

    function custom_url_from_iframe( $iframe ) {
    
    	$url = '';
    	preg_match('/src="([^"]+)"/', $iframe, $match);
    	if( isset($match[1]) && $match[1] ){
    		$url = $match[1];
    	}
    
    	return $url;
    }

    And call it as following.

    <?php echo custom_url_from_iframe( get_post_meta( get_the_ID(), 'Embed_Code', true ) ); ?>

    I have tested it and found it's working but if you face any issue with it feel free to ask it.

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.