• I have a site where businesses and customers register via MemberPress. Each have a slightly different registration form. Both customers and businesses must choose only one postcode area that they are interested in. Once the customers register they are taken to the page where they will see the ads for that area.

    For businesses they are taken to the wpadverts dashboard. I’m trying to find a way whereby the location/postcode they chose upon registration is already pre-populated in WP adverts and that when they post an ad that it appears on the page with the pre-defined slug that customers see.

    I know that the memberpress is recording the meta-data in WordPress so there must be a way to make this appear in WP adverts.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    you can pre-populate the adverts_location field in the [adverts_add] form by adding the code below in your theme functions.php file

    
    add_filter( "adverts_form_load", function( $form ) {
      if( $form["name"] != "advert" || is_admin() ) {
        return $form;
      }
      foreach( $form["field"] as $k => $f ) {
        if( $f["name"] == "adverts_location" ) {
          $form["field"][$k]["attr"] = array( "readonly" => "readonly" );
        }
      }
      return $form;
    } );
    add_filter( "adverts_form_bind", function( $form ) {
      $form->set_value( "adverts_location", "xxx" );
      return $form;
    } );
    

    This will do two things actually, make the location field read-only and set the value in this field to “xxx”, you of course need to update the code to use the value you have saved in memberpress.

    This is as close as i need to repopulate the Phone field, But i have a problem.

    add_filter( "adverts_form_bind", function( $form ) {
        $user_id = get_post(get_current_user_ID())->post_author;
    	//echo $user_id;
        $args = array(
        'author'         => $user_id,
        'post_type'      => 'advert-author',
        'post_status'    => array( 'publish', 'advert-hidden' ),
        'posts_per_page' => 1
    );
    $authors = get_posts( $args );
    //var_dump ($authors);	
    $profile_id = $authors[0]->ID;
    //echo $the_user;
    
    $profile_phone = get_post_meta( $profile_id, "user_phone", true ); 
        
      $form->set_value( "adverts_phone", $profile_phone );
      return $form;
    } );

    The phone shown is NOT the user’s profile phone stored in the system. If i am an admin shows the phone of another user. If i’m a regular user it shows the admin’s phone and when logged out throws an error

    Notice: Trying to get property of non-object in /home/racertrader/public_html/tests/wp-content/themes/RTrader/functions.php on line 3028

    Where line 3028 is $user_id = get_post(get_current_user_ID())->post_author;

    What i’m doing wrong?

    Plugin Author Greg Winiarski

    (@gwin)

    This code get_post(get_current_user_ID()) is incorrect the get_post() as a param expects a post id while you are passing currently logged in user id (get_current_user_ID()).

    Anyway you are looking to just get the current user id, so you can change the line

    
    $user_id = get_post(get_current_user_ID())->post_author;
    

    to

    
    $user_id = get_current_user_ID();
    

    Hello Greg.
    Now it works properly on my case.
    Hope for OP is working correctly.

    Thread Starter maritimefox

    (@maritimefox)

    I have applied the code to the functions.php of my theme but I suspect I have made an error as it doesn’t appear to be showing what I expected to see.

    I added a custom field to memberpress with a slug of “mepr_home_postcode”. In this example home postcode would be bb3-darwen

    This is what I entered into the “xxx” in the code you supplied.

    What I expected to see what where the ad is displayed.

    https://mywebsite.com/bb3-darwen/offer/test-wednesday/

    Additionally in the author manager I want it to be location specific

    https://postcodeoffers.co.uk/bb3-darwen/author-manager/

    Am I expecting too much of this piece of code?

    Thanks,
    J

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    i am not sure, can you post the custom code you are using, it will be easier to tell then?

    Thread Starter maritimefox

    (@maritimefox)

    what I want to achieve is for a business or customer to enter their postcode and for that postcode to follow them as they journey through the website. So for a business that registers they can only post ads in that postcode area and for a customer that registers they can only see the ads/offers in that postcode area.

    Maybe I’m trying to achieve something that is not possible with WPAdverts?

    add_filter( “adverts_form_load”, function( $form ) {
    if( $form[“name”] != “advert” || is_admin() ) {
    return $form;
    }
    foreach( $form[“field”] as $k => $f ) {
    if( $f[“name”] == “adverts_location” ) {
    $form[“field”][$k][“attr”] = array( “readonly” => “readonly” );
    }
    }
    return $form;
    } );
    add_filter( “adverts_form_bind”, function( $form ) {
    $form->set_value( “adverts_location”, “mepr_business_postcode” );
    return $form;
    } );

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    ok so what the code does right now is setting in the location field a static “mepr_business_postcode” text, i understand you want to pull actual value of the postcode saved in the user profile.

    I am not that familiar with MemberPress but as far as i know the data you save in the profile they are storing in the wp_usermeta table, so you should be able to access the currently logged in user business_postcode like this

    
    add_filter( "adverts_form_bind", function( $form ) {
      if( get_current_user_id() < 1 ) {
        return $form;
      }
      $pc = get_user_meta( get_current_user_id(), "mepr_business_postcode", true );
      
      // set value in [adverts_add] location field
      $form->set_value( "adverts_location", $pc );
      // set value in [adverts_list] location field
      $form->set_value( "location", $pc );
    
      return $form;
    } );
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Pre-populating location with page slug’ is closed to new replies.