PHP Logo

There are various reasons that you might want to force a website to use SSL. In general, if you have an SSL cert setup for your website, you should probably force all users to https even if the page doesn’t contain sensitive data. In an ideal world, you would do this on the server side of things. Write some rules with the conf file that will force all traffic over https. If you are in a position where you can’t use the server then it is also easy to force SSL with PHP. It is also very easy to do it with pretty much any programming language, but for this example I will use PHP.

And there you have it. It is that simple. If you are using something like Cloudflare it can get a little tricky sometimes depending on how you have cloudflare configured to handle the SSL. For a standard site, this will be a simple way to force the use of SSL.

It is also worth mentioning that you must have an SSL cert configured on your server in order to make this work. If you do not have a site that supports HTTPS then you cannot make this work.

Email

When you have obtained a list of emails from a location that required little to no validation on whether it was a real email, you will be stuck trying to determine if the email address is real or not. You don’t want to risk sending out an email to these users without checking as a regular high bounce rate is a quick way to get your email server blacklisted. There is a 2 step method that you can use to validate if an email address is valid or not. This assumes that you have first filtered out values that are missing an @ symbol and a domain. This guide will show you how to check if an email is valid. For example, how can you tell if john@somesite.com is real or fake?

Step 1

The first thing you will need to do is check if the domain name is valid and has an active mail server/ MX record associated with it. Sometimes an email may have been valid at one stage, but the website has now been shut down. Sending an email to this address wont do anything. By checking to see if the domain name is real you will be able to filter out people who provide stupid domain names that never existed and also filter out emails from valid websites that are not capable of receiving emails.

For the example I am going to use PHP to write the script for this. Many other languages have similar methods that do the same thing, so this should be fairly easy to do with other programming languages. PHP has a function called “getmxrr()”. This function will obtain the MX record for a domain. For those who do not know what this is, a MX record is used in the DNS settings to point to the IP of a domains email server. If one is missing then the domain is not capable of receiving an email and is therefore invalid.

Just because a domain has an MX record, does not mean that the email address is valid. In fact, this makes sending bad emails to this server even more likely to cause you to get blacklisted.

Step 2

This is the most difficult to test while also being the most important. If someone provides an email like asdasd@gmail.com, step 1 will return this as being a valid email address. gmail.com is a valid email domain, but asdasd is likely a non existent user. This step will allow you to determine whether this is a valid inbox or not. Keep in mind that this step requires you to directly contact the email server to essentially ask if the inbox exists. I would suggest you run this from a test machine so you do not run the risk of blacklisting the IP. This many requests in a short period might be considered suspicious.

If you have worked with mail servers in the past, you may be familiar with HELO. This can be used to easily check if a mailbox exists or not. If you send the command and get a positive response you know that this inbox exists. If not you know its fake. I have combined step 1 with step 2 to generate a complete script below that will allow you to check if an email is valid and filter out bad mailboxes.

 

TinyMCE File Manager

This tutorial will show you how to take the media manager from WordPress and use it on an external page with a stock version of TinyMCE. I saw some posts about this and people were calling the WordPress editor to achieve this. Its an option, but it is not what this guide is for. If you have downloaded a standard version of TinyMCE and want the be able to browse and upload images using the WordPress media manager then this guide will show you how to do it. There are several file managers for TinyMCE, all which work perfectly fine. I have always found the WordPress media manager to be very powerful and easy to use. Unfortunately its part of WordPress and cant be downloaded on its own. Thankfully I have been able to find a way to use the media uploader within TinyMCE a lot more easy than i though. Here is how to use WordPress media manager with TinyMCE.

The first thing you will need to do is be able to load up the WordPress media manager on the page you are using. This means you need to either load the core of WordPress or just call the media method from WP. The following code will load the WordPress core and que all of the files you need to be able to call the media manager.

The next thing to do is create a javascript function that will load up the WordPress media manager. The following function will do this and will then return the selected image to the instance of TinyMCE that triggered it. The reason for this, is you may have more than one instance of TinyMCE on the page and you want to make sure you return data to the correct one.

You can test if the function works by calling it using a href. The final step to do is to link this function in with the init of TinyMCE. The following code is all you need

This variable in the TinyMCE init will tell the app that you are using a file manager and to call a function called “wpmediabrowser” when the browse button is clicked. When you select the image from the WordPress file manager it will return the image url back into the box.

When pulling comments from a database table, it can be very costly when you are performing a query that will automatically sort all of the comments into the parent child hierarchy. Every extra ms your SQL server spends performing calculations is time that is wasted. If you can easily move this logic to the programming language that you are using it will free up your DB server to serve more data. If you have a blog post that has comments, obtaining the comments from the DB for this post is a simple process, but the problem is that the order of the comments will not be right. If someone replies to the first comment days after lots of other comments have been made, this comment will show up at the end of the list returned from the DB. There are ways in which you can construct your query so that it will return an array of child comments for each comment, but this is way too wasteful. Using a simple recursive method you can sort out the comments using a programming language so as not to lock up your DB server any longer.

