Advertisement
vapvarun

Dynamic ACF Field Integration for "Extra Fields" Group in Business Profiles

Sep 3rd, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | Software | 0 0
  1. // Define a constant for the ACF group key
  2. define('WBCOM_ACF_BUSINESS_INFO_GROUP_KEY', 'group_66d6cf54cb672');
  3.  
  4. /**
  5.  * Get ACF Business Information group fields
  6.  *
  7.  * @return array|false
  8.  */
  9. function wbcom_get_business_info_group_fields() {
  10.     $fields = acf_get_fields( WBCOM_ACF_BUSINESS_INFO_GROUP_KEY );
  11.     return is_array( $fields ) ? $fields : false;
  12. }
  13.  
  14. /**
  15.  * Display ACF Business Information fields in the contact tab
  16.  *
  17.  * @return void
  18.  */
  19. function wbcom_display_business_info_fields_in_contact() {
  20.     acf_form_head();   
  21.     $fields = wbcom_get_business_info_group_fields();
  22.    
  23.     if ( !empty( $fields ) ) {
  24.         acf_render_fields( $fields, get_the_ID() );
  25.     } else {
  26.         // Handle the case where no fields are found or an error occurred
  27.         echo '<p>No fields available to display.</p>';
  28.     }
  29. }
  30. add_action( 'business_profile_after_contact_info', 'wbcom_display_business_info_fields_in_contact' );
  31.  
  32. /**
  33.  * Save ACF Business Information fields
  34.  *
  35.  * @param int $post_id
  36.  * @return void
  37.  */
  38. function wbcom_save_business_info_fields( $post_id ) {
  39.     if ( isset( $_POST['acf'] ) && ! empty( $_POST['acf'] ) ) {
  40.         foreach ( $_POST['acf'] as $field_key => $field_value ) {
  41.             $field = get_field_object($field_key);
  42.            
  43.             if( $field ) {
  44.                 switch( $field['type'] ) {
  45.                     case 'text':
  46.                     case 'textarea':
  47.                     case 'email':
  48.                     case 'url':
  49.                         update_field( $field_key, sanitize_text_field( $field_value ), $post_id );
  50.                         break;
  51.                     case 'number':
  52.                         update_field( $field_key, floatval( $field_value ), $post_id );
  53.                         break;
  54.                     default:
  55.                         update_field( $field_key, $field_value, $post_id );
  56.                         break;
  57.                 }
  58.             }
  59.         }
  60.     }
  61. }
  62. add_action( 'save_post', 'wbcom_save_business_info_fields', 99, 1 );
  63.  
  64. /**
  65.  * Show ACF Business Information fields in the about section
  66.  *
  67.  * @return void
  68.  */
  69. function wbcom_show_business_info_fields_in_about_section() {
  70.     $fields = wbcom_get_business_info_group_fields();
  71.  
  72.     if ( !empty( $fields ) ) {
  73.         foreach ( $fields as $field ) {
  74.             $value = get_field( $field['name'], get_the_ID() );
  75.             if ( !empty( $value ) ) {
  76.                 echo '<h2>' . esc_html( $field['label'] ) . '</h2>';
  77.                
  78.                 switch( $field['type'] ) {
  79.                     case 'url':
  80.                         echo '<p><a href="' . esc_url( $value ) . '" target="_blank">' . esc_html( $value ) . '</a></p>';
  81.                         break;
  82.                     default:
  83.                         echo '<p>' . esc_html( $value ) . '</p>';
  84.                         break;
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90. add_action( 'bp_business_profile_after_render_contact_info', 'wbcom_show_business_info_fields_in_about_section' );
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement