Skip to content

Instantly share code, notes, and snippets.

@marklchaves
Last active February 17, 2025 12:49
Show Gist options
  • Save marklchaves/bb8ca21d242384d587a5636110bb24af to your computer and use it in GitHub Desktop.
Save marklchaves/bb8ca21d242384d587a5636110bb24af to your computer and use it in GitHub Desktop.
Send a query parameter to a form inside a Popup Maker popup
<?php // Ignore this first line when copying to your child theme's functions.php file.
/**
* Based on https://youtu.be/zml-xz78o2Q
*/
function send_fields_to_a_form() { ?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
const popupID = 1689, // Change to your popup ID.
fieldSelector = '#workshop-name'; // Change to your form field selector.
$(document).on('pumBeforeOpen', '#pum-'+popupID, function () {
const trigger = $.fn.popmake.last_open_trigger[0].firstChild, // Grab the popup trigger. Tweak as needed depending on where you place your trigger.
field = $(fieldSelector); // Prepare the destination field.
// Add more fields as needed.
// e.g. field2 = $(fieldSelector2);
// If the trigger has a link, continue.
if (trigger && "" !== trigger.href) {
const url = new URL(trigger.href); // Grab the link.
const searchParams = new URLSearchParams(url.search); // Grab the query parameters.
field.val(searchParams.get('workshop')); // Set the destination field the workshop query parameter.
field.attr('readonly', true); // Set the field to read only for display purposes.
// Grab more query parameters as needed using the get() function like above.
// e.g. searchParams.get('wochentag');
} // if
}); // listener
}); // jQuery
</script>
<?php }
add_action( 'wp_footer', 'send_fields_to_a_form', 500 ); // Load the script in the footer with a "late" priority.
/**
* You can add the PHP code snippet to your child theme's functions.php file
* or with third-party plugins such as My Custom Functions and Code Snippets.
*
* Learn more:
* - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-js/
* - https://wppopupmaker.com/docs/getting-started-with-custom-code/getting-started-with-custom-php/
*/
@marklchaves
Copy link
Author

marklchaves commented Jun 2, 2022

Screen Captures

popup-maker-cf7-fields-test-click-open-link

popup-maker-cf7-workshop-field

popup-maker-cf7-fields-test-result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment