Support » Fixing WordPress » AJAX and Permalinks

  • Hi! I have found some posts where users had similar issues but never found a good answer. Here is my problem: I wrote a plugin that provides shortcodes for email forms (that are submitted with AJAX), but it only sometimes works with pretty permalinks.

    Here is the code:

        Plugin Name: Contact Form
        Plugin URI:
        Description: Put contact forms on your WordPress site.
        Author: S. Tyler
        Version: 1.0
        Author URI:
    // <--- Hooks, shortcodes, and global variables --->
    // <----------------------------------------------->
    Javascript/AJAX method called by shortcode that places form on page. Form is returned as string
    so as to be displayed before the_content. $atts should be supplied in the shortcode in the form of
    function submit_email_form($atts) {
    	extract(shortcode_atts(array('to_email' => ''), $atts));
    	$nonce = wp_create_nonce('nonce_1');
    <script type='text/javascript'>
    	jQuery('#submit_email').click(function() {
    			type: "post",
    			url: "wp-admin/admin-ajax.php",
    			data: {action: 'email_contact', fname: jQuery('#fname').val(), femail: jQuery('#femail').val(), to_email: '<?php echo $to_email; ?>', fsubject: jQuery('#fsubject').val(), fmessage: jQuery('#fmessage').val(), _ajax_nonce: '<?php echo $nonce; ?>'},
    			success: function(data) {
    			error: function(data) {
    				alert("Something is wrong!");
    		return false;
    	$contactform = '<form method="post" id="contactform"><input type="text" name="name" value="Name" id="fname">
            <input type="text" name="email" value="Email" id="femail">
            <input type="text" name="subject" value="Subject" id="fsubject">
    	<textarea name="message" id="fmessage" maxlength="1000" cols="45" rows="10">Message</textarea>
    	<input type="submit" value="Submit" id="submit_email" class="submit"></form><div id="email_submitted"></div>';
    	return $contactform;
    The function that actually sends the email after checking that the message/name/etc. are not empty and that the
    given email is valid.
    function send_email() {
    	$name = $_POST['fname'];
    	$to_email = $_POST['to_email'];
    	$from_email = $_POST['femail'];
    	$subject = $_POST['fsubject'];
    	$message = $_POST['fmessage'];
    	if($name == '' || $to_email == '' || $from_email == '' || $message == '') {
    		echo "One or more of the necessary inputs was left blank. Please try again with a name, email, and message.";
    	$headers  = 'MIME-Version: 1.0' . "\r\n";
    	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    	$headers .= 'From: ' . $from_email;
    	if(validate_email($from_email)) {
    		echo "Your message has been sent.";
    	} else {
    		echo "Please try again with a valid email.";
    function validate_email($email) {
    	if(filter_var($email,FILTER_VALIDATE_EMAIL)) {
    		return true;
    	} else return false;

    It did seem to work for a little while with pretty permalinks but now nothing happens when you click to submit a form. Is there something obvious I’m missing? Thank you!

  • The topic ‘AJAX and Permalinks’ is closed to new replies.