Skip to content

Instantly share code, notes, and snippets.

@wpbean
Created November 13, 2024 23:53
Show Gist options
  • Save wpbean/1a5abfea883621b4e150eab1362a420f to your computer and use it in GitHub Desktop.
Save wpbean/1a5abfea883621b4e150eab1362a420f to your computer and use it in GitHub Desktop.
wpb-popup-for-contact-form-7 plugin
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Ajax Class
*/
class WPB_PCF_Ajax {
/**
* Class Constructor.
*/
public function __construct() {
add_action( 'wp_ajax_wpb_pcf_fire_contact_form', array( $this, 'wpb_pcf_fire_contact_form' ) );
add_action( 'wp_ajax_nopriv_wpb_pcf_fire_contact_form', array( $this, 'wpb_pcf_fire_contact_form' ) );
}
/**
* Form Content
*/
public function wpb_pcf_fire_contact_form() {
check_ajax_referer( 'wpb-pcf-button-ajax', 'wpb_pcf_fire_popup_nonce' ); // Verify the nonce.
$pcf_form_id = isset( $_POST['pcf_form_id'] ) ? intval( $_POST['pcf_form_id'] ) : 0;
if ( $pcf_form_id > 0 && get_post_type( $pcf_form_id ) === 'wpcf7_contact_form' ) {
$shortcode = sprintf( '[contact-form-7 id="%d"]', esc_attr( $pcf_form_id ) );
$response = '<div class="wpb-pcf-wpcf7-form">';
$response .= do_shortcode( $shortcode );
$response .= '</div>';
wp_send_json_success( $response );
}else{
wp_send_json_error( esc_html__( 'Invalid CF7 Form ID', 'wpb-popup-for-cf7-lite' ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment