Monday, February 21, 2011

Wrapping a chunk of text with tags using PHP

I'm trying to allow a user (using Wordpress) to insert a jquery slideshow gallery (http://www.queness.com/resources/html/slideshow/jquery-slideshow.html) based on a faux tag. For example:

[slideshow]
    <img src="url" /><br />
    <img src="url" />
[!slideshow]

Would produce something similar to

<div id="gallery">
     <a href="#"><img src="url" /></a><br />
     <a href="#"><img src="url" /></a><br />
</div>

I think where I'm having problems is the jquery code requires the img to be enclosed with anchor tags. Here's what I have, but thanks to Wordpress, anything above or below the code isn't formatted correctly. I've used the Wordpress formatting function, but it wraps EVERY line in a paragraph tag, so it just breaks everything.

function make_slideshow($string) {

 $patterns[0] = '/(\[\[slideshow\]\])/';
 $patterns[1] = '/(\[\[\!slideshow\]\])/';
 $replacements[0] = '<div id="gallery">';
 $replacements[1] = '<div class="caption"><div class="content"></div></div></div>';
 $replace = preg_replace($patterns, $replacements, $string);

 $new_line = explode("\n", $replace);

 foreach($new_line as $key => $value) {
  if($value == "" || $value == " " || is_null($value)) {
   unset($new_line[$key]);
  }
 }

 $sorted_lines = array_values($new_line);

 foreach($sorted_lines as $key => $value){
  if( (stristr($value, 'href') === FALSE) && (stristr($value, 'img') !== FALSE) ){
   $sorted_lines[$key] = '<a href="#">' . $value . '</a>';
  }

  if( (stristr($value, 'show') === FALSE) && ($key === 1) ){
   $value = explode(" ", $value);
   $value[0] .= ' class="show"';
   $sorted_lines[$key] = implode(" ", $value);
  }

 }

return $sorted_lines;

};

Normally I find all my other answers on SO, so this is only my first question. I don't know if it's way too big of a problem for someone else to try to help me out with, but I am stuck so I figured I'd give it a shot.

From stackoverflow
  • There's nothing in that code that adds a p tag. If that's the problem, you need to find the real code that adds p as a wrapper.

  • Wordpress adds p tags automatically in the editor; you need to stop WP from doing that. In my experience, you either remove all line breaks in the editor - scrunch the code up - or use a few plugins, to stop WP from adding the formatting: TinyMCE Advanced and/or Disable wpautop.

    zack : It's my understanding that Wordpress doesn't store the p tags within the database, but formats with a function [like the_content()]. If Wordpress did store the p tags with the corresponding paragraphs, I don't think I would have a problem with this at all.
    songdogtech : I didn't say WP adds p tags via the database; it's irrelevant anyway. I said WP adds them in the editor. I run JS in posts and pages by stripping out the white space in the scripts; if you want to preserve white space, try those plugins. Try stripping the white space from your jQuery and see if it works.
  • What WordPress function is calling your routine?

    I had a similar problem with:

    <?php wp_list_pages( $args ); ?>
    

    Was adding <ul> links to everything I wanted to surpress..

    Check the WP dev docs for the routine that is up one hierarchy from your routine and I bet there will be an $arg value to suppress <p> tags.

    http://codex.wordpress.org/Function_Reference

0 comments:

Post a Comment