-1

I am trying to place 2GIS map into wordpress shortcode. I cannot use HTML widget because I need PHP, I will use PHP to pull latitude and longitude from wordpress post fields. This will be in a single post page. I echo the HTML code because it should be inside function for wordpress shortcode. But then I get error on line 55.

Parse error: syntax error, unexpected 'map' (T_STRING), expecting ';' or ',' on line 55

Please help me understand this.

<?php
function test_func(){
  echo '
    <html>
      <head>
          <title>API карт 2ГИС</title>
          <script src="https://maps.api.2gis.ru/2.0/loader.js?pkg=full"></script>
          <script type="text/javascript">
              var map;

              DG.then(function () {
                  map = DG.map('map', {                // line 55
                      center: [54.98, 82.89],
                      zoom: 13
                  });

                  DG.marker([54.98, 82.89]).addTo(map);
              });
          </script>
      </head>
      <body>
          <div id="map" style="width:800px; height:400px"></div>
      </body>
  </html>
  ';
}

add_shortcode('test_shortcode', 'test_func');
?>
Rogster
  • 39
  • 1
  • 1
  • 5

2 Answers2

2

You have encapsulated your String in single quotes and used single quotes inside the string which confuses the compiler.

So what you need to do is to escape the internal single quotes ( and I am only showing the one little snippet of code here) like...

 map = DG.map('map',

Escaping the single quotes using \

 map = DG.map(\'map\',
TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
1

This might work too, and it's easier especially if you have more quotes:

<?php
function test_func(){
?>
  <html>
      <head>
          <title>API карт 2ГИС</title>
          <script src="https://maps.api.2gis.ru/2.0/loader.js?pkg=full"></script>
          <script type="text/javascript">
              var map;

              DG.then(function () {
                  map = DG.map('map', {                // line 55
                      center: [54.98, 82.89],
                      zoom: 13
                  });

                  DG.marker([54.98, 82.89]).addTo(map);
              });
          </script>
      </head>
      <body>
          <div id="map" style="width:800px; height:400px"></div>
      </body>
  </html>
<?php
}

add_shortcode('test_shortcode', 'test_func');
?>
Daniel
  • 2,318
  • 2
  • 22
  • 53