A simple SQL query like this is going to pull some comments from a database table that is storing comments for a blog post.

This query now leaves you with a list of comments that are a mix of parent and child comments are not in order. This seems a bit messy and can be a daunting problem to solve, but look at how simple that SQL query is. It is worth the effort when your DB server has very little to do to sort the comments. We are going to start this by creating the recursive function. A recursive function is a function that calls itself many times. The following function is written in PHP, but can be easily translated to any other programming languages.

This function accepts 2 parameters. The parent ID which is the Id of the comment that we want to get the child comments for, and then it contains the full list of all comments that were returned from the database query. When you want to see if a comment has children, you will loop through all of the comments checking to see if any of the comments have a parent ID that is equal to the current comment. Once we find a match we first need to check and make sure that this child comment doesn’t also have child comments,. This is why we use a recursive method. We cant tell how many comments have replies and how deep they go. This way we can do it without needing to know. Once we have checked for more child comments we can add this child comment to an array of child comments for the current comment we are checking.

The only thing left to do now is to determine how to set it off. We need a way to call this method and restart the recursive loop. Here is a completed method that will show you how to pull them from the database and then how to trigger the calling of the recursive loop.

 

Wordpress Logo

If you use a large amount of categories on your WordPress site, you will find that things get very very messy with a huge amount of categories showing up in the input box. One brilliant way to resolve this is to use a plugin called Intuitive Category Checklist. The good side, is the plugin creates a collapsible tree for all of your categories, the bad side is that you cannot add new categories when using the newer versions of wordpress. The plugin has been abandoned for over 2 years now, so its unlikely that we are going to get an update for this. We are going to have ot take matters into our own hands to maintain this awesome plugin!

Before making any changes to the live file, please make a backup in the event that something goes wrong. Go to your plugin directory and open up the file called “intuative-category-checklist.php” inside the plugin folder. Replace the entire contents of the file with the code below and it should resolve the issue with not being able to add new categories in the edit page.

Adding new categories, will show up at the top of the category box. While they do appear to have ignored the hierarchy of the categories, everything is fine. Its a visual issue, if you refresh the page everything will show up correctly. If anyone wants to work on a fix for this let me know in the comments below and i will update this post.

 

Wordpress Logo

Most guides on how to add a custom PHP page in WordPress will explain how to do it using a template in a theme, but this is a restrictive method of adding a custom PHP page to WordPress. It’s actually quite easy, perhaps slightly inefficient, to dynamically create a custom template for a WordPress theme. If you add a custom PHP page to wordpress using a plugin you only need to write the code once and it works for every theme.

The main goal here is to create a PHP page where you can generate any content you like. For this I wanted to pull data from a database outside of WordPress and I didn’t want to create a theme template for dozens of themes. By adding it in the plugin level you can create the page once and call the theme to render the content. There are some rules that you need to follow in order to do this and there may be some further tweaks that need to be made to cover all kinds of themes. I have tested this and it works with all of the themes that I have tested. Some of the steps here might seem unneeded, but I had to add them in order to make the code work with all themes that i tested with.

 

The first thing you need to do is have a plugin to use. Create a file in this plugin called test.php. The page does not need to be a part of the plugin at all, it just needs to be in the plugin directory so you can easily move it around to other WordPress installs. By default your theme will have a default query. Some themes will just pull a random post and others will generate a post feed. There is no way to prevent this, so instead we need to override the global variables to change it. This is the part that’s a little inefficient, but as you probably know, breaking the WordPress loop isn’t something you want to dig into.

The code below will call the blog header. This will start the loop and leave you with some global variables that contain data that your theme will then display. You need to override this data with your own custom data. This sort of acts like data injection. If anything this is like a custom hook. Once you get the global variables needed, you can do whatever you like with them.

If we override all of the relevant data within these variables we can let our theme do all the hard work of rendering the content correctly. This allows us to produce content that matches the look and feel of our website without having to write css for each theme we want to support. To see what is inside each variable you can use a var_dump();

The first thing and most obvious is to override the $post variable. Put whatever you want inside these variables. Hard code them, pull them from an API or get the data from a different DB. Doesnt matter where it comes from, so long as it overrides the existing data.

We have now overridden the global $post variable with our own custom content. This is normally enough for most themes. The theme will display this post and you wont need to do any additional work. Unfortunately the default method of displaying a post, which a lot of themes use, will first print this post and then also display a bunch of other recent posts.

Some themes will use the $wp_query objects via the have_posts() and get_post() methods. This will get the content for $post from $wp_query. We are going to have to override this variable if we want to stop the list of posts from displaying. The following code will override any arrays and variables that contain multiple objects.

This should be everything that you need to make themes display the posts correctly. If you find there are themes that need extra work please post in the comments and i will update the post.

