PHP Archives

How to add something to the_content in WordPress using PHP

Sometimes we need to add additional text or elements to the_content in WordPress. This can include links, icons, shout outs, author biographies, and so forth. I wanted to add an Apple Podcasts badge underneath each post in my podcast categories, adding a link to the badge, depending on the category. I’ll elaborate how I did this in a later post.

Right now, let’s have a look at how we can add elements to the content retrieved from WordPress. This should work independently of your theme, and no theme-file-hacking is necessary to accomplish this.

All we need to do is intercept the_content as it is requested, append our elements and return an amended version of the_content. Let’s see how this works.

Read more

How to declare Classes in PHP

Since PHP 5 we can use object oriented features in PHP. This allows us to not only use functions and variables, but also write classes and create instances of them in our code. Here’s quick rundown on how to do it. Writing and instantiating a Class Creating classes is very similar to writing functions. Here’s … Read more

How to update legacy constructor methods in PHP 7

When I was fiddling with my P2 Categories theme last week, debug mode generated several warnings when run in PHP 7.2.1. That’s because class methods are no longer allowed to have the same name as the class itself. This was allowed in PHP 5 and earlier, but from what I gather it’s no longer the … Read more

How to install PHP 7.x on a CentOS LAMP Stack

By default, CentOS 7 comes with support for PHP 5.4. Sadly that version has reached the end of its life in 2015and is no longer updated by the developers. If we want to stay up to date with the latest software, we may want to upgrade (if our applications are working with newer versions of PHP).

For CentOS users this either means to compile cutting edge versions from source and tweaking lots of scary system configurations – or dipping into the power of Software Collections. These are official pre-compiled packages by the software vendor, designed to run newer versions of software alongside those that are provided by default.

At the time of writing, PHP 7.2 is available but it’s not part of the software collections yet, so we’ll use  PHP 7.1 with FPM support under Apache (as it’s the recommended way to do so).

Let’s begin!

Read more

How to install PHP from source on CentOS

I have recently installed PHP 7 from source on a fresh minimal CentOS 7 box. No previous version of PHP was installed, and I thought I’d give 7 a spin. There were a few pitfalls I hadn’t come across before, so here’s what worked for me. Downloading and extracting the source code It sounds crazy, … Read more