CodeCabin / wp-google-maps

WP Google Maps
16 stars 12 forks source link

The hook/filter for changing a marker dynamically by taking the value of a ACF field. #1063

Closed JordenHogans closed 7 months ago

JordenHogans commented 7 months ago

I need the right hook/filter for my piece of code. im trying to dynamically set a marker image by using a ACF Custom field value. Is there any way to achieve this?

function customize_marker_based_on_acf_field( $marker_data, $post_id ) {
    $verblijf_type = get_field('type_verblijf', $post_id);

    switch ($verblijf_type) {
        case 'appartement':
            $marker_data['icon'] = 'url_naar_marker_1.png'; 
            break;
        case 'villa':
            $marker_data['icon'] = 'url_naar_marker_2.png'; 
            break;
        // Voeg meer cases toe voor andere verblijftypes
    }

    return $marker_data;
}

add_filter('wp_go_maps_filter_marker', 'customize_marker_based_on_acf_field', 10, 2);
DylanCodeCabin commented 7 months ago

Hi @JordenHogans,

Thanks for getting in touch, we do appreciate your time.

By default, our ACF integration only includes some key fields which the marker system needs to render locations, this means you cannot access additional ACF field values without additional queries. With that said, the example you've created is possible to some extent, which I will share with you a lower down.

Before I get into that, I thought it might be worth mentioning that we do currently allow a custom marker icon to be set using an ACF field. To do this, simply create a new ACF field (Image field) which is setup as seen below: image

Specifically with the following:

This will allow you to set a custom marker icon for each post, which our plugin will respect.


If you'd still prefer more granulate WordPress filter control, I have prepared a snippet for you below:

add_filter('wpgmza_fetch_integrated_markers', 'custom_filter_function_example', 99, 2);

function custom_filter_function_example($markers, $markerFilter){
    /* Returns all markers */
    foreach($markers as $index => $marker){
        if(strpos($marker->id, 'acf_') !== FALSE){
            /* This is an ACF field */
            $metaId = str_replace('acf_', '', $marker->id);
            /* Convert post meta id to post ID */
            $postId = \WPGMZA\Integration\MarkerSource::getPostIDFromMetaID($metaId);

            if(!empty($postId)){
                $type = get_field('type_verblijf', $postId);

                switch ($type) {
                    case 'appartement':
                        $markers[$index]->icon = "full_url_path_example.png";
                        break;
                }

            } 
        }
    }

    return $markers;
}

I hope this helps.

JordenHogans commented 7 months ago

Hi Dylan,

Thanks for the response, very useful. Everything works like it should now. I still preferred to use the ganulate filter control. i'm creating an interface for someone that has barely any digital knowledge, so its key as much elements as possible are automated and don't have to be edited manually.

Thank you so much.

Kind regards.

Jorden

Op di 6 feb 2024 om 07:19 schreef Dylan Auty @.***>:

Hi @JordenHogans https://github.com/JordenHogans,

Thanks for getting in touch, we do appreciate your time.

By default, our ACF integration only includes some key fields which the marker system needs to render locations, this means you cannot access additional ACF field values without additional queries. With that said, the example you've created is possible to some extent, which I will share with you a lower down.

Before I get into that, I thought it might be worth mentioning that we do currently allow a custom marker icon to be set using an ACF field. To do this, simply create a new ACF field (Image field) which is setup as seen below: image.png (view on web) https://github.com/CodeCabin/wp-google-maps/assets/16894783/9ccf40ab-d2c3-4c1e-ab88-4486112402df

Specifically with the following:

  • Field Name: marker_icon
  • Return Format: Image ID

This will allow you to set a custom marker icon for each post, which our plugin will respect.

If you'd still prefer more granulate WordPress filter control, I have prepared a snippet for you below:

add_filter('wpgmza_fetch_integrated_markers', 'custom_filter_function_example', 99, 2);

function custom_filter_functionexample($markers, $markerFilter){ / Returns all markers / foreach($markers as $index => $marker){ if(strpos($marker->id, 'acf') !== FALSE){ / This is an ACF field / $metaId = strreplace('acf', '', $marker->id); / Convert post meta id to post ID / $postId = \WPGMZA\Integration\MarkerSource::getPostIDFromMetaID($metaId);

        if(!empty($postId)){
            $type = get_field('type_verblijf', $postId);

            switch ($type) {
                case 'appartement':
                    $markers[$index]->icon = "full_url_path_example.png";
                    break;
            }

        }
    }
}

return $markers;

}

I hope this helps.

— Reply to this email directly, view it on GitHub https://github.com/CodeCabin/wp-google-maps/issues/1063#issuecomment-1928857404, or unsubscribe https://github.com/notifications/unsubscribe-auth/BEKUZOHM3ZVTLB7E4MAAUZLYSHDO5AVCNFSM6AAAAABC2JDNFCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRYHA2TONBQGQ . You are receiving this because you were mentioned.Message ID: @.***>

--

https://www.hogans-agency.nl/wp-content/uploads/2023/04/hogans-logo-2023.png Kind Regards / Met vriendelijke groet,

Rijnzathe 6, 3454 PV Utrecht

+31 (00) 85 106 35 95

www.hogans.nl

DylanCodeCabin commented 7 months ago

My pleasure @JordenHogans - Glad to hear it helped and all the best with your project! 🙂