Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active March 11, 2022 10:41
Show Gist options
  • Save wpmudev-sls/159150f2b5b2fb195b5d405554528eee to your computer and use it in GitHub Desktop.
Save wpmudev-sls/159150f2b5b2fb195b5d405554528eee to your computer and use it in GitHub Desktop.
[Forminator Pro] - Custom Prefill shortcode
<?php
/**
* Plugin Name: [Forminator Pro] - Custom Prefill shortcode
* Description: Adds the [wpmudev_forminator_form] which accepts custom prefill values passed as a discrete parameters, for example [wpmudev_forminator_form id="6" foo="bar" bar="baz"]
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-3388
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_shortcode( 'wpmudev_forminator_form', function( $atts = array() ) {
if ( empty( $atts['id'] ) ) {
return;
}
// Backup the $_GET/$_REQUEST data.
$base_get = $_GET;
$base_request = $_REQUEST;
$id = $atts['id'];
unset( $atts['id'] );
foreach ( $atts as $name => $value ) {
$_GET[ $name ] = $value;
$_REQUEST[ $name ] = $value;
}
// NOTE: This only works if the target form has previously configured its
// "Pre-populate" field attributes.
$html = do_shortcode( sprintf( '[forminator_form id="%d"]', $id ) );
// Restore the original data.
$_GET = $base_get;
$_REQUEST = $base_request;
return $html;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment