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.
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;
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;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…)
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…
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:
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.
It is possible fairly easy to do. Here’s an old basic that is only a single file that does just this:
https://plugins.svn.wordpress.org/if-shortcode/tags/0.2.1/plugin.php