I found this answer on Stack Overflow, answer this exact question.
I've modified it slightly, because I'm using a button
and not an anchor
:
jQuery( ".modal" ).on( "show.bs.modal", function ( e ) {
var target = jQuery( e.relatedTarget ).attr( "data-target" ).replace( "#", "" );
var modal_ids = [
"modal-request-estimate",
"modal-schedule-appointment",
"modal-rate-review"
];
if ( modal_ids.indexOf( target ) == - 1 ) {
return false;
}
jQuery( this ).find( ".modal-body" ).load( location.href + "?modal=" + target );
} );
If then added a PHP callback function to run on template_redirect
in order to check for $_GET[ 'modal' ]
and output the proper content upon valid conditions.
My PHP callback looks like this:
add_action( 'template_redirect', function () {
$modal_id = null;
if ( isset( $_GET['modal'] ) ) {
$modal_id = filter_var( $_GET['modal'], FILTER_SANITIZE_STRING );
}
if ( ! $modal_id ) {
return false;
}
echo render_form( $modal_id );
return true;
} );
Note: render_form();
is a very simple function that exclusively outputs a standard HTML input form.
Just to be clear: I don't always write my callback functions anonymously. I did it just for simplicity of asking this question.
At this point: when the button is clicked, the modal window pops up, the form is rendered in the body of the modal window. However, the entire website's template is also outputted into the modal window.
How do I get it to ONLY output the form into the modal's body, instead of the ENTIRE website's template?