Category Archive: PHP
Posts about PHP
Working on my free WordPress plugin, Simple Download Counter, I needed a quick way to check if a remote file exists. As in, requesting the file returns a 200 “OK” response. Here are a couple of ways to do it via PHP.
When working on a PHP project, I usually log any errors to a file (error log), but on some projects it’s necessary to display any errors right there on the page. Here is the code that I use to make it happen.
Originally, the WordPress database was formatted as type MyISAM. Then years later they changed the database format to InnoDB. Reportedly, InnoDB is superior to MyISAM in several ways. This is true for any dynamic site, not just WordPress. If your database is not InnoDB, you may benefit from changing it. This quick tutorial explains how […]
This quick function does one simple thing. It removes empty file data from a PHP files array. As you may know, the structure of the $_FILES array is sort of backwards from what normally would be expected. Because of this, removing empty file data from the array requires some extra wrangling. So to make things […]
Here is a nice PHP snippet for GeoIP lookups. As written the script uses api.hostip.info for the lookup service, but that is easily changed as needed (GeoIP lookup services change or go offline constantly). So for what it’s worth, figured it was worth sharing rather than complete and utter deletion. Yes I am a code […]
Here is a function used in my free WordPress plugin User Submitted Posts that enables you to rename WordPress media uploads. So for example, if the user uploads a JPG image named “sweet-ride.jpg”, the following function will append a random string to rename the file “sweet-ride-random.jpg”, where “random” is a random alphanumeric string containing 20 […]
Working on my plugin Disable Gutenberg, I needed a way to get the ID of an edited post. Not on the front-end, but on the “Edit Post” screen in the WP Admin Area. Unfortunately WordPress does not provide a built-in core function for handling this, so it’s necessary to roll our own solution.
Found a couple of useful PHP code snippets for adding and removing query strings from URLs. This is useful when implementing paged and search functionality, among other things. These functions work great and are a real time saver — copy, paste, test, and done.
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.
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 […]
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..
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..