Skip to content

Instantly share code, notes, and snippets.

@dsebao
Created August 12, 2019 13:11
Show Gist options
  • Save dsebao/28b687acd935a1183b0f47ef0f1c2150 to your computer and use it in GitHub Desktop.
Save dsebao/28b687acd935a1183b0f47ef0f1c2150 to your computer and use it in GitHub Desktop.
<?php
//In ajax.php
function sendPublicform(){
if($_POST['typeform'] == 'sometypeform'){
//some actions
wp_send_json(array('success' => true,'message' => __('Saved','themedomain')));
}
}
add_action('wp_ajax_sendPublicform', 'sendPublicform');
add_action('wp_ajax_nopriv_sendPublicform', 'sendPublicform');
$('#button').on('click', function(event) {
$.ajax({
type: 'POST',
url: jsvar.ajaxurl,
dataType: 'json',
data: mydata,
global: true,
success: function(data){
console.log(data);
},
error: function(e){
console.log(e);
}
});
)};
<?php
//In app.php
function add_scripts(){
wp_register_script('main-js', get_template_directory_uri() . "/src/js/main.js");
global $messagesajax;
$paramsLogin = array(
'msg' => $messagesajax,
'ajaxurl' => admin_url('admin-ajax.php'),
);
wp_localize_script('main-js','jsvar',$paramsLogin);
wp_enqueue_script('main-js');
}
add_action( 'wp_enqueue_scripts', 'add_scripts' );
<?php
//In functions, register the script and enqueue
include_once(get_stylesheet_directory() . '/app.php');
include_once(get_stylesheet_directory() . '/ajax.php');
@dsebao
Copy link
Author

dsebao commented Aug 12, 2019

// I tried to test this but when i need to receive large content (chunks of html) this is not to handy

$messagesajax = array(
	'signup-1' => __( 'We just sent you an email to verify your user. Please check your inbox.', 'growlink' ),
	'Wrong credentials' => __('Wrong credentials','growlink'),
);

wp_register_script('main-js', get_template_directory_uri() . "/src/js/main.js");

global $messagesajax;
$paramsLogin = array(
    'msg' => $messagesajax,
    'ajaxurl' => admin_url('admin-ajax.php'),
);
wp_localize_script('main-js','jsvar',$paramsLogin);
wp_enqueue_script('main-js');

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