Simple Ways to Execute PHP in WordPress Posts and Pages

Simple Ways to Execute PHP in WordPress Posts and Pages

For those who need a custom solution

Since web design is comprised of several basic elements – HTML, CSS and PHP,  which are 3 different languages, some people reach a different level of knowledge in them. On the other hand, systems like Wordpress are made to be used by people who don’t have knowledge for any of these languages. That’s the reason why directly inputting PHP code isn’t possible – because it wouldn’t be needed for the masses. Yet if you want to do it, there are ways, and that’s what we’re going to discuss that in this article.

Custom Template on Custom Page

First we’re going to discuss what Wordpress uses to display itself on the browser. Apparently, PHP does all the work in processing the content, and stores it so that it can be returned by certain functions in the theme template files.  Once there, they’re displayed , wrapped around the HTML of the template. But what does that mean to us ?

One of the reasons why this is so important to us, is because we can use that structure to execute our own code . We can make a custom template to be used only on a certain page, where we need the PHP code executed.

Make a function in the current template

Another commonly used way to bring our own code into Wordpress is to build a function (or several functions) into our code. Using “if … else” conditionals, or “case” structures, we can execute the function with different parameters wherever we need it in the template.  Wherever we need the code to run, we just execute the function like this :

<?php functionname($parametername) ?>

Both of the above mentioned ways have a problem, however. They cannot run in a specific post, because they depend on the template. We can either use them on the whole template, or on a single page that uses a different template, but not separately, for a post. So what do we do then ?

Use plugins that allow code execution

There are several plugins who do the same trick we need, but we’re going to focus on two particular plugins: Exec-PHP and Shortcode Exec-PHP.  You’ve probably already figured out what the difference between them is.

Exec-PHP is a wordpress plugin which makes execution in posts as easy as 1-2-3. This can be handy for various reasons, especially if you have knowledge of how to use scripts or even write scripts professionally. You can use PHP libraries to generate graphics, calculate statistics and bring more unique content to your site.

Still, Exec-PHP has one crucial drawback – if you are running a blog with several authors and several people with access to posting ,security is rather compromised since anyone can write code in the posts.

Here’s where we introduce Shortcode Exec-PHP. This plugin has rights management, which allows us to define the level of rights necessary to write PHP code, which will later be included with a shortcode in the post. The advantage here is that only certain people can write the PHP scripts and snippets, but many can execute the shortcodes associated with them.

With Shortcode Exec-PHP you can have one administrator that writes the scripts, and several moderators who use them if they find necessary. That’s not only good for security, but also prevents unskilled people from executing code on Wordpress, and prevents potential data loss and crashes which may occur if an incompetent person was to attempt executing their code.

It doesn’t need to be hard to include your own functionality via PHP. As with many other things in Wordpress, it’s wonderful that someone has already thought about it, and has already made it easy via plugins.

 

Please Login to post your comment