Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress中更新帖子时更改帖子作者_Php_Wordpress - Fatal编程技术网

Php 在wordpress中更新帖子时更改帖子作者

Php 在wordpress中更新帖子时更改帖子作者,php,wordpress,Php,Wordpress,当一个作者在仪表板中点击一篇文章的“更新”时,我如何让文章作者自动更改为任何作者 我试图使用这段代码在更新帖子但什么也没发生时触发一些东西。有什么想法吗 add_action( 'publish_post', 'changeAuthor' ); function changeAuthor($post_id){ echo "hello"; } 做了更多的研究并得到了答案: 要确保你采取了正确的行动,请使用以下命令 add_action('edit_post', 'functiontoc

当一个作者在仪表板中点击一篇文章的“更新”时,我如何让文章作者自动更改为任何作者

我试图使用这段代码在更新帖子但什么也没发生时触发一些东西。有什么想法吗

add_action( 'publish_post', 'changeAuthor' );

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

做了更多的研究并得到了答案:

要确保你采取了正确的行动,请使用以下命令

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

另外,不要通过回显来测试这一点,因为wordpress会以某种方式重定向回显,而回显不会出现!但是其他任何功能都可以使用:)

这可能是要调用的函数。。。 代码未经测试

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

    }

}

此外,还需要检查当前作者是否与原始作者不同。DB事务中没有不需要的点。