• Resolved sslv

    (@sslv)


    Hi,

    I’m currently using UsersWP and GeoDirectory and I want to achieve the following:

    • Provide “My Shop Location” as a menu item in the UsersWP>Account page (DONE: https://paste.pics/9d861cfe4438b7bfcbf13ffee5bac379)
    • Limit one location creation per user (DONE: https://paste.pics/708992d4ef3a997151b571f8d7b8a28a )
    • Check with PHP if the currently_logged_in_user has created or not created a location. Here is my PHP code for all of this functionality so far:
    • if ( is_user_logged_in() ) {
          echo 'Welcome, registered user!';
      
      	global $current_user; 
      	get_currentuserinfo();
      	switch (true)  {
      		case ( user_can( $current_user, "barber") ):
      			//blank
      			break;
      		
      			
      		//if user role is barber shop:
      		case ( user_can( $current_user, "barber_shop") ):
      			
      			
      			//create a new meny item
      			add_filter('uwp_account_available_tabs', 'uwp_account_available_tabs_cb', 10, 1);
      			function uwp_account_available_tabs_cb($tabs){
      
      				$tabs['barber-shop-tab'] = array(
      					'title' => __( 'Edit My Barber Shop', 'userswp' ),
      					'icon'  => 'fas fa-store-alt',
      				);
      
      				return $tabs;
      			}
      			
      			//create a header title for the new menu item
      			add_filter('uwp_account_page_title', 'uwp_account_page_title_cb', 10, 2);
      			function uwp_account_page_title_cb($title, $type){
      				if ( $type == 'barber-shop-tab' ) {
      					$title = __( 'Edit My Barber Shop', 'uwp-messaging' );
      				}
      
      				return $title;
      			}
      			
      			//create content of the new menu item
      			add_filter('uwp_account_form_display', 'uwp_account_form_display_cb', 10, 1);
      			function uwp_account_form_display_cb($type){
      				if ( $type == 'barber-shop-tab' ) {
      					
      					//check if user already has created a location:
      					//if yes provide an Edit Location button
      					//if not provide a Create New Location button
      					
      					echo do_shortcode('[gd_add_listing mapzoom="0" label_type="horizontal" show_login="1" mb="3"]');
      				}
      			}
      			break;
      
      		case ( user_can( $current_user, "administrator") ):
      			//blank
      			break;
      	}
      	
      } else {
          echo 'Welcome, visitor!';
      }

      I have noticed that when using echo do_shortcode(‘[gd_add_listing mapzoom=”0″ label_type=”horizontal” show_login=”1″ mb=”3″]’); inside of UsersWP > Account, it brakes the MAP and it’s not loading. To avoid this I want to simply create two buttons inside UsersWP > Account > Edit My Barber Shop.

    • Check if user has a post:
    • – If yes:
    • Get post ID
    • – Provide a link button (“Edit Location”) which will go on separate page to edit Location (Using this approach I would avoid the map breaking)
    • – If not:
    • – Provide a link button (“Create new location for my Barber Shop”) which will go on a separate page to enable users create a new Location (Will redirect to GD/Add-Listing default page.)

    Thanks for your time, if you have any suggestions please let me know how to achieve this.

    • This topic was modified 2 years, 11 months ago by sslv.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    There are many standard WP functions you can use:
    https://developer.www.ads-software.com/reference/functions/count_user_posts/

    Then just use the get_posts() function filtered by author id and post type and status.

    Stiofan

    Thread Starter sslv

    (@sslv)

    Hi,

    I only managed to get the author id, i’m not so sure about the post type and status…

    • I got the id using get_current_user_id();, which is correct i think.
    • But for the post type what do I need to specify? From what I understand from the documentation is the slug used or I’m I wrong?

    • if I use this echo “Post Type: ” . get_post_type(get_the_ID()); it returns Page.
    • As for status is this what I want: https://developer.www.ads-software.com/reference/hooks/get_post_status/
    • Let me know.

      Sorry but I’m not very familiar with php code

    Plugin Author Stiofan

    (@stiofansisland)

    Ah sorry, i thought you were a developer, I might have mixed you up with someone else.

    Try something like:

    
    $user_id = get_current_user_id();
    if($user_id){
    $args = array(
      'numberposts' => 1,
      'post_type'   => 'gd_place',
      'post_status'    => 'publish'
      'author'    => $user_id
    );
     
    $user_places = get_posts( $args );
    print_r($user_places); // debugging
    }

    Thanks,

    Stiofan

    Thread Starter sslv

    (@sslv)

    Thanks for your help, I really appreciate that ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check if current user has created a location or not’ is closed to new replies.