• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

Manipulating wordpress plugin, regex mod possibly?

I am trying to modify embediframe plugin for word press. currently this plugin allows the users to enter [iframe url height width] in a page or post and it displays the iframe.

I would like to modify this plugin so the user only has to enter [url] and the iframe displays.

I am not going to be modifying the height and width of the iframe so these parameters are not neccessary nor desired. attached is the plugin code. i am not familiar enough with the preg_replace_callback function or regex or php for that matter.

any help will be greatly appreciated.

thanks!
<?php
include (dirname (__FILE__).'/plugin.php');
 
class EmbedIframe extends EmbedIframe_Plugin
{
	function EmbedIframe ()
	{
		$this->register_plugin ('embediframe', __FILE__);
		
		$this->add_filter ('the_content');
		$this->add_action ('wp_head');
	}
	
	function wp_head ()
	{
		
	}
	
	function replace ($matches)
	{
		$tmp = strpos ($matches[1], ' ');
		if ($tmp)
		{
			// Because the regex is such a nuisance
			$url  = substr ($matches[1], 0, $tmp);
			$rest = substr ($matches[1], strlen ($url));
			
			$width  = 800;
			$height = 875;
 
				$parts = array_values (array_filter (explode (' ', $rest)));
				$width = $parts[0];
				unset ($parts[0]);
				$height = implode (' ', $parts);
                       
			
			return $this->capture ('iframe', array ('url' => $url, 'width' => $width, 'height' => $height));
		}
		
		return '';
	}
 
	function the_content ($text)
	{
	  return preg_replace_callback ("@(?:<p>\s*)?\[iframe\s*(.*?)\](?:\s*</p>)?@", array (&$this, 'replace'), $text);
 
	}
}
 
$embediframe = new EmbedIframe;
?>

Open in new window

0
thtate
Asked:
thtate
  • 4
1 Solution
 
James RodgersWeb Applications DeveloperCommented:
try replacing the embediframe.php with this

lets you set a default max size, so users can make it smaller if they want, but if no size is specified or  a size larger than you will allow it eill default to the max size


<?php
/*
Plugin Name: Embed Iframe
Plugin URI: http://blog.deskera.com/wordpress-plugin-embed-iframe
Description: Allows the insertion of code to display an external webpage within an iframe. The tag to insert the code is: <code>[iframe url width height]</code>
Version: 1.0
Author: Deskera
Author URI: http://deskera.com

1.0   - Initial release
*/


$iMaxHeight = 600;
$iMaxWidth      = 300;


include (dirname (__FILE__).'/plugin.php');

class EmbedIframe extends EmbedIframe_Plugin
{
      function EmbedIframe ()
      {
            $this->register_plugin ('embediframe', __FILE__);
            
            $this->add_filter ('the_content');
            $this->add_action ('wp_head');
      }
      
      function wp_head ()
      {
            
      }
      
      function replace ($matches)
      {
            $tmp = strpos ($matches[1], ' ');
            if ($tmp)
            {
                  // Because the regex is such a nuisance
                  $url  = substr ($matches[1], 0, $tmp);
                  $rest = substr ($matches[1], strlen ($url));
                  
                  $width  = 100;
                  $height = 200;
                  

                        $parts = array_values (array_filter (explode (' ', $rest)));
                        $width = $parts[0];
                        
                        unset ($parts[0]);
                        $height = implode (' ', $parts);

                  
                  return $this->capture ('iframe', array ('url' => $url, 'width' => ($width && $width<=$iMaxWidth ? $width : $iMaxWidth ), 'height' => ($height && $height<=$iMaxHeight ? $height : $iMaxHeight )));
            }
            
            return '';
      }

      function the_content ($text)
      {
        return preg_replace_callback ("@(?:<p>\s*)?\[iframe\s*(.*?)\](?:\s*</p>)?@", array (&$this, 'replace'), $text);
      }
}

$embediframe = new EmbedIframe;
?>
0
 
thtateAuthor Commented:
This is great so far, i did notice however , that if you dont enter a number or a space after the url, it breaks it, you have to at least leave a space before the closing bracet. is there a way to eliminate the need to leave a space after the url and also elimate the text "iframe" after the first bracet? so the code you are typing in the post would look like:

