tags:

views:

381

answers:

2

I've added a custom "save_post" action to my theme (code is below). However, when I place images or video code in the post, its stripped away. The only way I can get it to stay is to comment out the add_action line. What do I need to do in order to keep all the post info intact?

add_action('save_post', 'custom_add_save');

function custom_add_save($postID){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['my_customHeader']) 
        {
            update_custom_meta($postID, $_POST['my_customHeader'], 'my_customHeader');
        }
        else
        {
            update_custom_meta($postID, '', 'my_customHeader');
        }
        if ($_POST['my_customTitle']) 
        {
            update_custom_meta($postID, $_POST['my_customTitle'], 'my_customTitle');
        }
        else
        {
            update_custom_meta($postID, '', 'my_customTitle');
        }
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
}
A: 

I'm not exactly very well-versed with the Wordpress hooks related to saving posts, but based on your PHP alone, I'm seeing that your custom_add_save() function is not returning the post id when it's processing a manual save (i.e. when you click the Save Draft / Publish button on the Wordpress UI).

It's certainly returning the post id during an auto-save (as per the first block of code as you enter custom_add_save), though.

Maybe you'd like to look into that. :)

Richard Neil Ilagan
A: 

you also need to add a nonce value to prevent concurrency

add a hidden input in your form:

<input type="hidden" name="customCategory_noncename" id="customCategory_noncename" value="<?= wp_create_nonce('customCategory'); ?>" />

and add this to your save code

// verify this with nonce because save_post can be triggered at other times
    if (!wp_verify_nonce($_POST['_noncename'], 'my_customHeader')) return $post_id;

also by default i think wordpress strips out html formatting in the editor in favour of its own 'smart' html tagging

Rob