• Hi,
    I have a multiusers website and I would assign the post author automatically to the current logged in user without giving the right to the users doing it manually.

    Thank you in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure I follow. By default, WP always assigns the current user as post author, assuming the user has the capability to create posts. Only admins and editors can manually change authors to other users. They are still assigned as author by default for any posts they create, it’s just that they are able to change it to someone else if they want to.

    Can you describe an example where you don’t want to “give the right to the users doing it manually”?

    Thread Starter twins02

    (@twins02)

    Hi, thank you for your reply.

    So, I already published new posts on the website as DRAFT and without any author assigned to them. Let’s assume I have 2 journalists “A” and “B”. In the Author option in post I will have two names ( A and B) and “A” is the author per default. So even if “B” tried to publish his post it will be on the name of “A”. And I don’t want to give them the access to change the name author, it is hidden. That’s why Im wondering if there is any possibility to publish a post by the connected user and not the author name per default in the options.

    Thank you again

    Moderator bcworkz

    (@bcworkz)

    Ah, I see. Thanks for explaining further.
    The author can be changed to whom ever in the ‘wp_insert_post_data’ filter, which passes the data just about to be saved. Of course you can get the current user with wp_get_curent_user(). Check the user’s capability with current_user_can(), as PHP can set any user as author regardless of capability.

    That alone will set the author for any post insertion or update no matter what, which is probably beyond what you intended. You can check a number of things to decide if the author should be changed or not. New posts do not have the ID field yet assigned. The new status is passed in the data. The current status of posts being updated can be determined by getting the post from the DB, as it has not yet been updated. You probably are looking for a draft status currently and publish status in the passed data.

    • This reply was modified 6 years, 4 months ago by bcworkz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to set post author autoamtically as the current logeed in user’ is closed to new replies.