This Has Been Tested and It Does Wonders
This technique takes the Any PHP Anywhere
concept to a whole new level.
Update: With this technique
, you can use php in posts and pages, outside of sidebars.
an example of conditional widgets.
Originally Posted by shocka
Jerry - I tried the widget idea but with the widget styling it threw everything off. Maybe there is an idea here I am missing?
You can add custom css and override the widget css to look however you want.
Perhaps you can setup variables in the widget so it never outputs anything and use those variables with printf? Just a thought but seems messy with useless HTML being outputted.
Here's what worked for me, using php in widgets (which can be anywhere) and snippets. The widget has a function call:
This calls a function declared in a snippet:
ob_start(); // keep the_title() from being echoed to screen
$title = ob_get_clean(); // make value of the_title() into a variable, clean and stop buffer
if ($title == "Home")
$html = "home page html code and text...";
elseif ($title == "About")
$html = "about page html code and text...";
$html = "default html code and text...";
There are probably more elegant ways of doing this. I only do pretty simple procedural coding. If I were really doing a lot of this, I'd probably use arrays.
Other things besides the page title could be used as conditions. For example, a restaurant could post the soup of the day in a widget with code such as:
if (date("D") == "Mon")
$soup = "Chicken Noodle";
elseif (date("D") == "Tue")
$soup = "Beef Barley";
If you use any of the limited mz php code that outputs text as a condition, such as the_title() in the example, use output buffering. Otherwise, the output will be echoed when you test the condition, probably where you don't want it to appear.
Also, instead of just being, essentially, a string table, this function could call other functions, or even include files.
As far as a little extra html and css to use the widgets, my take on that is that WP outputs so much more bloat code than we would if we were writing code from scratch in notepad or something. So, what's a few more grains of sand on the beach? As long as whatever it outputs is consistent, we can style it so it looks fine to the user.
The shortcode didn't seem to have the continuity of being able to reuse code from one to another like the snippets. With snippets, all the code is activated and accessible. Maybe I'm just used to it.
I couldn't get other things to work like I wanted in pages or templates (outside of sidebars.) I tried the ezphp plugin and even tried filtering (although I haven't tried filtering with output buffering yet), but they didn't work like I wanted, either.
Remember, you can even make your entire content area a sidebar if you needed to. I've tried it, and it works. A sidebar can also replace the footer or the header.
and prefer to use this method for PHP code since all my PHP sits in one spot.
That's why I like the snippet plugin, and I like the same concept for custom css
Right now I am thinking of using a physical template with shortcodes.
With that plugin, can you put the shortcode into the virtual templates?
Please post your results. Thanks.