Category Archive: PHP
Posts about PHP
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..
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).
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 […]
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 […]
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.
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!