The final thing to do, now that we have armed all of the global variables, is to call the default page.php to display our content. We want this to be dynamic so we will get the current theme directory from WordPress. All themes will contain a page.php, so we can be confident that this will work for us.

All of the code above will allow you to add a custom php page to WordPress using a plugin that is completely dynamic. You will not have to modify a theme at all, this code will handle everything through the plugin. Please post any issues or suggestions below so I can make this post as efficient as possible.

laravel logo

I looked up a few tutorials online on how to install Laravel and I got to the point where composer was giving me an error saying that i needed to upgrade to PHP 5.5. Since I had 5.4 running on the server and also had quite a lot of stuff already running on this server I was left in a difficult position where I needed to install Laravel and it seemed like I was required to install PHP 5.6 or 5.5. These are not supported by the default repos with CentOS. Going down the road of using custom repos is just asking for trouble, so I decided to find a way to do this without having to upgrade. So here is how I installed Laravel using PHP 5.4.

I am going to assume that you already have apache and php installed and running on your server.

Install Composer

I looked around and everyone was saying to run a bunch of stuff to download and compile composer. Seems like a waste of time when it was available using yum. I’m not sure if this is required, but you should install the epel repo to make sure you can install everything you need.

Thats it, composer is installed. Sure beats having to do a bunch of weird stuff to get it installed.

Install Laravel

Based on the official documentation for installing Laravel 5 http://laravel.com/docs/5.0 there are 2 different ways to install. I first tried to do it via the laraval new method and this worked, but it messed up. It seemed like the version of php I was running did not live up to the requirements. I don’t know why, so i tried the second option and this worked! To do this i ran the following commands.

This will begin the install process which can take some time as it does a whole load of stuff. Half way through i got an error message saying I needed a GitHub access token in order to continue further. Thankfully i already have a github account, but if you do not then you will need to create one.

To get a GitHub access token, follow this guide. https://help.github.com/articles/creating-an-access-token-for-command-line-use/

Enter the Github access token and it will continue with the install.

Once installed this should be it. If you go to the laravel/public directory in your browser you should see the default Laravel page. If you get an error 500 then it is likely due to apache not having write permissions. This can be fixed with the chown command.

 

Google Logo

The lastmod  field in sitemaps is subject to a lot of trouble for most people who are trying to build their own dynamic sitemaps. The main issue is that google only accepts a single type of timestamp and if it is now 100% correct it will throw a load of errors telling you the date is incorrect. I had a lot of difficulty finding the correct format to use for the timestamp. Quite often the solution would result in a date set to 1970, which of course was very incorrect. The solution below is how i was able to correctly use a MySQL timestamp in a Google sitemap for the lastmod field.

This will generate the correct output provided the input was first a valid SQL timestamp. One thing to be aware of and it is something that caused me to waste quite a lot of time. Even though you have updated the sitemap and the date is parsing correctly, Googles webmaster tools might still show the same error and will continue to show the error for a few minutes (even as far as an hour). As long as you are confident that the format is what it should be, wait a while and the webmaster tools page will eventually update correctly.

Wordpress Logo

Content filters are used to display some data at a specific location in a post. For example, if you wanted to display some data at the end of every single post on your blog, you could use a content filter and it would allow you to do this. You will need to be able to write PHP code in order to do this. If you follow my full WordPress Plugin Tutorial you should be able to build an entire plugin from start to finish without too much trouble.

So to start off, you will need to have your own custom plugin created. If you do not know how to do this, follow my guide on creating a WordPress plugin template. A plugin is very simple to create, so don’t worry if you aren’t particularly strong at programming in PHP.

Step 1 – Register The Content Filter In The Main Plugin File

Open up your plugin and go to the main php file (usually the same name as the plugin itself) and you can add the filter that will use “the_content” as the data that will be filtered. This means that when wordpress is loading the page and it comes to the point where it loads the content of the post, it will check to see if there are any filters that have been added. If it finds a filter for “the_content” it will run the function that is defined in the filter.

The following code will add a filter that is triggered when WordPress is loading the post content and it will then call a function called myinfo_filter().

Step 2 – Create The Filters Function

In order to make this code work we are going to need to create a function called myinfo_filter. The function is just a standard PHP function that will have 2 simple requirements. It must accept a variable that contains the content of the entire post and it must also return the content for the post.

When WordPress reads the code above it will call a function called myinfo_filter and it will give this function all of the content from the post. If you do not return any data the post will show up blank. If you do not add the function you will get a fatal exception. So lets add a simple function that will append some data onto the end of the post.

This function will accept the content from the post and if this is a single page ie. a post. it will add ” CUSTOM CONTENT!” onto the end of the post. This will show up for every single post on your blog. You can add some more advanced logic here to append something more substantial to the end of the post.

You could also append the data to the start of the post by doing the exact same thing except putting the data at the start.

 

Save the file and your filter should now be adding content to the end of your post.