WP-Mix

A fresh mix of code snippets and tutorials

Category Archive: PHP

Posts about PHP

Page 2 of 7

PHP Truncate Text at Word

Here is a PHP snippet to truncate a string of text at the specified number of words. This is a nice alternative to just shortening a string of text based on character count. With this technique, the truncated string contains only complete words and is readable.

PHP Search Multidimensional Array

When you need to search an array using PHP, you can call upon the ancient powers of in_array() and call it a day. But that only works for flat, or one-dimensional arrays. What if you need to find a value in a multi-dimensional array? Well my friend, there isn’t a native PHP function to help […]

Minimal, optimal email headers

I recently had a conversation with an email guru concerning the ideal headers to use when sending plain-text email messages. Here is the punchline of that insightful exchange..

Including Arrays in URI Requests

Some esoteric code phenomena for you today.. in this post I explain how PHP handles arrays when they are included in URL requests (via the query string). It’s something I failed to grasp until doing some in-depth work developing my professional WordPress firewall plugin. Now let’s jump in..

PHP List All Directory Files

Here are three ways to list all files in a directory via PHP: via iterator class, file function, and while loop (in order of preference).

PHP run script for specific IP address

When working on a live site (which of course you should never do), it may be useful to execute some PHP script only for your own IP address. This quick tutorial provides a simple script that can make it happen. $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'undefined'; $agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'undefined'; $address […]

PHP Protect Include Files

Including files plays an important role in PHP development. For example, your app may consist of a main plugin file that includes several smaller files, each of which contains some distinct bit of functionality. When including such files, it’s a good idea to protect them against direct access. This tutorial rounds up a bunch of […]

PHP Stringify Formatted Text

This code snippet takes a string of text and removes all carriage returns, new lines, and tabs, replacing any matches with a simple blank space. An ideal way to “stringify” a potentially formatted lump of alphanumeric text.

WordPress Fix Image Upload Errors

Here are some quick tips to help troubleshoot and fix image and file upload errors in WordPress. Hopefully it helps someone out there get things back on track with their uploaded images. I know it can be frustrating!

Include files from parent directory, subdirectory

This post concludes our trilogy of posts on getting file and directory path information with PHP and WordPress. In this final path-related post, you’ll get numerous ways to get and include files from parent directories, subdirectories, adjacent directories, and even the same directory, just to round things out.

Getting Path Info with PHP and WordPress

Massive round-up of PHP and WordPress techniques for getting various directory and file path information. This is a mega-reference aimed at PHP/WordPress developers.

PHP Get Absolute Path, Document Root, Base URL

Depending on your server configuration, getting correct path information can be challenging. For example, PHP does not provide a variable that will return the the base URL of your site. To help out, you can use the following code snippets to get the absolute path, document root, and base URL, respectively.