1

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.

Below is my process.php file:

<?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 how to send $user_post_redirect back to jQuery so I can redirect to it.

How can I achieve that?

3
  • Please read wordpress.stackexchange.com/questions/9231/… – your method cannot work in WordPress.
    – fuxia
    May 16, 2013 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? May 16, 2013 at 16:54
  • Do what Milo suggested; this is The Way to handle AJAX in WordPress.
    – fuxia
    May 16, 2013 at 16:56

1 Answer 1

4

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' );
5
  • ok, thank you. where do i put add_action( 'wp_ajax_some_action', 'your_process_function' ); May 16, 2013 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, 2013 at 17:08
  • can i add a nonce by putting _ajax_nonce: '<?php echo wp_create_nonce( 'my_ajax_nonce' ); ?>' in the data array? May 16, 2013 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 May 16, 2013 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? May 16, 2013 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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