Hook for Custom Fields

This topic contains 7 replies, has 3 voices, and was last updated by  Stiofan O’Connor 7 years, 9 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #379627

    Markus Boettcher
    Expired Member
    Post count: 8

    Hello,

    I have created some custom fields for hotel classification, price range etc.

    Hotel classification for instance needs to be entered as a number ranging from 1 to 5 in the backend but shown as a number of stars in the front-end.

    What is the right hook to add a filter or action to add some replace functionality in my php-file?

    I have searched the Codex documentation but could not find the right hook.

    Best, Markus

    #379731

    Paolo
    Site Admin
    Post count: 31211

    Hi Markus,

    I asked to the developers if there is a hook for this and how you can use it.

    They’ll let us know asap.

    Thanks

    #379744

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hi Markus,

    The hook name depends on the type of field used for the number and the HTML variable name used to name the custom field. If you provide wp-admin i will make you one example.

    Thanks,

    Stiofan

    #380267

    Markus Boettcher
    Expired Member
    Post count: 8

    Hi Stiofan,

    Thank you for looking into that topic.

    One good example for a custom field is geodir_classification in wp_geodir_gd_place_detail. The values need to be entered as number ranging from 1 to 5 but need to be shown as a number of stars in the front-end e.g. ***** for a 5-star hotel.

    I would like to implement something like:

    add_filter(‘XXX_HOOK’,’ggp_geodir_classification’);
    function ggp_geodir_classification( $classification ){
    if($classification ==’5′)
    $stars = str_replace(‘5′,'<i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i>’,$classification);
    elseif($classification ==’4′)
    $stars = str_replace(‘4′,'<i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i>’,$classification);
    elseif($classification ==’3′)
    $stars = str_replace(‘3′,'<i class=”fa fa-star”></i><i class=”fa fa-star”></i><i class=”fa fa-star”></i>’,$classification);
    elseif($classification ==’2′)
    $stars = str_replace(‘2′,'<i class=”fa fa-star”></i><i class=”fa fa-star”>’,$classification);
    else
    $stars = str_replace(‘1′,'<i class=”fa fa-star”></i>’,$classification);
    return $stars;
    }

    What would be the XXX_HOOK?

    Admin credentials follow as a private reply.

    Best, Markus

    #380268

    Markus Boettcher
    Expired Member
    Post count: 8
    This reply has been marked as private.
    #380302

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Thanks for the info i have added the following snippet to your site under “Code Snippets”

    
    
    add_filter('geodir_custom_field_output_text','ggp_geodir_classification',15,3);
    function ggp_geodir_classification( $html,$location,$cf){
      global $post;
      
      if($cf['name']!='geodir_classification'){return $html;} // if not the field we want then return standard
      
      $classification = $post->{$cf['htmlvar_name']};
        
      if($classification =='5')
      $html = str_replace('5','<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>',$html);
      elseif($classification =='4')
      $html = str_replace('4','<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>',$html);
      elseif($classification =='3')
      $html = str_replace('3','<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>',$html);
      elseif($classification =='2')
      $html = str_replace('2','<i class="fa fa-star"></i><i class="fa fa-star">',$html);
      else
      $html = str_replace('1','<i class="fa fa-star"></i>',$html);
      return $html;
    }

    Thanks,

    Stiofan

    #380473

    Markus Boettcher
    Expired Member
    Post count: 8

    Hi Stiofan,

    thank you a lot! It works very well.

    Best, Markus

    #380576

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    You are welcome 🙂

    Feel free to leave us a review to help us grow https://wordpress.org/support/plugin/geodirectory/reviews/?rate=5#new-post

    Thanks,

    Stiofan

Viewing 8 posts - 1 through 8 (of 8 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount