My *nix world

Inject PHP code WordPress

In this article I proved the concept of injecting JavaScript code in a WordPress post/page, so don't miss it.

WordPress is a very well written CMS driven by a plug-in architecture. If you want to insert some PHP code into a WP post/page you have few options:

  • you can create a WP child theme and then a custom page template where you could inject PHP code WordPress
  • you can write a plug-in that basically will parse the original page content and replace a PHP code block with its runtime evaluated equivalent

Create a child theme

If you want to extend the functionalities of your WP default theme you might consider creating a new child theme which inherits all its parent functionalities. Activate and use the newly created child theme. You can define the child theme customizations (by writing and/or injecting some PHP code in header, footer, page content, etc) that will enhance the current theme user experience. Read more.

Create a custom page template

You can define a custom page template that you can use it later when you write a post. In this custom page template you embed the PHP code you need. It will run for each post that uses this custom page template. Read more.

Writing a plug-in

This is my favourite method although the injection of PHP code in your WP post can be achieved easily by using the two methods named earlier. The principle should be exactly the same, the only difference is that you wrap the whole thing/customization into a small package called Plug-in. There is a difference, though: it will work not only on the default website theme but also on all the themes that your website has (like the theme used to deliver the content to the mobile devices, etc).

To write a plug-in is as simple as saying "Hello". I'm not kidding, it really is!

Check this out:

This is the simplest WordPress plug-in ever. You only need to specify the "Plugin Name : <the name>" and that's it. Of course, it does nothing but at least you can understand the concept. If you want to learn more about writing plug-ins for WordPress then don't forget to read this.

The aim of a WP plug-in is to alter the way that the WP produces the content by default. What the WP developer did was to create an architecture that allows you to hook/inject your code just before/after various portion of their code. By doing that you have the chance to alter the post/page content just before WP does or just after it did.

How can we use this for inject PHP code WordPress?

Well, we write a function and inject it right before the WP sends/outputs the page content to the client. By doing this we have the chance to alter the default page content, also to filter the content delivered to the client. This behaviour was named (by WP developers) filter. We can write and add one or several filters that will actually filter portions (like the header, the footer, the page content, etc) of the web page.

The simplest filter ever could look like this:

We wrote a filter that does nothing. But this was the idea, to be simple, to help you understand the concept. What it does? It receives the original content ($content variable) and delivers it as is (the unchanged $content).

So far your plug-in code looks like this:

Don't hurry to use it, it will not work in this version. We are just one step away, though.

We could have hundred of various functions in this file so WP has no clue which one is some helper function and which one represents actually the filter that you want to use. To let WP know about your filter all you have to do is to add one line more in the plug-in file main code:

While 'my_filter_function' refers our custom function that is supposed to filter the original page content, the 'the_content' is actually the WP predefined name of the hook/filter. If you use some filter name that only you (and your friends probably) know about it then nobody will use it because nobody has knowledge about its existence. So meanwhile stick to the predefined WP filter list, it is pretty large, you will see.

Anyway, how looks our final version of WP plug-in now?

Do you want to use it? If you do then please follow the steps below:

...and that's it!

Now, let's customize little bit this plug-in so that anytime when it finds in the page content a custom short code like:

it will evaluate that PHP code at runtime and will replace the original block of code with the runtime evaluated result that corresponds to the PHP code.

The steps to implement this are the following:

  • search the original content for all blocks that start and ends with our custom tag (i.e. [my_tag] and [/my_tag])
  • evaluate at runtime the PHP code and store the result in a variable
  • replace the entire block (including the tags) of code with the result value stored in the variable mentioned above
  • return to the WP the altered version of the page content

Search for our custom tag

We can simply use the regex pattern together with the PHP preg_match_all function:


The regex pattern above will match all blocks that starts with [my_tag] followed by zero or more of any characters and which ends wit [/my_tag]. We should decode that HTML block because it certainly will be HTML encoded. In case we still find any HTML code enclosed within <some_tag> and </some_tag> we should discard that code because it would never be evaluated as a PHP code. Actually our PHP code will fail to evaluate if such HTML code is found.

Evaluate at runtime the PHP code


We are going to use the PHP output buffering control to capture our PHP evaluated code that will certainly be printed out by the PHP engine. So instead to be delivered to the client (as a response) we capture that output into a buffer and we use that result. So, first we turn on the output buffering then evaluate and capture the buffer content and last but not least turn off the output buffering. The $eval_buffer will contain the content of the realtime/evaluated value of our PHP code.

Replace the block code with the result


We don't want to output the original PHP code block but its realtime evaluated value. So in the original page content we replace that PHP block code with its runtime value.

The complete function code


It encloses about 10 lines of code. Great!

Your plug-in file will enclose one function and one filter.

If you write a post/page and, among other things you wrote, you will write (at design time) the text below:


when the page will be generated, at runtime, it will output something like this:

Hello world! Right now is 2014-02-19 15:01:23

Just as an example, I have used the technique described above in a page where I run a PHP code that scans a certain folder and presents a list of files to download. Just take a look, see the Download tab.
You will see an unordered list of (.zip) files that was generated by nothing less than a [my_tag] ... PHP code [/my_tag] plain text in my post/page. Nice!

Conclusion

To inject a custom PHP code into your web WP pages you need to write a 10 lines plug-in that does the job. That simple is to inject PHP code WordPress web page.
[nolatex]

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Inject PHP code WordPress

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Inject PHP code WordPress

Latest posts by Eugen Mihailescu (see all)

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: