Wordpress Themes - WP Forum at BFA
There will be no more development for Atahualpa (or any other theme), and no support. Also no new registrations. I turned off the donation system. I may turn the forum to read only if it gets abused for spam. Unfortunately I have no time for the forum or the themes. Thanks a lot to the people who helped in all these years, especially Larry and of course: Paul. Take care and stay healthy -- Flynn, Atahualpa developer, Sep 2021

Wordpress Themes - WP Forum at BFA » WordPress Themes » Montezuma Theme »

How to: Any PHP Anywhere


  #1  
Old Jan 25, 2013, 03:02 PM
jerryc
 
367 posts · Oct 2012
Florida
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.
  1. In main template, just below the opening div tag for the content area, we insert:

    PHP Code:
    <?php dynamic_sidebar'image-gallery-0' ); ?>
    This automatically created a new sidebar in the widgets area named "image-gallery-0" .

  2. In the widget area, we drag the PHP Code widget to the image-gallery sidebars and insert the PHP code into the PHP Code widget named image-gallery-0.

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:
custom_ 
so as not to conflict with any wp or theme 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".

Last edited by jerryc; Mar 26, 2013 at 09:08 AM. Reason: Clarification
  #2  
Old Jan 30, 2013, 02:13 PM
jerryc
 
367 posts · Oct 2012
Florida
This techinque just took the concept to a whole 'nother level with conditions.

Update: Here's an example of conditional widgets.

Last edited by jerryc; Mar 22, 2013 at 04:04 PM.
  #3  
Old Feb 7, 2013, 09:00 PM
jerryc
 
367 posts · Oct 2012
Florida
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 
your_php_code(); 
?>
Simply put:

HTML Code:
[insert_php] 
your_php_code(); 
[/insert_php]
instead, and it will work just as coded.

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] 
global $var;
your_php_code($var); 
[/insert_php]
You may also need to declare the variable as a global in the snippet. (I may have overdone this, but I got it to work when it otherwise wouldn't by using global in both places. Maybe a PHP expert can clarify this point.)

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.

Last edited by jerryc; Mar 19, 2013 at 11:09 PM. Reason: Clarification

Bookmarks




All times are GMT -6. The time now is 08:09 AM.


Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.