Use PHP Buffer to Insert an ACF form into Shortcodes Ultimate Accordion

I love Advanced Custom Fields and Shortcodes Ultimate, but making them play well together can be elusive. If you’re like me, you may think that the following code will place an ACF form inside a Shortcodes Spoiler:

echo do_shortcode('[su_accordion][su_spoiler title="New Post" style="fancy"]' . acf_form($args) . '[/su_spoiler][/su_accordion]');

But because of the way the form renders, it ends up outside of the accordion. The trick is to use PHP output buffering to assign the form output to a variable first. Then place the variable inside the shortcode:

How to Load External Data Into a Fiddle

If you attempt to load data from an external domain into a Fiddle you’re likely going to receive an XMLHttpRequest error that looks something like “Origin is not allowed by Access-Control-Allow-Origin”. You can use JSFiddle’s echo requests if you just want to simulate returning data, but sometimes you want to demonstrate an actual retrieval.

Legitimate security concerns regarding cross-site HTTP requests are the reason why you can’t simply read in any old file. However, using appropriate page headers makes accessing files from other domains possible. So, if we can set page headers in a file that returns our data, it should be accessible, right? Right!

This can’t be done using a JSON file, but it can be done in a PHP file that is setup to output JSON. The PHP code would look something like this:

// Allow access from anywhere. Can be domains or * (any)

// Allow these methods of data retrieval
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');

// Allow these header types
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

// Create our data object in that crazy unique PHP way
$arr = array(array("id" => 1, "character" => "Minstrel", "line" => "And Saint Attila raised the hand grenade up on high, saying, 'O Lord, bless this thy hand grenade, that with it thou mayst blow thine enemies to tiny bits, in thy mercy.' And the Lord did grin."));

// Return as JSON
echo json_encode($arr);

Note that you don’t have to use PHP. You can use just about any server side scripting language; ASP, .NET, ColdFusion, Ruby, etc., as long as you set the headers properly.

Check out an example:
[iajsfiddle fiddle=”bNyqY” height=”200px” width=”100%” show=”result,html,js,css” skin=”default”]