I have created a plugin that creates a custom Post Type called smm_newsletter
, and used that Post Type to create newsletter template and send a newsletter email. Then, I made a meta-box for sending button include in that Post Type's post-new.php/post.php.
The script for wp_localize_script is this
function smm_admin_scripts() {
wp_register_script( 'smm-js-priv-script', plugins_url( '/js/private/smm-builder.js', __FILE__ ), array( 'jquery' ), '', true );
// localize admin URL
$php_localized_priv_script = array( 'admin_url' => admin_url() );
wp_localize_script( 'smm-js-priv-script', 'smm_priv_script', $php_localized_priv_script );
wp_enqueue_script( 'smm-js-priv-script' );
}
add_action( 'admin_enqueue_scripts', 'smm_admin_scripts' );
*I added exactly same localized admin URL to my public scripts function and it work fine in input
submit button. This code too, it work fine in input
submit button.
This is code for that button
// I do not use type="submit" because publish/update button use it.
<input id="smm_newsletter_send" name="smm_newsletter_send" type="button" value="Send Newsletter">
and ajax
jQuery( document ).ready( function($) {
/* Newsletter Section */
// I did localized script for path to wp-admin here
var wpajax_url = smm_priv_script.admin_url + 'admin-ajax.php';
var sendingNewsletter_url = wpajax_url + '?action=smm_newsletter_sender';
// out put form console: http://localhost/wordpressProject/wordpress/wp-admin/admin-ajax.php?action=smm_newsletter_sender
console.log(sendingNewsletter_url);
$( '#smm_newsletter_send' ).on( 'click', function( e ){
e.preventDefault();
$form = $( 'form#post' );
// setup our form data for ajax post
var form_data = $form.serialize();
console.log( form_data ); // here it work find
// submit form data with ajax post
$.ajax({
'method' : 'post',
'url' : sendingNewsletter_url,
'data' : form_data,
'dataType' : 'json',
'catch' : false,
'success' : function( data, textStatus ){
if( data.status == 1 ) {
// success
// notify the user of success
window.location.reload( true );
console.log( data.status + '\n' );
console.log( data.message + '\n' + '——————————' );
} else {
// error
// begin building our error message text
console.log( data.status + ' ' + 'error' + '\n' );
console.log( data.message + '\n' + '——————————' );
}
},
'error' : function( jqXHR, textStatus, errorThrown ) {
// ajax didn't work
console.log('A jQuery Ajax error has occurred! See details below...');
console.log(textStatus);
console.log(errorThrown);
}
});
// stop the form from submitting nornally
return false;
});
});
But, after I clicked that button ajax()
only console.log undefined for me
undefined error
undefined
——————————
So, I tried another method to test my sending newsletter function [smm_newsletter_sender()
] to make sure that function works, by changing $_POST
to $_GET
. And simply placed URL that look like this http://localhost/wordpressProject/wordpress/wp-admin/admin-ajax.php?action=smm_newsletter_sender?post_ID=123
. It worked perfectly, by sending all of newsletter emails and returning the proper JSON. My email sending function is working fine, but the button for send not, why?.
At first, I had suspected about ajax()
, may I did something wrong about it? But I did not found anything wrong. So, may be It about WordPress itself that prevent something that I don't know.