If you come across a situation in which a conditional execution of a text or html widget in wordpress would come in handy, then you know how hard programming a widget to appear only on certain pages, or in certain situation on your pages, can be. That is because, it looks like wordpress creators thought from the very beginning, that everyone has the technical know-how and the level of proficiency in programming as they have, so they must have thought, it should be a no-brainer to learn complex functions, to create algorithms, create plugins, and then integrate into worpdress to extend its functionality.
The truth however is, that most wordpress users don’t even have a clue what a plugin is, let alone knowing how to implement, install, not to mention, create one, to extend the functionality of their wordpress site or blog. So if so happens, that you’d like to have a certain banner, for instance, appear only on your main page, but not on posts, or, some things, like share buttons, only appear on posts pages, but not on other pages, and you’d like to have for 3 different pages of your site, 3 different banners in your sidebar or sidebars … you are stuck.
Stuck, because issuing simple conditional statements, like telling your text/html widgets that the code in them should only execute on certain pages, is nearly impossible, because any code you input into those widgets, will be either ignored – if it’s a PHP code, – or simply parsed and output as HTML, and the parts that are “not understood” by the wordpress engine, will be gracefully ignored.
After having tried several plugins offered by the wordpress “official” plugins repository, and being unable to make ANY of them work, I did a lot of digging and documenting, and finally, I’ve come up with a much simpler solution then a plugin: you can add a filter to one single file in your theme directory, and there you go, you can from then onwards issue PHP statements – even more complex code snippets – in your text/html widgets, and they will be parsed as PHP and output as such.
For instance, you can make a 200×200 box with a banner appear only on your front page, or on pages that ARE NOT POST PAGES (pages from your top menu, label search pages, etc.) if you insert the following php code into your text/html widget:
(… here comes the code to be executed , like the bannner’s script, etc. …)
// we need to close the accolade for the if statement
So far, so good, right ?
To be able to have codes like this one above, and more, you need to edit ONLY ONE FILE in your themes directory.
Log in to your wordpress installation with the administrator account. Go to your dashboard, select “Appearance” from the menu, then select “Editor” from its menu options. The editor window will load. From the right, locate a line that say “Theme functions” and “functions.php” written under it. Click on that line in the right column. The functions.php will be loaded into the editor window.
Scroll or otherwise (click in the editor window and press CTRL+END simultaneously) go to the end of the file. You will see the closing PHP tag, “?>” as the last characters that appear in the file. Click in front of the question mark and press ENTER a couple of times so you’ll create some empty space in front of the closing tags.
Add the following code EXACTLY as it appears here, into the file, (copy-paste is good), before the closing tag.
return $html; }
Save the file, by pressing/clicking on the “Update file” button. That’s it, from now on, your PHP codes will be executed from any text/html widget.
NOTE: please bare in mind, that this function is for the SHORT opening tags version of PHP statements, you will *HAVE* to use “<?” instead of “<?PHP ” for issuing your PHP statements, otherwise, they will be ignored by your text/html widgets, just as they have been so far, before implementing this.
Questions ? Comments ? Leave a comment in one of the comment boxes under the post, and I’ll get back to you as soon as I can (usually in a few hours)