最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Edit a function to take different actions based on user role

programmeradmin3浏览0评论

I want to modify the publishorpend function below to consider the user group of the currently logged in user when deciding whether to set the post status as "pending" or "publish" (in addition to considering whether submission_requires_approval is true or false). For example:

  1. If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
  2. if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
  3. If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending. *this would be ideal but not absolutely necessary
        public function publishorpend( $listing_id ) {
        $listing = \MyListing\Src\Listing::force_get( $listing_id );

        $post_status = mylisting_get_setting( 'submission_requires_approval' ) ? 'pending' : 'publish';

        wp_update_post( [
            'ID' => $listing->get_id(),
            'post_status' => $post_status,
        ] );

    }

I found this function which may be helpful:

function wcmo_get_current_user_roles() {
  if( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;
    return $roles; // This returns an array
    // Use this to return a single value
    // return $roles[0];
  } else {
    return array();
  }
}

Thanks!

I want to modify the publishorpend function below to consider the user group of the currently logged in user when deciding whether to set the post status as "pending" or "publish" (in addition to considering whether submission_requires_approval is true or false). For example:

  1. If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
  2. if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
  3. If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending. *this would be ideal but not absolutely necessary
        public function publishorpend( $listing_id ) {
        $listing = \MyListing\Src\Listing::force_get( $listing_id );

        $post_status = mylisting_get_setting( 'submission_requires_approval' ) ? 'pending' : 'publish';

        wp_update_post( [
            'ID' => $listing->get_id(),
            'post_status' => $post_status,
        ] );

    }

I found this function which may be helpful:

function wcmo_get_current_user_roles() {
  if( is_user_logged_in() ) {
    $user = wp_get_current_user();
    $roles = ( array ) $user->roles;
    return $roles; // This returns an array
    // Use this to return a single value
    // return $roles[0];
  } else {
    return array();
  }
}

Thanks!

Share Improve this question asked May 5, 2020 at 23:48 PatrickPatrick 1
Add a comment  | 

1 Answer 1

Reset to default 0

wp_update_post() ends up using wp_insert_post(), which has wp_insert_post_data inside it. The filter "Filters slashed post data just before it is inserted into the database." I think that would be suitable moment to add the custom logic you described as publishorpend method doesn't seem to have other filters available in it.

You could perhaps do something like this,

add_filter('wp_insert_post_data', 'filter_wp_insert_post_data', 10, 2);

function filter_wp_insert_post_data( $data, $postarr ) {
  // Only affect custom post type. Update the post type slug!
  if ( 'my_post_type' !== get_post_type($data['ID']) ) {
    return $data;
  }

  // You may want to have some extra checks so that the status change doesn't affect every post of this type
  // if ( ! some_theme_or_plugin_function_to_check_if_orphaned($data['ID']) ) {
  //   return $data;
  // }

  // assuming the function returns boolean or truthy/falsy value
  $requires_approval = mylisting_get_setting( 'submission_requires_approval' ); 

  // If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
  if ( ! $requires_approval ) {
    return $data;
  }

  // if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
  // As a personal preference, check capabilities instead of role. Admins and editors can edit_others_posts
  if ( current_user_can( 'edit_others_posts' ) ) {
    $data['post_status'] = 'publish';
  // If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending.
  } else if ( $requires_approval ) {
    $data['post_status'] = 'pending';
  }

  return $data;
}

This is just an example and may need some tweaking to match your exact setup and needs.

发布评论

评论列表(0)

  1. 暂无评论