cc licensed flickr photo shared by M.H.ick9s

Arrgh, just spent about an hour and half chasing myself in silly code circles. Not that it matters, but just to document my sanity/lack thereof…

I have a few WordPress pages hanging off the top banner, and I’d previously had them use PHP code in the pages, using one of the plugins that allow you to put executable PHP in your stuff (yes all warnings apply, you need to know what you are doing, security panic yadda yadda). I think I was using PHP Execution which, like Exec PHP just lets you put the code in the page including all the <?php … ?gt; stuff and let it go.

However, I often write posts that have PHp code in them, and I really like using the way the Syntax Highlghiter Plus plugin displays code, e.g.

The problem is, with any of those PHP plugins, WordPress tries to run my code.

When all I want to do is display it. And if I convert all the angle brackets to things like &lt; it is first a pain, and second, the Syntax Highlghiter Plus plugin does not render as the element they are.

I then thought to try one of the PHP plugins that require a special tag or shortcode wrapper, like Inline PHP but that was a nightmare for one of my pages that had about 14 different PHP chunks, and it does not behave like executed PHP (variable values are not kept alive).

So I had to brute force it- ditched all the PHP plugins. I narrowed it down to to key WP pages that needed to do some PHP action. I ended up creating special template files for these pages, and moving all the code, essentially, everything that was inn the content of my pages — into the template.

Huh?

Well, I started with the basic page template (page.php)

And I remove everything between <div class=”entry-content”> and its closing </div > — essentially lobotomizing the page – whatever I wrote in the wordpress editor, was not used. I saved this template for the Last 100 Barks Page as anew file, page-bark.php in my template directory.

The first thing to do is add a little line at the top that gives it a readable name in the WP editor:

And I cut all the code/content from the page in WordPress, and insert it into the template:

This is the code I use to generate a page with my last 100 blog posts (its really to help my find my own stuff), but I’ve hard coded all of the content and code logic into a template.

When I edit the page in WordPress, it can have whatever I want in the text content area, cause that is never used in the template, but the key is selecting te right template to use:

I’m sure some more clever person will point a much simpler solution…. but that person was not around tonight! So it has hack hack hack til something worked.

The post "WordPress Code Circles" was originally zapped with 10,000 volts and declared "It's ALIVE" by Dr. Frankenstein at CogDogBlog (http://cogdogblog.com/2010/04/wordpress-code-circles/) on April 27, 2010.

No comments yet.

Leave a Comment

All fields are required. Your email address will not be published.