I’m going to pretend that I’m walking myself through the answer for the first time so that you will hopefully learn how to answer questions like this yourself in the future.
The first thing I would do is try the shortcode the way you did it:
[gdoc key="ABCDEFG" datatables_buttons="copy, csv, print"]
Well, crap, that didn’t work. Does the plugin’s own documentation describe how to deal with custom DataTables options? Why, yes, it does. Again, quoting from the FAQ:
Some DataTables options need to be given JavaScript array literals
Hmm, okay. Does DataTables’s buttons
option require this? Why, yes, according to DataTables documentation, it does.
Okay, so what exactly is an array (of strings) in JSON? According to the JSON syntax specificaiton, it is an opening square bracket ([
), followed by a double-quote ("
), followed by the string (like copy
or csv
or whatever), followed by a double-quote ("
), optionally followed by a comma (,
) and any number of additional double-quoted strings ("print"
or whatever), and finally a closing square bracket (]
). For example:
["copy","csv","print"]
Will this work inside of a WordPress shortcode? Well, if we just put it in there, the shortcode would become this:
[gdoc key="ABCDEFG" datatables_buttons="["copy","csv","print"]"]
If I didn’t know any better, I might try this, and I would of course notice that it did not work. Why not? Well, first of all, WordPress might see the value of the datatables_buttons
attribute as [
. Why?
datatables_buttons="[" <-- The same double-quote opening the JSON string ends the shortcode attribute value. Oops.
^^
||—the attribute value itself
start the attribute value
Let’s see if the plugin’s documentation has mentioned anything about this (probably common) issue. Oh, look at that, it does. Quoting from literally the same FAQ answer as before:
Note that when a JSON string literal is supplied in a shortcode attribute ("desc"
), it must be double-quoted, so the shortcode attribute value itself must be single-quoted.
Let’s change our shortcode so it is now single-quoted and leave the JSON strings alone so that they can still be double-quoted:
[gdoc key="ABCDEFG" datatables_buttons='["copy","csv","print"]']
Better, but, damn, why won’t this work? It’s because WordPress treats [
and ]
as start-shortcode and end-shortcode special characters, so we can’t use [
or ]
inside of a shortcode. Wow, that’s frustrating! Too bad there isn’t any documentation from the plugin developer about this (also very common) situation, right? Oh, wait, still the exact same FAQ that I’ve been referring to this whole time says this:
using square brackets ([
and ]
) inside a shortcode confuses the WordPress parser, so these characters must be URL-escaped (into %5B
and %5D
, respectively)
Okay, so let’s just replace the square brackets in our shortcode with the appropriate escape sequence we learned by, y’know, just reading the FAQ. Now our shortcode looks like this:
[gdoc key="ABCDEFG" datatables_buttons='%5B"copy","csv","print"%5D']
Wow! It worked! How could it be that the answer to frequently asked questions is just to read the answers already published? CRAZY TOWN AMIRITE? ??