Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

$_POST varaible is not being populated with newly created 'page' data. Any ideas?

Trying to access the $_POST variable in this function to create an Audit post but the variable is not being sent through!

Code below:

<?php

// BUG: $_POST variable is not populated but only on the first save...
function sliced_create_audit_trail( $post_id ) {

    global $post;

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    /* Get the post type object. */
    //$post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    //if ( !current_user_can( $post_type->cap->edit_page, $post_id ) ) return $post_id;

    // OK, we're authenticated: we need to find and save the data

    /* Get the post type object. */
    if ( $_GET['post_type'] == 'page' && current_user_can( 'edit_pages' )  ) {

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'sliced_create_audit_trail');

        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) ) return;

        $newPost = array(
              'post_title'     => wp_strip_all_tags( $_POST['page_title'] ), // The title of your post.
              'post_type'      => 'sliced_audit_cpt',// Default 'post'. made, in GMT.
        ); 

        wp_insert_post( $newPost );

        // re-hook this function
        add_action('save_post', 'sliced_create_audit_trail');

    }
    return $post_id;
}
add_action( 'save_post', 'sliced_create_audit_trail' );

?>

1 Answer

Liam Maclachlan
Liam Maclachlan
22,805 Points

Don't worry!

The $_post variable is not availble on the initial save. Have to use global $post_data instead.

Or, as the codex points out:

"save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited. For example, quick edits use $_POST.

Since this action is triggered right after the post has been saved, you can easily access this post object by using get_post($post_id)"

<?php 

function sliced_create_audit_trail( $post_id ) {

    /* Get the post type object. */
    $the_post = get_post($post_id);

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // OK, we're authenticated: we need to find and save the data

    if ( $the_post->post_type == 'page' && current_user_can( 'edit_pages' )  ) {

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'sliced_create_audit_trail');

        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) ) return;

        $newPost = array(
              'post_title'     => $the_post->post_title, // The title of your post.
              'post_type'      => 'sliced_audit_cpt',// Default 'post'. made, in GMT.
        ); 

        wp_insert_post( $newPost );

        // re-hook this function
        add_action('save_post', 'sliced_create_audit_trail');

    }

    return $post_id;

}
add_action( 'save_post', 'sliced_create_audit_trail' );

?>