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'm reading into the save_post Codex and I find that you need to check the $_REQUEST['post_meta_data'] in the edit window to hook into save_post.

I altered the example code:

function av_subscribe_set_parent($post_id){
/* in production code, $slug should be set only once in the plugin,
   preferably as a class property, rather than in each function that needs it.
 */
$slug = 'page';

/* check whether anything should be done */
$_POST += array("{$slug}_edit_nonce" => '');
if ( $slug != $_POST['post_type'] ) {
    return;
}
if ( !current_user_can( 'edit_post', $post_id ) ) {
    return;
}
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"],
                       plugin_basename( __FILE__ ) ) )
{
    return;
}

/* Request passes all checks; update the post's metadata */
if ($_REQUEST['_wp_page_template'] == 'av_comment_subscribe.php') {
    update_post_meta($post_id, 'post_parent', 13570);
}

So basically, I'm checking if the page template is set to my custom template and then altering the post_parent meta data if true.

This is not working.

share|improve this question

1 Answer 1

You can check the request value, before save and get an hint via wp_die()

add_action( 'save_post','wpse46583_save', 10, 2 );
function wpse46583_save( $post_id, $post ) {

    // verify this is not an auto save routine. 
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;

    // You should check nonces and permissions here
    if ( ! current_user_can( 'edit_page', $post_id ) )
        return;

    if ( $_REQUEST['page_template'] !== 'page-contact.php' ) {
        // No page template assigned - do something here.
        wp_die( 'wrong template' );
    }

    return;
}

see Gist 3226847

share|improve this answer
    
I get the 'wrong template' error. I also tried with $_REQUEST['_wp_page_template'] with the same error. The template is definitely assigned to the page. Hm –  AlxVallejo Aug 1 '12 at 14:07
    
The template string is really right? –  bueltge Aug 1 '12 at 14:49
    
absolutely, in my case I'm using av_comment_subscribe.php which resides along with all the other child-theme template files. –  AlxVallejo Aug 1 '12 at 14:50
    
Also, I'm confused as to what exactly $_REQUEST is checking. Can it be any metadata? What about for example, 'category'? I'm trying to find a workaround... –  AlxVallejo Aug 1 '12 at 17:11
    
Have you seen via var_dump($_REQUEST) was was inside after save page? –  bueltge Aug 1 '12 at 19:23

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.