How to: Any PHP Anywhere
Out of the box, Montezuma allows a limited set of PHP code which serves the needs of most users. However, if you need other code, with two fantastic plugins, you can easily use any PHP code in any template or subtemplate.
First, here's an important concept. With Montezuma, when you hear the term sidebar, also think inside, above, and below your content margins. Montezuma not only allows unlimited widgets in your sidebars, you can have sidebars anywhere, such as in your header, footer, and even in the content area of a template. PHP Code Widget Plugin The PHP Code Widget Plugin, allows any PHP code be executed in a sidebar. For this example, we'll use some PHP code that's not on the limited list, and we'll use it in the content area of the main template, like is done here to make the random images at the top of each page.
Code Snippets Plugin If you're going to repeat code and want to follow Don't Repeat Yourself principles (which make changes much easier later), there's another great PHP plugin called Code Snippets Plugin. It allows you to add callable PHP code to your site, without modifying any of your wp or theme files. I put all the code to make the random images into a snippet, then, in the PHP sidebar, just called the function. This way, if I change something, I only have to edit the snippet, and it will get changed everywhere it's used. It's a good idea to prefix all your custom function and global variable names with something like: PHP Code:
If you're putting variables into your snippets, and calling them later, you may also have to use the global construct to access them. Happy coding! Update: Here's an example from Juggledad using a different php plugin "exec php". |
This techinque just took the concept to a whole 'nother level with conditions.
Update: Here's an example of conditional widgets. |
Update: PHP Outside of Sidebars
Using the two plugins described previously in this thread, php needs to be included within a sidebar widget. This is fine for templates. However, when you need to use php in a page or post, but not in a template, what do you do. Voila, another wonderful php plugin, Insert PHP. It's very simple. Where you would want to put: HTML Code:
<?php HTML Code:
[insert_php] Almost any PHP code can be called, including code in snippets (as described earlier in this thread). Text can be included from files with the include construct, however, functions from an included file will not be evaluated. If you want to use included functions, declare them first in a snippet. If you're referencing a variable, and it's not working, try using the global construct first, such as: HTML Code:
[insert_php] No sidebars are needed with this plugin. In fact, it won't work in a sidebar or a template; it only works directly in pages or posts. Other than the shortcode to call the plugin, no other shortcodes are needed for your PHP. Other than server memory, there is no limit to how many times you can use it or how large your code can be. Note: Be sure to use the html tab, rather than the visual tab, to insert the PHP code into your pages and posts. |
All times are GMT -6. The time now is 06:17 PM. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.