Last active
June 21, 2022 16:15
-
-
Save damiencarbery/ee04aa2a4bfb207276d225a00c275db9 to your computer and use it in GitHub Desktop.
Ajax script generator - Easily create starter ajax js and php code - https://www.damiencarbery.com/2018/11/ajax-script-generator/
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
jQuery(document).ready( function( $ ){ | |
$( '#plugin_php_code' ).html( 'Waiting for PHP code...' ); | |
$( '#plugin_js_code' ).html( 'Waiting for JavaScript code...' ); | |
jQuery( '#generate_ajax_code' ).on('click', function( e ) { | |
//jQuery( 'header' ).on('click', function( e ) { | |
e.preventDefault(); | |
$( '#plugin_php_code' ).html( 'Waiting for PHP code...' ); | |
$( '#plugin_js_code' ).html( 'Waiting for JavaScript code...' ); | |
jQuery.ajax({ | |
url : ajax_code_generator_js_obj.ajax_url, // Note that 'aj_ajax_demo' is from the wp_localize_script() call. | |
type : 'post', | |
data : { | |
action : 'ajax_code_generator_name', // Note that this is part of the add_action() call. | |
submitted_nonce : ajax_code_generator_js_obj.the_nonce, // Note that 'aj_demo_nonce' is from the wp_localize_script() call. | |
script_filename : $( '#script_filename' ).val(), | |
ajax_handler_function : $( '#ajax_handler_function' ).val(), | |
script_handle : $( '#script_handle' ).val(), | |
ajax_handler_name : $( '#ajax_handler_name' ).val(), | |
ajax_js_object_name : $( '#ajax_js_object_name' ).val(), | |
}, | |
success : function( response ) { | |
console.log( response ); | |
// Display both pieces of returned data in their respective 'pre' blocks. | |
$( '#plugin_php_code' ).html( response.data.ajax_php_code ); | |
$( '#plugin_js_code' ).html( response.data.ajax_js_code ); | |
}, | |
error : function( response ) { | |
alert('Error retrieving the information: ' + response.status + ' ' + response.statusText); | |
console.log( response ); | |
} | |
}); | |
}); | |
}); |
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
<?php | |
/* | |
Plugin Name: AJAX Code Generator | |
Plugin URI: https://www.damiencarbery.com/2018/11/ajax-script-generator/ | |
Description: Ajax code generated by code. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.1 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
add_shortcode( 'ajax_generator_form', 'dcwd_ajax_generator_form' ); | |
function dcwd_ajax_generator_form() { | |
$form = <<< EOF | |
<form> | |
<p><label for="script_filename">Script Filename</label><input type="text" name="script_filename" id="script_filename" required="required" value="my_ajax_code.js" /> <span>What is the name of your JS file?</span></p> | |
<p><label for="ajax_handler_function">Ajax Handler Function</label><input type="text" name="ajax_handler_function" id="ajax_handler_function" required="required" value="my_ajax_function" /> <span>What is the name of the php function that will be called?</span></p> | |
<p>The following are internal names that you can customise but do not need to.</p> | |
<p><label for="script_handle">Script Handle</label><input type="text" name="script_handle" id="script_handle" required="required" value="script_handle" /></p> | |
<p><label for="ajax_handler_name">Ajax Handler Name</label><input type="text" name="ajax_handler_name" id="ajax_handler_name" required="required" value="ajax_handle_name" /></p> | |
<p><label for="ajax_js_object_name">Ajax Javascript Object Name</label><input type="text" name="ajax_js_object_name" id="ajax_js_object_name" required="required" value="ajax_js_obj" /></p> | |
</form> | |
<button id="generate_ajax_code">Generate Code</button> | |
<pre id="plugin_php_code" class="ajax_code">Waiting for PHP code...</pre> | |
<pre id="plugin_js_code" class="ajax_code">Waiting for JavaScript code...</pre> | |
EOF; | |
return $form; | |
} | |
add_action( 'wp_enqueue_scripts', 'dcwd_admin_enqueue_script' ); | |
function dcwd_admin_enqueue_script() { | |
wp_enqueue_script( 'ajax_code_generator', plugin_dir_url( __FILE__ ). 'ajax-code-generator.js', array('jquery') ); | |
wp_localize_script( 'ajax_code_generator', 'ajax_code_generator_js_obj', array( | |
'ajax_url' => admin_url( 'admin-ajax.php' ), | |
'the_nonce' => wp_create_nonce('MY_NONCE_VAR') | |
)); | |
} | |
add_action( 'wp_ajax_nopriv_ajax_code_generator_name', 'ajax_code_generator_func' ); | |
add_action( 'wp_ajax_ajax_code_generator_name', 'ajax_code_generator_func' ); // For logged in users. | |
function ajax_code_generator_func() { | |
check_ajax_referer( 'MY_NONCE_VAR', 'submitted_nonce' ); // This function will die if submitted_nonce is not correct. | |
$script_filename = sanitize_text_field( $_POST[ 'script_filename' ] ); | |
$ajax_handler_function = sanitize_text_field( $_POST[ 'ajax_handler_function' ] ); | |
$script_handle = sanitize_text_field( $_POST[ 'script_handle' ] ); | |
$ajax_handler_name = sanitize_text_field( $_POST[ 'ajax_handler_name' ] ); | |
$ajax_js_object_name = sanitize_text_field( $_POST[ 'ajax_js_object_name' ] ); | |
$php_code = htmlentities ( "<?php | |
/* | |
Plugin Name: Generated AJAX | |
Plugin URI: http://www.damiencarbery.com | |
Description: Ajax code generated by code. | |
Author: The Author | |
Version: 0.1 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
add_action( 'wp_enqueue_scripts', 'dcwd_ajax_enqueue_script' ); | |
function dcwd_ajax_enqueue_script() { | |
wp_enqueue_script( '". $script_handle . "', plugin_dir_url( __FILE__ ). '" . $script_filename . "', array('jquery') ); | |
wp_localize_script( '" . $script_handle . "', '" . $ajax_js_object_name . "', array( | |
'ajax_url' => admin_url( 'admin-ajax.php' ), | |
'the_nonce' => wp_create_nonce('MY_NONCE_VAR') | |
)); | |
} | |
add_action( 'wp_ajax_nopriv_" . $ajax_handler_name . "', '" . $ajax_handler_function . "' ); | |
add_action( 'wp_ajax_" . $ajax_handler_name . "', '" . $ajax_handler_function . "' ); // For logged in users. | |
function " . $ajax_handler_function . "() { | |
check_ajax_referer( 'MY_NONCE_VAR', 'submitted_nonce' ); // This function will die if submitted_nonce is not correct. | |
//\$post_type = sanitize_text_field(\$_POST['post_type']); | |
// How to return multiple pieces of data. | |
\$response = array( 'first_message' => 'Hello', 'second_message' => 'This is the second message.' ); | |
wp_send_json_success( \$response ); | |
wp_send_json_error(); | |
wp_die(); | |
}" ); | |
$js_code = "jQuery(document).ready( function( $ ){ | |
jQuery( 'header' ).on( 'click', function(e) { // Click on the <header> area. | |
e.preventDefault(); | |
jQuery.ajax({ | |
url : " . $ajax_js_object_name . ".ajax_url, | |
type : 'post', | |
data : { | |
action : '" . $ajax_handler_name . "', // Note that this is part of the add_action() call. | |
submitted_nonce : " . $ajax_js_object_name . ".the_nonce, | |
//post_type : post_type | |
}, | |
success : function( response ) { | |
// Display both pieces of returned data in different alert boxes. | |
alert( response.data.first_message ); | |
alert( response.data.second_message ); | |
}, | |
error : function( response ) { | |
alert('Error retrieving the information: ' + response.status + ' ' + response.statusText); | |
console.log( response ); | |
} | |
}); | |
}); | |
});"; | |
$response = array( 'ajax_php_code' => $php_code, 'ajax_js_code' => $js_code ); | |
wp_send_json_success( $response ); | |
wp_send_json_error(); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment