Implement If-ElseIf-Else-EndIf with short codes

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…

Leave a Comment