Last active
March 4, 2017 06:10
-
-
Save dianjuar/97faf6223f2f15f6d84a64d9433575ec to your computer and use it in GitHub Desktop.
SublimeText Snipets to create a template to make an AJAX request in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<snippet> | |
<content><![CDATA[ | |
/** | |
* @param {string} security_nonce | |
* Code of the Nonce. Security Stuff | |
*/ | |
jQuery(function(\$) | |
{ | |
// The call trigered | |
\$('${1:#element_to_catch}').click(function(event) | |
{ | |
//---------------- ajax call ---------------- | |
\$.ajax( | |
{ | |
url: ajaxurl, | |
type: 'POST', | |
dataType: 'json', | |
data: | |
{ | |
//the WP actions that is going to receive this | |
action: '${2:ajax_action_example}', | |
//security stuff | |
security_nonce: security_nonce, | |
// Custom data | |
}, | |
// timeout: 30000, | |
success: function( response ) | |
{ | |
if(response.success) | |
console.log( 'succ: ', response ); | |
else | |
console.log( 'fail: ', response ); | |
}, | |
// Conection Error | |
error: function( error ) | |
{ | |
console.log( 'Conection error: ', error ); | |
} | |
}); | |
//---------------- ajax call ---------------- | |
}); | |
}); | |
]]></content> | |
<!-- Optional: Set a tabTrigger to define how to trigger the snippet --> | |
<tabTrigger>WP_ajax</tabTrigger> | |
<!-- Optional: Set a scope to limit where the snippet will trigger --> | |
<scope>source.js</scope> | |
</snippet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<snippet> | |
<content><![CDATA[ | |
# Enqueue the JS scripts | |
add_action( 'admin_enqueue_scripts', | |
array(&\$this, '${1:enqueueScripts}') ); | |
# Ajax listener, teacher crate a payment request | |
add_action('wp_ajax_${2:ajax_action_example}', | |
array(&\$this, '${3:ajax_request_receiver}') ); | |
/** | |
* Enqueue the JavaScript files | |
*/ | |
public function $1() | |
{ | |
#---------------WHERE TO PUT THE SCRIPTS ON--------------- | |
# Here you will specify where you want to enqueue the js file to make the ajax request | |
/*global \$current_screen; | |
# Only when the user is viewing the owlo_paid_to_teacher screen | |
if( \$current_screen->post_type !== 'post' ) | |
return;*/ | |
#---------------WHERE TO PUT THE SCRIPTS OFF -------------- | |
wp_enqueue_script( '${4:ajax_request}_script', | |
${5:AJAXEX_PLUGIN_URL}.'$4.js', | |
array( 'jquery' ), | |
'0.0', | |
true ); | |
# Send the parameters to the script | |
# Security stuff. | |
\$security_nonce = wp_create_nonce( IS_NONCE ); | |
wp_localize_script( '$4_script', | |
'security_nonce', | |
\$security_nonce ); | |
} | |
/** | |
* Function to attend the ajax call | |
*/ | |
public function $3() | |
{ | |
# ------------------------- SECURITY VALIDATION ON ----------------------------- | |
/** | |
* Checks if the nonce of the ajax is valid | |
* | |
* First parameters comes for the wp_nonce_field string | |
* and the second one comes for the ajax parameter | |
*/ | |
if( !check_ajax_referer(IS_NONCE, 'security_nonce') ) | |
return wp_send_json_error( 'Invalid Nonce' ); | |
# ------------------------- SECURITY VALIDATION OFF ----------------------------- | |
# ------------------------ BUSINESS LOGIC ON --------------------------------------- | |
# Debug | |
#wp_send_json_error(\$_POST); | |
# On success | |
#wp_send_json_success(); | |
# On Error | |
#wp_send_json_error(); | |
# ------------------------ BUSINESS LOGIC OFF -------------------------------------- | |
} | |
]]></content> | |
<!-- Optional: Set a tabTrigger to define how to trigger the snippet --> | |
<tabTrigger>WP_ajax</tabTrigger> | |
<!-- Optional: Set a scope to limit where the snippet will trigger --> | |
<scope>source.php</scope> | |
</snippet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment