M
M
mr_firuzinho2016-02-15 11:20:41
CMS
mr_firuzinho, 2016-02-15 11:20:41

How to add or add a function?

Help me please, there is a code for adding an object to bookmarks:

if ( ! function_exists( 'cs_addto_usermeta_carosel' ) ) :
  function cs_addto_usermeta_carosel() {
    $user = cs_get_user_id();
    if(isset($user) && $user <> ''){
      if(isset($_POST['post_id']) && $_POST['post_id'] <> ''){

        $cs_wishlist = cs_get_user_meta();
        $cs_wishlist = (isset($cs_wishlist) and is_array($cs_wishlist)) ? $cs_wishlist : array();
        if ( isset ( $cs_wishlist ) && in_array( $_POST['post_id'] , $cs_wishlist )) {
          $post_id = array();
          $post_id[] = $_POST['post_id'];
          $cs_wishlist = array_diff( $post_id , $cs_wishlist );
          cs_update_user_meta( $cs_wishlist );
          echo '<i class="icon-star-o"></i>'; 
          die();
        }

        $cs_wishlist = array();
        $cs_wishlist =  get_user_meta(cs_get_user_id(),'cs-directory-wishlist', true);
          $cs_wishlist[] = $_POST['post_id'];
          $cs_wishlist = array_unique($cs_wishlist);
          update_user_meta(cs_get_user_id(),'cs-directory-wishlist',$cs_wishlist);
          $user_watchlist = get_user_meta(cs_get_user_id(),'cs-directory-wishlist', true);

          $cs_icon = '<i class="icon-star2"></i>';
          $cs_pop_msg = '<div class="outerwrapp-layer">'.__('Added to Favourite','directory').'</div>';

          $cs_html = array(
                  'icon' => $cs_icon,
                  'msg' => $cs_pop_msg,
                 );
          $cs_json = json_encode($cs_html);
          echo $cs_json;
        }
    } else {
      _e('You have to login first.','directory');
    }
    die();  
  }
endif;

add_action("wp_ajax_cs_addto_usermeta_carosel", "cs_addto_usermeta_carosel");
add_action("wp_ajax_nopriv_cs_addto_usermeta_carosel", "cs_addto_usermeta_carosel");

The object is added to bookmarks only when the user has registered, I need this function to work only for certain roles. For example, I want to disable this feature for the subscriber role. Please help me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2016-02-15
@mr_firuzinho

function cs_addto_usermeta_carosel() {
$user_data = get_userdata( get_current_user_id() );
if( 'subscriber' === $user_data->roles[0] ) 
wp_die();
/** Остальной код **/
}

also this is an action, it is superfluous in your case
But in general, using user roles directly is not very convenient, it is much more convenient to use the features right away.

N
newobj, 2016-02-15
@newobj

What are roles? how do you store them?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question