In the previous post, I began sharing how to download user email addresses via JSON in WordPress (using Ajax). The first article covered:

  1. registering the JavaScript for doing so
  2. getting the users’ email addresses,
  3. and parsing out the results.

There’s more to do, though. Namely:

  1. converting the returned information into JSON,
  2. writing that file to disk,
  3. and how to tie the rest of it together using JavaScript.

And that’s what the remainder of this post is going to cover. So if you haven’t read the preceding article, I recommend it if for no other reason than to glance at the code to see it thus far.

User Email Addresses via JSON, Continued

Aside from the fact that there are other ways to handle this same process (some more performant depending on the number of users), note that I am using jQuery (rather than vanilla JavaScript) to do this. Some people aren’t fans of doing this, and that’s cool, but I don’t have examples in this post for that.

Download User Email Addresses via JSON in WordPress: jQuery

I opt for these examples because jQuery ships with WordPress, many WordPress developers use it, it fits the bill for this situation and I [still] like it. 🙂

With that said, I’ll pick up where I left off.

1. Convert an Array Into JSON

If you’re familiar with PHP then this is something that’s easy to do; but if not, it’s no big deal. First, review json_encode in the PHP manual. Note that it takes an array and will return it as a formatted string.

This is what I use to convert the array of email addresses into a string of JSON that will eventually be written to disk. It’s one line of code.

json_encode($user_results);

Remember, however, there is a function that will generate the user results as needed. Specifically, I called the function getUserInfo, and I’ll share it momentarily.

Because since we’re going to be sending a file to the user, the information needs to be written to disk.

2. Write It to Disk

At this point, it’s time to write the results to disk. Depending on the permissions of your server, you may be able to write the file to disk directly in the plugin’s directory; otherwise, you may have to use the uploads directory.

Assuming that you’re operating with the former, here’s what the full function looks like:

First, it checks to see if results.json exists. If not, then it will create an empty file. After that, it opens the file for writing, drops the contents of the array in the JSON format into the file, then closes the resource.

Now, the file is on disk, so it’s time to send it to the user using JavaScript.

3. Send It to the User

First, we’ll revisit the _getEmailAddresses JavaScript function from the previous post. You can see the work done thus far in this gist.

Download User Email Addresses via JSON in WordPress: plugin_dir_url

Note the first thing to do is call a server-side function I’ve opted to call getEmailAddresses. Note that it runs the query, generates the array, writes the file to disk, and echoes the filename to the JavaScript function (because WordPress requires an echo rather than a return for Ajax calls):

Notice that this is three function calls each of which takes the results of the other as a parameter and then echoes the plugin_dir_url with the name of the file I’ve opted to use (clearly results.json).

4. Sending the File via JavaScript

This is where it gets a little more complicated. The code needs to:

  1. make sure there is a proper response,
  2. if so, then it will dynamically create a hidden anchor
  3. trigger the anchor’s click event to simulate a user clicking on it.

Make sense? Check out the results of the anonymous function below:

This will display the download file dialog to the user and will allow them to display the JSON file created through this and the previous post.

That’s It?

Essentially, yes. Granted, you may have to make tweaks to your code for your web server, but the general idea remains the same:

  1. run the query,
  2. generate an array,
  3. convert it to JSON,
  4. write it to disk,
  5. send the file to the user

And yes, you may opt to use the WordPress Coding Standards (versus PSR2), vanilla JavaScript (versus jQuery), and so on.

Regardless, the procedure remains the same and this is one way that you can provide a way to download user email addresses via JSON in WordPress.

Category:
Articles
Tags:
,