I try to optimize my internal code structure for my mapping plugin mapsmarker.com by moving large functions to separate files and readding them with include(...). Unfortunately, this doesnt work with the largest function lmm_showmap($atts), which starts with this:
function lmm_showmap($atts) {
global $wpdb;
$lmm_options = get_option( 'leafletmapsmarker_options' );
$uid = substr(md5(''.rand()), 0, 8);
extract(shortcode_atts(array(
'lat' => '', 'lon' => '',
'mlat' => '', 'mlon' => '',
'basemap' => $lmm_options[ 'defaults_marker_shortcode_basemap'],
'mpopuptext' => '',
'micon' => '',
'zoom' => intval($lmm_options[ 'defaults_marker_shortcode_zoom' ]),
'openpopup' => '',
'geojson' => '',
'geojsonurl' => '',
'layer' => '',
'marker' => '',
'markername' => '',
'panel' => '0',
'mapwidth' => intval($lmm_options[ 'defaults_marker_shortcode_mapwidth' ]),
'mapwidthunit' => $lmm_options[ 'defaults_marker_shortcode_mapwidthunit' ],
'mapheight' => intval($lmm_options[ 'defaults_marker_shortcode_mapheight' ]),
'mapname' => 'lmm_map_'.$uid
), $atts));
$pname = 'pa'.$uid;
//info: prepare layers
if (!empty($layer)) {...
Like I have done this with other functions, I created the new file <strong>inc/showmap.php</strong> which contains all code starting from <em>global $wpdb;</em>.
Then I changed the original function to this:
function lmm_showmap($atts) {
include('inc/showmap.php');
}
Unfortunately, this breaks my map in the frontend. Does anyone know why? Could it possibly be, that the $atts in brackets doesnt get propagated to the include file?
Any help is appreciated!
Arnav Joy answers:
if you include
include('inc/showmap.php');
inside your function
lmm_showmap
then how you are extracting the $atts paramerter for shortcode
Robert Seyfriedsberger comments:
this is the question ;-) I also tried the following - same negative result:
function lmm_showmap($atts) {
global $wpdb;
$lmm_options = get_option( 'leafletmapsmarker_options' );
$uid = substr(md5(''.rand()), 0, 8);
extract(shortcode_atts(array(
'lat' => '', 'lon' => '',
'mlat' => '', 'mlon' => '',
'basemap' => $lmm_options[ 'defaults_marker_shortcode_basemap'],
'mpopuptext' => '',
'micon' => '',
'zoom' => intval($lmm_options[ 'defaults_marker_shortcode_zoom' ]),
'openpopup' => '',
'geojson' => '',
'geojsonurl' => '',
'layer' => '',
'marker' => '',
'markername' => '',
'panel' => '0',
'mapwidth' => intval($lmm_options[ 'defaults_marker_shortcode_mapwidth' ]),
'mapwidthunit' => $lmm_options[ 'defaults_marker_shortcode_mapwidthunit' ],
'mapheight' => intval($lmm_options[ 'defaults_marker_shortcode_mapheight' ]),
'mapname' => 'lmm_map_'.$uid
), $atts));
include('inc/showmap.php');
}
any other idea how to solve this?
jazbek answers:
Hi Robert, try placing
global $atts;
at the top of your include file.
Robert Seyfriedsberger comments:
doesnt work unfortunately - any other ideas?
jazbek comments:
Are you sure your include file is being included? Are you receiving any php error messages?
Robert Seyfriedsberger comments:
I dont receive any errors. the function ends with
return $lmm_out;
could this perhaps be the reason for not showing any output?
jazbek comments:
First, I want to mention that this method of organizing your code is a bit strange; if nothing else, typically one would put the entire function in the same include file (i.e. the function lmm_showmap($atts) {
should be at the top of your include file.
Secondly, it would be very useful if you could post *all* of your code, and specify which files have which blocks of code.
Robert Seyfriedsberger comments:
Hi jazbek,
when I move
function lmm_showmap($atts) {
to inc/showmap.php, I get the following error when more than 1 shortcodes are parsed:
<blockquote>Fatal error: Cannot redeclare lmm_showmap3() (previously declared in /home/htdocs/wp-current/wp-content/plugins/leaflet-maps-marker/inc/showmap.php:2) in /home/htdocs/wp-current/wp-content/plugins/leaflet-maps-marker/inc/showmap.php on line 720</blockquote>
If only one shortcode is passed, I get the same (negative) result. I think it is because if I add function lmm_showmap($atts) { to the inc/showmap.php-file, I won´t get executed if I only include it with include('inc/showmap.php'); - right?
Sorry also for not posting all my code - as it is very long I thought it might confuse. Here is a link to the file on github:
[[LINK href="https://github.com/robertharm/Leaflet-Maps-Marker/blob/dev/leaflet-maps-marker.php"]]https://github.com/robertharm/Leaflet-Maps-Marker/blob/dev/leaflet-maps-marker.php[[/LINK]]
the function is called on line 64 and the code for lmm_showmap($atts) is found on lines 287 to 996
I´d really appreciate your help on this again!
best regards,
Robert