bozdoz / wp-plugin-leaflet-map

Add leaflet maps to Wordpress with shortcodes
https://wordpress.org/plugins/leaflet-map/
GNU General Public License v2.0
140 stars 71 forks source link

Shortcode is not applied #158

Closed sybena closed 2 years ago

sybena commented 2 years ago

I want to output a list of locations as a table and mark them up on a map. For this I have created a content type in WordPress with Pods with a property "Address". With a Pods shortcode, I am able to have the table created and list all the entries. With another Pods shortcode, I am able to generate a Leaflet shortcode.

The problem is that the Leaflet shortcode is output to the frontend, but is not interpreted by Leaflet as a map. If I use the Leaflet shortcode generated by Pods from the frontend in the backend, the map works as desired.

I'm afraid the problem arises because the shortcode generated by a shortcode is not taken into account and implemented.

I have created a WordPress page and noted the following shortcode:

[pods name="locations"]<br /><br />[/pods][pods name="locations"]<br />[leaflet-marker address="{@locations-address}"]<br />[/pods]

A correct shortcode is output in the frontend:

[leaflet-marker address="Ingolstädter Str. 101, 80939 München"]

In consultation with the developer of the Leaflet plugin for WordPress, I tried to insert the function with XYZ PHP code:

<?php
$a = do_shortcode( '[pods name="locations"][leaflet-marker]' . '[' . 'leaflet-marker address={@locations-address}' . '&#093' . '<br>[/pods]');
echo "[leaflet-map]  $a [leaflet-marker]";
?>

The result is better, as the map is generated largely as desired, but when I insert the XYZ PHP code into the WordPress page, no map is output.

sybena commented 2 years ago

I have found a solution in the meantime. The keyword is nested shortcodes (https://codex.wordpress.org/Shortcode_API) and the function "do_shortcode()".

Using a custom WordPress plug-in to run a script and output it as a custom shortcode, I was able to generate the map as desired. For this I used the following code. It is important to note that "do_shortcode()" must be used twice, first for Pods and then for Leaflet.

   function map_func( $atts ){

            $leaflet_map_shortcode = "[leaflet-map lat=48.13391418715657 lng=11.582841421510087 zoom=15 fitbounds max_zoom=19]";
            $leaflet_map = do_shortcode($leaflet_map_shortcode);

            $placese_marks_shortcode = '[pods name="places"]<br />[leaflet-marker address="{@places-address}" [...] [/leaflet-marker]<br />[/pods]';
            $placese_marks = do_shortcode($placese_marks_shortcode);
            $placese_marks2 = do_shortcode($placese_marks);
            return $leaflet_map . $placese_marks2;

        }
        add_shortcode( 'map2', 'map_func' );