Solved: WordPress Cannot Upload Images (NGINX)

Symptom: WordPress running on NGINX cannot upload images or files. You may see an error message that tells you “permission denied” in the media uploader. Naturally you check permissions on your uploads folder, but the permissions appear correct. You might have even tried setting chmod permissions to 777. However, none of this will help if NGINX, your web server, is denying WordPress the ability to upload to it’s temp directory.

Your php error log may contain something like this.

open() "/var/lib/nginx/tmp/client_body/*" failed (13: Permission denied)

View the temp folders current ownership and permissions. This is where the root of the issue is.

sudo ls -l /var/lib/nginx/

You should get something like this back:

drwx------ 7 owner group 4096 Jun 28 18:52 tmp

Specifically, look at the owner and group. This needs to be set to your WordPress application user and group. Not sure what that is? Go take a look.

sudo ls -l /var/www/pathtowordpress/wp-content

Set the ownership of your temp directory to match the wp-content folder.

sudo chown yourwebapp:yourwebapp /var/lib/nginx/tmp

At this point, if everything went as planned, you should be able to upload images and other files.


Truncate a Phrase Using Words, Not Characters (PHP)

Often when people truncate text in their WordPress excerpts or sample text, they simply limit the total number of characters and add an ellipsis. It’s pretty easy to do, but it usually ends up with a word being broken at the end.

For exampl…

This function will truncate any phrase by the number of words you specify and add an ellipsis if you specify one.

function ssm_truncate($phrase, $max_words, $ellipsis = '') {
   $phrase_array = explode(' ',$phrase);
   if(count($phrase_array) > $max_words && $max_words > 0)
      $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).$ellipsis;
   return $phrase;

One could expand on this and also add a character limit in the event that the phrase contains several long words. Perhaps I’ll do that someday.

PHP Phone Number Formatting Function

Phone numbers have got to be one of the most commonly gathered bits of information in forms. I created a little PHP function that formats them for the most common US strings, 7 or 10 digits.

The Function

function formatPhoneNumber($variableName) {
  global $$variableName;
  // Keep numbers only
  $$variableName = preg_replace("[^0-9]", "", $$variableName );
  // Remove "1" if it's the first character
  $$variableName = preg_replace("/^1/", "", $$variableName);
  // format for 7 digit numbers
  if(strlen($$variableName) == 7)
    $$variableName = preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $$variableName);
  // format for 10 digit numbers
  if(strlen($$variableName) == 10) // 10 digit numbers
    $$variableName = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $$variableName);

Calling the Function

if (isset($_POST['phoneNumber'])) {
  $phoneNumber = addslashes($_POST['phoneNumber']);
// Do something with the phone number, i.e. insert into a db...

Things This Doesn’t Account For:

Obviously, this does not perform any form validation. If anything other than a 7 or 10 digit number is submitted, it will only be stripped down to numbers.

Formatting Phone Numbers with PHP

When adding a phone number field to a form, it’s nice to format it (if it isn’t already). This doesn’t replace anything you would do to validate your form via Javascript. But it does create that little extra bit of uniformity, even if it is slightly redundant.

Update: I have updated this a bit and created a reusable function. You can find it hereContinue reading Formatting Phone Numbers with PHP

Using PHP Classes and Functions in WordPress

Creating custom functions in your WordPress template’s function.php file is a regular thing for many of us. What you might not know is how easy it is to wrap them in a class, or even why you would. Wrapping them in a class keeps them both organized and self-contained from other functions that may have the same name. This can be especially important if you are using third party plugins. Continue reading Using PHP Classes and Functions in WordPress

Role Based Profile Fields in WordPress

I recently set out to customize WordPress profile fields for a specific role. You may want to have custom fields for users, but have different fields for different roles. For example, Authors may need some additional contact information, while Subscribers need only some basic fields. The concern I had was wether or not an Admin would be able to pull up any profile and edit all of the fields. Here’s the solution I found. Continue reading Role Based Profile Fields in WordPress

WordPress File Uploads Checked By MIME Type

Building a custom HTML form for WordPress is something I do fairly regularly. When adding an upload form, it’s important to check for allowed file types. You wouldn’t want anyone uploading a malicious PHP script, right? Wouldn’t it be great if WordPress did that for you? Of course it would. Continue reading WordPress File Uploads Checked By MIME Type

Understanding the WordPress Quick Edit Custom Box

Alright, I have to confess. This took me some time to wrap my head around. It also seemed like a lot of people were having the same problem. The question is “How do I add custom post meta fields to the quick edit function in WordPress?”

For some reason, I had a hard time finding the answers I needed in Google. I must not have been asking the right question or something. So here is what I learned. Continue reading Understanding the WordPress Quick Edit Custom Box

Use PHP STRISTR To Stop Comment Form Spammers

It’s not often that spam gets past my defenses. However, when it does, it’s really annoying. So today I set out to stop comment form spammers from submitting forms with one or more links.

PHP’s STRISTR searches a string for anything that matches your search. So, searching for “http://” will return true if it is found in the string. This will only work on form fields that you do not want to allow a link to be added. So, it may help to give visitors a heads up before they post a comment with a hyperlink included. Continue reading Use PHP STRISTR To Stop Comment Form Spammers