Skip to content

Instantly share code, notes, and snippets.

@dancameron
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save dancameron/bb9dfab632682867d9ac to your computer and use it in GitHub Desktop.

Select an option

Save dancameron/bb9dfab632682867d9ac to your computer and use it in GitHub Desktop.
Shortcode to retrieve remote form - ninjademo.com
<?php
/**
* Add the remote form via AJAX or php get.
* Shortcode is [sa-create-demo-form]
* Currently no attributes are used even though they could be used
* so that the $demo_url variable doesn't need to be updated manually below.
*
* @param array $atts shortcode atts
* @return string Create demo form.
*/
function create_ninja_demo( $atts = array() ) {
// URL of Demo site, used for the form action
$demo_url = 'http://sandbox.sproutapps.co/sprout-invoices';
// ajax url, with ajax action to return the form
$endpoint = $demo_url.'/wp-admin/admin-ajax.php?action=try_demo_form';
// AJAX Method
ob_start(); ?>
<span id="ninja_demo_creation_form"></span>
<script>
jQuery.ajax({
url: '<?php echo $endpoint ?>',
success: function( result ) {
// Result form
var $form = jQuery( result );
// Add the domain to the form action, since ninja forms wants to be obtuse.
$form.find( 'form' ).attr( 'action', '<?php echo $demo_url ?>/#ninja-demo' );
// Some classes for myself
$form.find( 'input[type="submit"]' ).addClass( 'button button-small' );
// Drop the form into the span above.
jQuery( "#ninja_demo_creation_form" ).html( $form );
}
});
</script>
<?php
$form = ob_get_clean();
/*/
// Use PHP instead of AJAX. Problem, it's slow...
$form = wp_remote_get( $endpoint );
// Add classes
$form = str_replace( 'type="submit"', 'type="submit" class="button button primary"', $form );
// Add the domain to the action
$form = str_replace( '#ninja-demo', $demo_url . '/#ninja-demo', $form );
// return the form
$form = wp_remote_retrieve_body( $form );
/**/
return $form;
}
add_shortcode( 'sa-create-demo-form', 'create_ninja_demo' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment