wazzaday
wazzaday

Reputation: 9664

Change post author on post update in wordpress

When an author clicks "update" on a post in the dashboard, how would I have the posts author change automatically to whatever author it was?

I'm trying to use this code to trigger something when a post is updated but nothing happens. Any Ideas?

add_action( 'publish_post', 'changeAuthor' );

function changeAuthor($post_id){
    echo "hello";
}

Upvotes: 3

Views: 5908

Answers (2)

Bart
Bart

Reputation: 515

this could be the function to call... code ist not tested.

add_action('save_post', 'functiontocall');

functiontocall () {
    if ( ! wp_is_post_revision( $post_id ) ){

        $my_post = array(
            'ID'            => $post_id,
            'post_author'   => get_current_user_id(),
        );


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

        // update the post, which calls save_post again
        wp_update_post( $my_post );

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

    }

}

Upvotes: 5

wazzaday
wazzaday

Reputation: 9664

Did some more research and got an answer:

To make sure you hit the right action use the following

add_action('edit_post', 'functiontocall');
add_action('save_post', 'functiontocall');
add_action('publish_post', 'functiontocall');
add_action('edit_page_form', 'functiontocall');

Also, do not test this by echoing something because of some way wordpress redirects the echo will not appear! But anything else works :)

Upvotes: 1

Related Questions