php - Retrieving Author ID in wp-admin area

Currently able to display the post author ID on the frontend with no difficulties. Returning the ID in wp-admin when ed

Currently able to display the post author ID on the frontend with no difficulties. Returning the ID in /wp-admin when editing a post is proving to be tricky. Here's what I have so far:

$current_user_id = get_current_user_id(); // Get the current users ID
$author_id = get_post_field ('post_author', $post_id); // Get the author ID

if( is_user_logged_in() && $current_user_id == $author_id ) { 
    // wp-dashboard script
}

I've tried a handful of other methods for getting the user ID with no success. Displaying $current_user_id through the theme's functions.php file worked without issues along with other PHP code. Perhaps the ID isn't being loaded when functions.php loads?

Currently able to display the post author ID on the frontend with no difficulties. Returning the ID in /wp-admin when editing a post is proving to be tricky. Here's what I have so far:

$current_user_id = get_current_user_id(); // Get the current users ID
$author_id = get_post_field ('post_author', $post_id); // Get the author ID

if( is_user_logged_in() && $current_user_id == $author_id ) { 
    // wp-dashboard script
}

I've tried a handful of other methods for getting the user ID with no success. Displaying $current_user_id through the theme's functions.php file worked without issues along with other PHP code. Perhaps the ID isn't being loaded when functions.php loads?

Share Improve this question asked Jun 10, 2019 at 7:05 Josh CalicoJosh Calico 132 bronze badges 3
  • In which action hook you are trying to do this? – maheshwaghmare Commented Jun 10, 2019 at 7:59
  • I think you don't need to use is_user_logged_in() because get_current_user_id() return 0 for non logged in user. – maheshwaghmare Commented Jun 10, 2019 at 8:03
  • I should have clarified that I'm using this for the plugin co-authors plus so that I can allow authors to assign other authors. After placing an edit_cap filter: github/Automattic/Co-Authors-Plus/issues/… The problem was that co-authors could take over a given post from the original author. As a result I needed a way to have the add_filter to execute only if the page author is viewing the post. – Josh Calico Commented Jun 10, 2019 at 17:51
Add a comment  | 

1 Answer 1

Reset to default 1

It can be done like this without having to rely on the $post global.

if ( is_admin() ) {
    if( isset( $_GET['post'] ) && isset( $_GET['action'] ) && $_GET['action'] === 'edit' ) {
        $post_id = $_GET['post'];
        $current_post = get_post($post_id);
        $author_id = $current_post->post_author;
    } 
}

The query string variable post is always the post_id you are editing and additionally you need to check if the action query variable is set and it equals to edit.

Additionally you can explore the get_current_screen() function to setup more advanced conditionals for better security.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745420677a4626951.html

相关推荐

  • php - Retrieving Author ID in wp-admin area

    Currently able to display the post author ID on the frontend with no difficulties. Returning the ID in wp-admin when ed

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信