[ label, type, group ] */ function abddir_get_meta_fields() { return [ // Contact Info 'abddir_address' => [ 'label' => 'Street Address', 'type' => 'text', 'group' => 'contact' ], 'abddir_address2' => [ 'label' => 'Address Line 2', 'type' => 'text', 'group' => 'contact' ], 'abddir_community' => [ 'label' => 'Community / Township (optional)', 'type' => 'text', 'group' => 'contact' ], 'abddir_city' => [ 'label' => 'City', 'type' => 'text', 'group' => 'contact' ], 'abddir_state' => [ 'label' => 'State', 'type' => 'text', 'group' => 'contact' ], 'abddir_zip' => [ 'label' => 'ZIP Code', 'type' => 'text', 'group' => 'contact' ], 'abddir_phone' => [ 'label' => 'Phone', 'type' => 'tel', 'group' => 'contact' ], 'abddir_email' => [ 'label' => 'Email', 'type' => 'email', 'group' => 'contact' ], 'abddir_website' => [ 'label' => 'Website URL', 'type' => 'text', 'group' => 'contact' ], 'abddir_contact_name' => [ 'label' => 'Contact Name (optional)', 'type' => 'text', 'group' => 'contact' ], // Description 'abddir_description' => [ 'label' => 'Business Description', 'type' => 'textarea', 'group' => 'info' ], // Social Media 'abddir_facebook' => [ 'label' => 'Facebook URL', 'type' => 'text', 'group' => 'social' ], 'abddir_instagram' => [ 'label' => 'Instagram URL', 'type' => 'text', 'group' => 'social' ], 'abddir_twitter' => [ 'label' => 'X / Twitter URL', 'type' => 'text', 'group' => 'social' ], 'abddir_linkedin' => [ 'label' => 'LinkedIn URL', 'type' => 'text', 'group' => 'social' ], 'abddir_youtube' => [ 'label' => 'YouTube URL', 'type' => 'text', 'group' => 'social' ], 'abddir_tiktok' => [ 'label' => 'TikTok URL', 'type' => 'text', 'group' => 'social' ], 'abddir_pinterest' => [ 'label' => 'Pinterest URL', 'type' => 'text', 'group' => 'social' ], // Logo 'abddir_logo' => [ 'label' => 'Logo / Photo', 'type' => 'image', 'group' => 'logo' ], // Display order 'abddir_sort_order' => [ 'label' => 'Sort Order', 'type' => 'number', 'group' => 'display' ], ]; } // Register meta boxes function abddir_add_meta_boxes() { // Logo box label adapts to directory type $dir_type = 'business'; $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only, used only to determine directory type for meta box label if ( $post_id ) { $dir_id = intval( get_post_meta( $post_id, 'abddir_directory_id', true ) ); $dir_s = abddir_get_directory_settings( $dir_id ); $dir_type = $dir_s['type'] ?? 'business'; } $logo_box_label = ( $dir_type === 'member' || $dir_type === 'employee' ) ? 'Avatar / Photo' : 'Business Logo'; add_meta_box( 'abddir_logo_box', $logo_box_label, 'abddir_render_meta_box_logo', 'abddir_listing', 'side', 'high', [ 'dir_type' => $dir_type ] ); add_meta_box( 'abddir_contact_info', 'Contact Information', 'abddir_render_meta_box_contact', 'abddir_listing', 'normal', 'high' ); add_meta_box( 'abddir_social_links', 'Social Media Links', 'abddir_render_meta_box_social', 'abddir_listing', 'normal', 'default' ); add_meta_box( 'abddir_display_opts', 'Display Options', 'abddir_render_meta_box_display', 'abddir_listing', 'side', 'default' ); } add_action( 'add_meta_boxes', 'abddir_add_meta_boxes' ); function abddir_render_meta_box_logo( $post, $metabox ) { wp_enqueue_media(); $logo = get_post_meta( $post->ID, 'abddir_logo', true ); $dir_type = $metabox['args']['dir_type'] ?? 'business'; $is_people = ( $dir_type === 'member' || $dir_type === 'employee' ); $btn_label = $is_people ? 'Select Photo' : 'Select Logo'; // Stored value can be an attachment ID or a direct URL $preview_src = ''; if ( $logo && is_numeric( $logo ) ) { $preview_src = wp_get_attachment_image_url( intval( $logo ), 'medium' ) ?: ''; } elseif ( $logo ) { $preview_src = $logo; } ?>

Listings without an image show a colored initials avatar.

ID, 'abddir_directory_id', true ) ); if ( ! $dir_id && isset( $_GET['dir_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only, used only to pre-fill hidden field for new listing flow $dir_id = intval( $_GET['dir_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } if ( $dir_id ) { echo ''; } // Get directory type and address format for this listing $dir_s = abddir_get_directory_settings( $dir_id ); $dir_type = $dir_s['type'] ?? 'business'; $address_format = $dir_s['address_format'] ?? 'us'; $is_intl = ( $address_format === 'international' ); $fields = abddir_get_meta_fields(); $contact_fields = array_filter( $fields, fn($f) => $f['group'] === 'contact' ); $info_fields = array_filter( $fields, fn($f) => $f['group'] === 'info' ); echo ''; foreach ( array_merge( $contact_fields, $info_fields ) as $key => $field ) { // Move Website URL to Social Links for member/community directories if ( $key === 'abddir_website' && $dir_type === 'member' ) continue; // Adjust labels by directory type if ( $dir_type !== 'business' && $key === 'abddir_description' ) { $field['label'] = 'Bio / Description'; } // Adjust address field labels for international mode if ( $is_intl ) { if ( $key === 'abddir_state' ) $field['label'] = 'State / Province / Region'; if ( $key === 'abddir_zip' ) $field['label'] = 'Postal Code'; } $value = get_post_meta( $post->ID, $key, true ); echo ''; // After ZIP field, inject Country row for international directories if ( $key === 'abddir_zip' && $is_intl ) { $country_val = get_post_meta( $post->ID, 'abddir_country', true ); echo ''; } } echo '
'; if ( $field['type'] === 'textarea' ) { echo ''; } else { echo ''; } echo '
'; echo ''; echo '
'; } function abddir_render_meta_box_social( $post ) { $dir_id = intval( get_post_meta( $post->ID, 'abddir_directory_id', true ) ); $dir_s = abddir_get_directory_settings( $dir_id ); $dir_type = $dir_s['type'] ?? 'business'; $fields = abddir_get_meta_fields(); $social_fields = array_filter( $fields, fn($f) => $f['group'] === 'social' ); echo ''; // For member directories, show Website URL at the top of Social Links if ( $dir_type === 'member' ) { $website_val = get_post_meta( $post->ID, 'abddir_website', true ); echo ''; } foreach ( $social_fields as $key => $field ) { $value = get_post_meta( $post->ID, $key, true ); echo ''; } echo '
'; echo ''; echo '
'; echo ''; echo '
'; } function abddir_render_meta_box_display( $post ) { $sort_order = get_post_meta( $post->ID, 'abddir_sort_order', true ); echo '


'; echo '

'; } // Save meta function abddir_save_meta( $post_id ) { if ( ! isset( $_POST['abddir_meta_nonce'] ) ) return; if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['abddir_meta_nonce'] ) ), 'abddir_save_meta' ) ) return; if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; if ( ! current_user_can( 'edit_post', $post_id ) ) return; $fields = abddir_get_meta_fields(); foreach ( $fields as $key => $field ) { if ( $field['type'] === 'image' ) { if ( isset( $_POST[$key] ) ) { // Attachment ID from the media picker, or a direct URL (legacy) $raw = sanitize_text_field( wp_unslash( $_POST[$key] ) ); $value = is_numeric( $raw ) ? absint( $raw ) : esc_url_raw( $raw ); update_post_meta( $post_id, $key, $value ); } } elseif ( isset( $_POST[$key] ) ) { $raw = wp_unslash( $_POST[$key] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized on the following lines via sanitize_text_field/esc_url_raw/sanitize_email/sanitize_textarea_field $value = sanitize_text_field( $raw ); if ( $field['type'] === 'url' ) $value = esc_url_raw( $raw ); if ( $field['type'] === 'email' ) $value = sanitize_email( $raw ); if ( $field['type'] === 'textarea' ) $value = sanitize_textarea_field( $raw ); update_post_meta( $post_id, $key, $value ); } } // Save Country field (international directories) if ( isset( $_POST['abddir_country'] ) ) { update_post_meta( $post_id, 'abddir_country', sanitize_text_field( wp_unslash( $_POST['abddir_country'] ) ) ); } // Save directory assignment — only write if not already set, so we never // accidentally reassign an existing listing via a stale hidden field. if ( isset( $_POST['abddir_directory_id'] ) && intval( $_POST['abddir_directory_id'] ) > 0 ) { $existing = intval( get_post_meta( $post_id, 'abddir_directory_id', true ) ); if ( ! $existing ) { update_post_meta( $post_id, 'abddir_directory_id', intval( $_POST['abddir_directory_id'] ) ); } } } add_action( 'save_post_abddir_listing', 'abddir_save_meta' );