In the previous post, I began sharing how to download user email addresses via JSON in WordPress (using Ajax). The first article covered:
- getting the users’ email addresses,
- and parsing out the results.
There’s more to do, though. Namely:
- converting the returned information into JSON,
- writing that file to disk,
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
I opt for these examples because jQuery ships with WordPress, many WordPress developers use it, it fits the bill for this situation and I 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.
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.
3. Send It to the User
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).
This is where it gets a little more complicated. The code needs to:
- make sure there is a proper response,
- if so, then it will dynamically create a hidden anchor
- 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.
Essentially, yes. Granted, you may have to make tweaks to your code for your web server, but the general idea remains the same:
- run the query,
- generate an array,
- convert it to JSON,
- write it to disk,
- send the file to the user
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.