logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
How to prevent plugins from adding html code to javascript?

I am the developer of the mapping plugin www.mapsmarker.com and having troubles with other plugins which change the output of my javascript causing my maps to break. See this thread for details on this example:

SEO Friendly Images-Plugin for example adds

title="image test" alt=" image test"


instead of

title=\"image test\" alt=\" image test\"


to my javascript code which causes my map to break.

Is there a way to prevent other plugins from adding custom html-code to my javascript output?

This question has been answered.

Robert Harm | 09/19/12 at 10:18am Edit


(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    09/19/12
    10:29am
    John Cotton says:

    These are the culprit lines from the SEO plugin:


    function seo_friendly_images($content) {
    return preg_replace_callback('/<img[^>]+/', 'seo_friendly_images_process', $content);
    }
    add_filter('the_content', 'seo_friendly_images', 100);



    If I were you, I'd add some code that removed his filter, and then added my own back in with a more targetted regex - that's the problem: his currect regex picks up all image tags - you need one that excludes content inside <script> tags (or something like that).

    • 09/19/12 10:51am

      Robert Harm says:

      thanks for the fast answer - forwarded it to the plugin developer of seo friendly images. I wonder if there e.g. a wordpress function or best practice exists which you can use to prevent other plugins from adding code to your plugins code at all.

    • 09/19/12 12:44pm

      John Cotton says:

      I wonder if there e.g. a wordpress function or best practice exists which you can use to prevent other plugins from adding code to your plugins code at all.


      There is plenty of best practice, but sadly many plugins don't play ball. In this case, I suspect it's a genuine oversight on the implications of altering js within the content.

      He uses a very low priority (100) which means he'll tend to come in after all other plugins and shortcodes have been executed. I'm assuming your plugin works with a shortcode?

    • 09/19/12 4:30pm

      Robert Harm says:

      I would like to suggest an enhanced regex which excludes <img>-tags if they are between <script*>-tags. That would be the only solution that would last I guess. Any idea how this regex has to look like (I am still not good at this :-/)

  • avatar
    Last edited:
    09/19/12
    1:39pm
    Gabriel Reguly says:

    Hi Robert,

    The answer got double posted, so I removed this one.

    Regards,
    Gabriel

    Previous versions of this answer: 09/19/12 at 1:39pm

    • 09/19/12 1:38pm

      Gabriel Reguly says:

      Hi Robert,

      Why don't you get rid of your \"?

      Instead of

      var mapquest_osm = new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", {maxZoom: 18, minZoom: 1, errorTileUrl: "http://current.mapsmarker.com/wp-content/plugins/leaflet-maps-marker/inc/img/error-tile-image.png", attribution: "Map: Tiles Courtesy of MapQuest <img src=\"http://current.mapsmarker.com/wp-content/plugins/leaflet-maps-marker/inc/img/logo-mapquest.png\" style=\"display:inline;\" /> (OpenStreetMap, CC-BY-SA)", subdomains: ["otile1","otile2","otile3","otile4"], detectRetina: true});


      do this


      var mapquest_osm = new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", {maxZoom: 18, minZoom: 1, errorTileUrl: "http://current.mapsmarker.com/wp-content/plugins/leaflet-maps-marker/inc/img/error-tile-image.png", attribution: 'Map: Tiles Courtesy of MapQuest <img src="http://current.mapsmarker.com/wp-content/plugins/leaflet-maps-marker/inc/img/logo-mapquest.png" style="display:inline;" /> (OpenStreetMap, CC-BY-SA)', subdomains: ["otile1","otile2","otile3","otile4"], detectRetina: true});


      Regards,
      Gabriel

    • 09/19/12 3:07pm

      Robert Harm says:

      Hi Gabriel,
      unfortunately this is not possible as the php-code for generating this js-code is already using single quotes:

      $lmm_out .= 'var mapquest_osm = new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", {maxZoom: 18, minZoom: 1, errorTileUrl: "' . LEAFLET_PLUGIN_URL . 'inc/img/error-tile-image.png", attribution: "' . $attrib_mapquest_osm . '", subdomains: ["otile1","otile2","otile3","otile4"], detectRetina: ' . $lmm_options['map_retina_detection'] . '});'.PHP_EOL;


      chaning this which would also result in breaking the code :-/

    • 09/19/12 3:41pm

      Gabriel Reguly says:

      Hi Robert,

      Escaping ( adding a back-slash ) works also for 's.

      So this


      $lmm_out .= 'var mapquest_osm = new L.TileLayer("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", {maxZoom: 18, minZoom: 1, errorTileUrl: \'' . LEAFLET_PLUGIN_URL . 'inc/img/error-tile-image.png\', attribution: "' . $attrib_mapquest_osm . '", subdomains: ["otile1","otile2","otile3","otile4"], detectRetina: ' . $lmm_options['map_retina_detection'] . '});'.PHP_EOL;


      should work fine.

      Regards,
      Gabriel

    • 09/19/12 3:52pm

      Gabriel Reguly says:

      Hi Robert,

      I changed one of your files, please test if it works.

      https://dl.dropbox.com/u/355512/leaflet-maps-marker.php

      Regards,
      Gabriel

    • 09/19/12 4:30pm

      Robert Harm says:

      doesnt work unfortunately.

      I would like to suggest an enhanced regex which excludes <img>-tags if they are between <script*>-tags. That would be the only solution that would last I guess. Any idea how this regex has to look like (I am still not good at this :-/)

This question has expired.



Arnav Joy, Robert Harm, Dbranes voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.