Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change post author on post update in wordpress

Tags:

php

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";
}
like image 715
wazzaday Avatar asked Dec 29 '25 23:12

wazzaday


2 Answers

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');

    }

}
like image 112
Bart Avatar answered Dec 31 '25 11:12

Bart


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 :)

like image 24
wazzaday Avatar answered Dec 31 '25 12:12

wazzaday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!