Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have very little experience with WordPress and PHP, so I apologize in advance for syntax problems.

I'd like to know if it's possible to implement If/Else logic as short codes. Something like:

[if] Version: IF [elseif] Version: ELSEIF [else] Version: ELSE [/if]
// OR...
[if] Version: IF [if branch="elseif"] Version: ELSEIF [if branch="else"] Version: ELSE [/if]

If you want more background into why I would want to do such an idiotic thing, see the referenced post on SO.

Parse CFML tags in PHP

share|improve this question
add comment

2 Answers 2

Having a look at your SO post, seeing the CF Tag output, then seeing what you're trying to attempt here with shortcodes is, as Otto said, going to be an incoherent mess.

There already exists plugins that allow you to write PHP logic amongst your post content, and the following Allow PHP in Posts and Pages will give you that ability in the form shortcode syntax like this;

[php]

    if ( condition ) {

        //do something...

    } elseif ( condition ) {

       //do something...

    } else {

       //do something...

    }

[/php]

Simple.

Everything between your [php] <-- --> [/php] is regular PHP. No need to write your own shortcode functions to mimic the if/elseif/else logic. That would be a waste of time and why that's so is because the biggest problem you're going to face has nothing to do with the if/elseif/else syntax of PHP but rather being able to parse and interpret via logic the existing CFTags, their intended purpose and then translating that purpose into its physical equivalent in PHP of which needs to still comply with the WordPress API especially if any of that functionality is reliant on the WordPress framework (?)

Taking my example above, its not going to take long before your logic is running out of control (in terms of size) and that's no real place to be throwing a bunch of code (into your post content editor anyway).

What you could do, still using the same concept above is create functions that perform the equivalent actions of your CFTags and have those functions stored within your functions.php file in your theme folder then from within the same block of code above you could do something like;


  //shortcode followed by reference to your function

  [php] echo do_tag_condtional(); [/php]

  //example function that accept arguments etc  

  [php] echo cftag_feature($args = 'bla');  [/php] 

  //if your function returns its output as echo then you don't need to echo within your post

  [php] quick_tag(); [/php] 

Those functions correspond to methods you've created to perform particular tasks, only that your code is now much more manageable if having to insert it amongst your post content.

It also means you can control the functionality of your said functions external to that of your posts for increased flexibility should you need to expand upon those functions.

BUT...

If your experience is limited as you suggested, are you going to be capable in parsing CFTags and then turning them into some PHP logic that you can actually use? Because if not, then the aforementioned isn't going to do a great for you until you overcome that stumbling block.

This is why I have to agree with Otto that you would be better off filtering your content hook but as an alternative suggestion if serious regular expression matching is going to be to difficult, I would use SimpleHTMLDom Parser within your functions as a HELPER CLASS which will allow you to iterate over HTML and select elements based on tags, extract the content you need, manipulate it and pass it out the other end as the the_content filter moves on through and you're almost as good as gold...

Example (very crude...)

add_filter('the_content','cftag_parser');

function cftag_parser($content) {

    include('path/to/simple_html_dom.php');  

    $html = new simple_html_dom();  
    $html->load($content);  

    //get an element  
    $element = $html->find("<cf_taglinks></cf_taglinks>");  

    //modify it  
    $element[1]->innertext .= " insert this text inbetween the above tags";  

    //output it  
    $html->save();     

  return $content;
}

There's also a pretty decent tutorial here,

http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/

Which will give you some additional insight on how you can use SimpleHTMLDom Parser to help you parse over your content as an alternative to, or even an addition to some regular expression matching.

Food for thought...

share|improve this answer
    
Awesome. I had no idea that there would be a plugin that would allow me to embed PHP in my post. I may not know PHP that well, but it can be learned, and probably a lot easier than the deep dive into WordPress. I'll go down this road and circle back to let you know how it worked. I can't accept an answer until I have it fully working, but this looks like it should do the trick. –  Snekse Jul 11 '12 at 18:25
    
The deep dive into WP, requires you pass through PHP anyways, so they go hand in hand. Learn one and you'll have much more success with the former. http://www.php.net is the official home of PHP docs which are invaluable. Good luck. Be sure to post back with Q's and progress... –  userabuser Jul 12 '12 at 19:23
add comment

Not easily, and anyway it would be painful and an incoherent mess.

Instead, I recommend writing your own custom parser code to handle your cf:custom tags as-is, without converting them to any other format or shortcode or what have you. You can use filters on the_content and (shudder) regexp to find your codes and deal with them appropriately.

Your basic filter looks like this:

add_filter('the_content','my_filter');
function my_filter($content) {
  // do whatever you like to the $content here
  return $content;
}

Whatever it returns replaces the content. Also, filters are chained in WordPress, with each one receiving the output of the previous. So if you have some custom CF: tags in there, then you can have a filter for each one (or set of them) and have it only alter and replace the relevant content section that the filter pertains to.

share|improve this answer
    
The custom parser was the original route I started on, but that quickly got messy as we needed to handle nested tags, tags with optional end tags, etc. The short codes has worked fairly well with two exceptions: if/else blocks and setting variables. That will be my next question posted: can you set a PHP variable within WordPress content? –  Snekse Dec 6 '11 at 14:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.