Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a form that passes field values to jQuery, which validates the fields and creates the variables being sent in data: {} in the snippet below.

$.ajax({
    type: "POST",
    url: "<?php bloginfo('stylesheet_directory'); ?>/process.php",
    dataType: "json",
    data: {
        "title":title,
        "goals":goals,
        "progress":progress,
        "categories":categories,
        "tags":tags,
        "video":video,
        "audio":audio,
    },
    success: function() {
        //get permalink for post from php and go to it
    }
});

process.php is not receiving any data, or it's not posting the data. I'm not sure why. Here is process.php

<?php

    $user_submitted_title = sanitize_text_field($_POST['user_submitted_title']);
    $user_submitted_progress = $_POST['user_submitted_progress'];
    $user_submitted_goals = $_POST['user_submitted_goals'];
    $user_submitted_categories = $_POST['user_submitted_categories'];
    $user_submitted_tags = sanitize_text_field($_POST['user_submitted_tags']);
    $user_submitted_video = sanitize_text_field($_POST['user_submitted_video']);
    $user_submitted_audio = sanitize_text_field($_POST['user_submitted_audio']);

    $user_post = array(
        'comment_status' => 'open',
        'post_author'    => $user_ID,
        'post_category'  => array($user_submitted_categories),
        'post_content'   => '<h2>Project Progress</h2>' . $user_submitted_progress . '<h2>Project Goals</h2>' . $user_submitted_goals,
        'post_status'    => 'publish',
        'post_title'     => $user_submitted_title,
        'post_type'      => 'post',
        'tags_input'     => $user_submitted_tags
    );

    $user_post_id = wp_insert_post($user_post);
    add_post_meta($user_post_id, 'wpcf-video', $user_submitted_video);
    add_post_meta($user_post_id, 'wpcf-audio', $user_submitted_audio);

    $user_post_redirect = get_permalink($user_post_id);

?>

I need to figure out why nothing posts when I use the form, and I need to figure out how to send $user_post_redirect back to jquery so I can redirect to it. Can someone please help?

share|improve this question
    
Please read wordpress.stackexchange.com/questions/9231/… – your method cannot work in WordPress. –  toscho May 16 '13 at 16:49
    
my form is in a page template, my jquery is in a header template, and my php is in a file called process.php. Can that work within those files, or do i have to make a plugin? –  Nick Passaro May 16 '13 at 16:54
    
Do what Milo suggested; this is The Way to handle AJAX in WordPress. –  toscho May 16 '13 at 16:56
add comment

1 Answer

up vote 1 down vote accepted

When you load your process.php file directly, it's not within the context of the WordPress environment, so no WordPress functions are available. WordPress has a native AJAX API that should be used for this sort of thing.

First, enqueue your javascript file, then use wp_localize_script to pass the location of admin-ajax.php, which will be processing the requests:

function wpa_scripts() {
    wp_enqueue_script(
        'wpa_script',
        get_template_directory_uri() . '/js/script.js',
        array('jquery'),
        null,
        true
    );
    $script_data = array(
        'admin_ajax' => admin_url( 'admin-ajax.php' )
    );
    wp_localize_script(
        'wpa_script',
        'wpa_data',
        $script_data
    );
}
add_action( 'wp_enqueue_scripts', 'wpa_scripts' );

Next, add an action to your ajax data, which will map to the function hooked to process the request:

$.ajax({
    type: "POST",
    url: wpa_data.admin_ajax,
    dataType: "json",
    data: {
        "action":"some_action",
    },
    success: function() {
        //get permalink for post from php and go to it
    }
});

Last, add the action in php and hook it to the function which will receive and process it, then move the contents of process.php inside this function

add_action( 'wp_ajax_some_action', 'your_process_function' );
add_action( 'wp_ajax_nopriv_some_action', 'your_process_function' );
share|improve this answer
    
ok, thank you. where do i put add_action( 'wp_ajax_some_action', 'your_process_function' ); –  Nick Passaro May 16 '13 at 16:59
    
in your theme's functions.php file or a plugin, along with the your_process_function that's hooked to the action. –  Milo May 16 '13 at 17:08
    
can i add a nonce by putting _ajax_nonce: '<?php echo wp_create_nonce( 'my_ajax_nonce' ); ?>' in the data array? –  Nick Passaro May 16 '13 at 17:24
    
It still isn't posting. can i send you my functions file, my header template, and my page template? I can put them up on pastebin –  Nick Passaro May 16 '13 at 17:36
    
Nevermind, I got it. sanitize text field was breaking it, because ajax isnt submitting text fields. i'll sanitize those fields before the ajax call. Thank you so much. can you give me a link to your personal website so i can give you credit in the site i'm making? –  Nick Passaro May 16 '13 at 17:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.