[http://youriframeurl.com]

thank you so much for your help!
0
 
James RodgersWeb Applications DeveloperCommented:
not teh most elegant, but try this

<?php
/*
Plugin Name: Embed Iframe
Plugin URI: http://blog.deskera.com/wordpress-plugin-embed-iframe
Description: Allows the insertion of code to display an external webpage within an iframe. The tag to insert the code is: <code>[iframe url width height]</code>
Version: 1.0
Author: Deskera
Author URI: http://deskera.com

1.0   - Initial release
*/




include (dirname (__FILE__).'/plugin.php');

class EmbedIframe extends EmbedIframe_Plugin
{


      function EmbedIframe ()
      {
            $this->register_plugin ('embediframe', __FILE__);
            
            $this->add_filter ('the_content');
            $this->add_action ('wp_head');
      }
      
      function wp_head ()
      {
            
      }
      
      function replace ($matches)
      {
      $iDefHeight = 100;
      $iDefWidth  = 200;

      $iMaxHeight = 600;
      $iMaxWidth      = 500;
            $tmp = strpos ($matches[1], ' ');
            if(!$tmp){
                  $tmp = $matches[1];
                  $url=$tmp;
                  $rest='';
            }
            
            if ($tmp)
            {
                  // Because the regex is such a nuisance
                  if(!isset($url)){
                  $url  = substr ($matches[1], 0, $tmp);
                  }
                  if(!isset($rest)){
                  $rest = substr ($matches[1], strlen ($url));
                  }
                  $height = $iMaxHeight;
                  $width = $iMaxWidth;
                        $parts = array_values (array_filter (explode (' ', $rest)));
                        if($parts[0]){
                              $width = ($parts[0]< $iMaxWidth ? $parts[0] : $iMaxWidth) ;
                        }
                        
                        if($parts[1]){
                              $height = ($parts[1]< $iMaxHeight ? $parts[1] : $iMaxHeight) ;
                        }                        
                  echo "<br>h:".$height."<br>w:".$width;
                  
                  return $this->capture ('iframe', array ('url' => $url, 'width' => $width, 'height' => $height));
            }
            
            return '';
      }

      function the_content ($text)
      {
        return preg_replace_callback ("@(?:<p>\s*)?\[iframe\s*(.*?)\](?:\s*</p>)?@", array (&$this, 'replace'), $text);
      }
}

$embediframe = new EmbedIframe;
?>
0
 
James RodgersWeb Applications DeveloperCommented:
that has some test code in it use this instead

<?php
/*
Plugin Name: Embed Iframe
Plugin URI: http://blog.deskera.com/wordpress-plugin-embed-iframe
Description: Allows the insertion of code to display an external webpage within an iframe. The tag to insert the code is: <code>[iframe url width height]</code>
Version: 1.0
Author: Deskera
Author URI: http://deskera.com

1.0   - Initial release
*/




include (dirname (__FILE__).'/plugin.php');

class EmbedIframe extends EmbedIframe_Plugin{
      
      function EmbedIframe (){
            $this->register_plugin ('embediframe', __FILE__);
            
            $this->add_filter ('the_content');
            $this->add_action ('wp_head');
      }
      
      function wp_head (){
            
      }
      
      function replace ($matches){
      $iMaxHeight = 600;
      $iMaxWidth      = 500;
            $tmp = strpos ($matches[1], ' ');
            if(!$tmp){
                  $tmp = $matches[1];
                  $url=$tmp;
                  $rest='';
            }
            
            if ($tmp){
                  // Because the regex is such a nuisance
                  if(!isset($url)){
                  $url  = substr ($matches[1], 0, $tmp);
                  }
                  if(!isset($rest)){
                  $rest = substr ($matches[1], strlen ($url));
                  }
                  $height = $iMaxHeight;
                  $width = $iMaxWidth;
                        $parts = array_values (array_filter (explode (' ', $rest)));
                        if($parts[0]){
                              $width = ($parts[0]< $iMaxWidth ? $parts[0] : $iMaxWidth) ;
                        }
                        
                        if($parts[1]){
                              $height = ($parts[1]< $iMaxHeight ? $parts[1] : $iMaxHeight) ;
                        }                        
                  return $this->capture ('iframe', array ('url' => $url, 'width' => $width, 'height' => $height));
            }
            return '';
      }

      function the_content ($text){
        return preg_replace_callback ("@(?:<p>\s*)?\[iframe\s*(.*?)\](?:\s*</p>)?@", array (&$this, 'replace'), $text);
      }
}

$embediframe = new EmbedIframe;
?>
0
 
James RodgersWeb Applications DeveloperCommented:
glad i could help


thanks for the points
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now