[wp-hackers] nested shortcode

Daiv Mowbray daiv at daivmowbray.com
Fri Jan 23 09:47:28 GMT 2009


Thanx Charles Clarkson for the suggestion, but you assume that all I  
want is to access an item for css manipulation.
Actually I want to add a rel="myrel" or class="myclass" for javascript  
manipulation.

Viper007Bond's suggestion worked well for me:
But I can't use shortcode attributes.

The suggestion:
add_filter ( "the_content", array(&$this, "myrel_links_out"), 8);
// array(&$this, is to reach the function inside of this class.

function myrel_links_out( $content ) {		
			$short_tag = '[myrel';			
			$pos = strpos($content,$short_tag);
			if($pos === false) {
				return $content;
			}
			else {
				$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\") 
(.*?)><(.*?)title=('|\")(.*?)('|\")(.*?)><\/a>/i";
				$replacement = '<a$1href=$2$3.$4$5 title="$9" rel="myrel[%ID%]"$6>< 
$7title=$8$9$10 $11></a>';
				$content = preg_replace($pattern, $replacement, $content);
				$content = str_replace("%ID%", $post->ID, $content);
			}
			return $content;
		}
So I have done this:
But I had to wrap the content in [myrel]content[/myrel]

add_shortcode('myrel', array( &$this , 'myrel_shortcode_out' ) );

function myrel_shortcode_out( $attr, $content = null ) { 		
	extract(shortcode_atts(array(
             'opA' => '',
             'opB' => '',
             'opC' => ''
             ), $attr));

	// opdate options if any changes with shortcode
		if ($attr !='') {	
		$this->plugin_change_options($attr);
	// call to function which updates plugin options array with shortcode  
over ride. see bellow.
          }

         extract($this->pluginOptionsOut);// extract options array  
into variables

		$pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\") 
(.*?)><(.*?)title=('|\")(.*?)('|\")(.*?)><\/a>/i";
		$replacement = '<a$1href=$2$3.$4$5 title="$9" rel="myrel[%ID%]"$6>< 
$7title=$8$9$10 $11></a>';
		$content = preg_replace($pattern, $replacement, $content);
		$content = str_replace("%ID%", $post->ID, $content);

         return do_shortcode($content);

		}
function plugin_change_options($attr){	

			$this-> pluginOptionsOut = array_merge($this-> pluginOptionsOut,  
array_filter($attr));
   			return $this-> pluginOptionsOut;
		}

On Jan 21, 2009, at 11:06 AM, Viper007Bond wrote:

> Shortcodes are not the best way to do what you are trying to  
> accomplish. Use
> a generic the_content filter and just check for the existence of
> "[addmyclass]" in the post. If found, regex the images and remove  
> the tag.


----------
Daiv Mowbray
daiv at daivmowbray.com
----------




More information about the wp-hackers mailing list