Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

Wishlist Add and Remove in WordPress Custom Code for Mobile/Rest API Development

Wishlist Add and Remove in WordPress Custom Code for Mobile/Rest API Development

 In this article, we are going to explain how to add and remove the wishlist using custom code in WordPress and also for mobile application API Development. 

 $post_id= "current post id";
 $current_user = get_user_by('ID', "curent user id");
 $user_meta_key = "User-Meta-Value-You-need-to-display";

 $neighborhood_id = get_user_meta($current_user->ID, $user_meta_key, TRUE);

 Store Current Post Id in Array
  $post_id1[]=$post_id; 

Condition to check Values exist in user meta table and if already exist we are going to remove it else we are going to add the Value in user meta table 

if (in_array($post_id, $neighborhood_id))
  {
      $keys = array_search($post_id, $neighborhood_id);
       unset($neighborhood_id[$keys]);
       
        update_user_meta($current_user->ID, 'bookmarks', $neighborhood_id );
         echo "Whishlist Removed Successfully"; 
  }
else
  {
      
      
      if (!empty($neighborhood_id) && $neighborhood_id > 0){
    $bookmarks2=array_merge($post_id1,$neighborhood_id);
   }else{
        $bookmarks2 = "";
   }
   
   
   if( $bookmarks2 != "" ) {
        
       update_user_meta($current_user->ID, 'bookmarks', $bookmarks2 );
         echo "Whishlist Added Successfully";
    } else {
        add_user_meta(  $current_user->ID, 'bookmarks', $post_id1 );
         echo "Whishlist Added Successfully";
    }
  }
   

Add the following code in functions.php file  

function add_user_bookmarks() {
       $userid=$_REQUEST['userid'];
        $post_id=$_REQUEST['post_id'];
        $key=$_REQUEST['key'];
        $current_user = get_user_by('ID', $userid);
      // print_r($current_user);
         
    //$bookmarks  =  get_user_meta( $userid, 'bookmarks', TRUE );
   // print_r($bookmarks);
   
  $neighborhood_id = get_user_meta($current_user->ID, 'bookmarks', TRUE);
     // print_r($neighborhood_id);
          $post_id1[]=$post_id;    

if (in_array($post_id, $neighborhood_id))
  {
      $keys = array_search($post_id, $neighborhood_id);
       unset($neighborhood_id[$keys]);
       
        update_user_meta($current_user->ID, 'bookmarks', $neighborhood_id );
         echo "Whishlist Removed Successfully"; 
  }
else
  {
      
      
      if (!empty($neighborhood_id) && $neighborhood_id > 0){
    $bookmarks2=array_merge($post_id1,$neighborhood_id);
   }else{
        $bookmarks2 = "";
   }
   
   
   if( $bookmarks2 != "" ) {
        
       update_user_meta($current_user->ID, 'bookmarks', $bookmarks2 );
         echo "Whishlist Added Successfully";
    } else {
        add_user_meta(  $current_user->ID, 'bookmarks', $post_id1 );
         echo "Whishlist Added Successfully";
    }
  }
     
    
}
 
 
add_action( 'init', 'add_user_bookmarks' );

Mobile App Rest API for Wishlist Add/Remove

If you are planning to develop Rest-Api for mobile app add the following in your function.php file 

Access the URL through Postman
https://yourdomain.com/admin-ajax.php?action=add_user_bookmarks&key=190&post_id=24815&userid=1

function add_user_bookmarks() {
    
       $userid=$_REQUEST['userid'];
        $post_id=$_REQUEST['post_id'];
        $key=$_REQUEST['key'];
        $current_user = get_user_by('ID', $userid);
    
         
   
   
  $neighborhood_id = get_user_meta($current_user->ID, 'bookmarks', TRUE);
     
          $post_id1[]=$post_id;    

if (in_array($post_id, $neighborhood_id))
  {
      $keys = array_search($post_id, $neighborhood_id);
       unset($neighborhood_id[$keys]);
       
        update_user_meta($current_user->ID, 'bookmarks', $neighborhood_id );
         $values=array(
                                              "ID"=>$userid,
                                              "status"=>true,
                                              "status_code"=>200,
                                              "message"=>'Whishlist Removed Successfully'
                                              );
  }
else
  {
      
      
      if (!empty($neighborhood_id) && $neighborhood_id > 0){
    $bookmarks2=array_merge($post_id1,$neighborhood_id);
   }else{
        $bookmarks2 = "";
   }
   
   
   if( $bookmarks2 != "" ) {
        
       update_user_meta($current_user->ID, 'bookmarks', $bookmarks2 );
         $values=array(
                                              "ID"=>$userid,
                                              "status"=>true,
                                              "status_code"=>200,
                                              "message"=>'Whishlist Added Successfully'
                                              );
    } else {
        add_user_meta(  $current_user->ID, 'bookmarks', $post_id1 );
         $values=array(
                                              "ID"=>$userid,
                                              "status"=>true,
                                              "status_code"=>200,
                                              "message"=>'Whishlist Added Successfully'
                                              );
    }
  }
   
     if($key==190){
        
        $json=json_encode($values,JSON_PRETTY_PRINT);
        echo $json;
        
        http_response_code(200);
         die();
     }
    
}
 
 
add_action( 'init', 'add_user_bookmarks' );


If this article is helpful to share it with your friends. 


 

Tags

  • WordPress Rest API Samples

  • Custom Code for Wishlist

  • WordPress Mobile Wishlist API Samples

  • Wishlist Add/Remove Sample code
  • API Custome code in Wordpress
  • WordPress wishlist custom code

Related Posts

How a Matrimonial Mobile App Can Simplify Your Search for a Life Partner

How a Matrimonial Mobile App Can Simplify Your Search for a Life Partner

Finding a life companion has changed in the current digital era, moving beyond conventional matchmaking. The advent of matrimonial smartphone apps has revolutionized the process of finding a compatibl

Read More
How To Do 301 Redirect to Website Domain Name

How To Do 301 Redirect to Website Domain Name

It’s a standard question that pops up. You own several domains and host duplicate copies of an equivalent site (or simply point all of them to at least one site). Your business strategy changes

Read More
Efficient Lead Generation for Schools: Best Software Solutions for 2024

Efficient Lead Generation for Schools: Best Software Solutions for 2024

Schools are constantly looking for efficient methods to draw in potential students in an increasingly competitive educational environment. Reaching a larger audience is only one aspect of effective le

Read More