サイト内検索
cocoonのバージョンを上げると関数が実行されない | 不具合報告 | Cocoon フォーラム

Topic starter2022年6月6日 23:02
不具合・カスタマイズ対象ページのURL: https://clim.ganbagroup.com/rec/20220604.html
相談内容:投稿ページコメントの本文の前に空白が挿入されます( https://wp-cocoon.com/community/bugs/%e6%8a%95%e7%a8%bf%e3%83%9a%e3%83%bc%e3%82%b8%e3%82%b3%e3%83%a1%e3%83%b3%e3%83%88%e3%81%ae%e6%9c%ac%e6%96%87%e3%81%ae%e5%89%8d%e3%81%ab%e7%a9%ba%e7%99%bd%e3%81%8c%e6%8c%bf%e5%85%a5%e3%81%95%e3%82%8c/#post-62412)の続きです
cocoonのバージョンを上げていくと地図と標高グラフが表示されなくなってしまいます
解決のために試したこと:バージョン2.1.3.1まではOK
2.1.3.2にすると投稿ページのleafletとHighchartsが表示されなくなります
固定ページのleafletは表示されます
バージョン2.1.3.2にして該当ページにてF12を押すと
Uncaught ReferenceError: displayCtMap is not defined
at 20220604.html4
20220604.html:392 Uncaught ReferenceError: displayLogMap is not defined
at 20220604.html5
というエラーが出ます
displayCtMap、displayLogMapというのはjavascript.jsに書いてある関数です
バージョン2.1.3.1までは正常に表示されますのでコードに問題はないと思います
コードを示したいのですが、PHPでデータベースからデータを引っ張ってきていたり、他の色々な関数を呼び出したりで、なかなか大掛かりなコードになってしまいます
上記エラーは呼び出した先のコードにエラーがあるのか、呼び出す関数が呼び出せないのかお分かりになりますか?
よろしくお願いします
環境情報:
----------------------------------------------
サイト名:がんばれ富士登山
サイトURL: https://clim.ganbagroup.com
ホームURL: https://clim.ganbagroup.com
コンテンツURL:/wp-content
インクルードURL:/wp-includes/
テンプレートURL:/wp-content/themes/cocoon-master
スタイルシートURL:/wp-content/themes/cocoon-child-master
子テーマスタイル:/wp-content/themes/cocoon-child-master/style.css
スキン:/wp-content/themes/cocoon-master/skins/skin-fuwari-ebicha/style.css
WordPressバージョン:6.0
PHPバージョン:7.4.28
ブラウザ:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
サーバーソフト:Apache
サーバープロトコル:HTTP/1.1
言語:ja,en-US;q=0.9,en;q=0.8
----------------------------------------------
テーマ名:Cocoon
バージョン:2.1.3.1
カテゴリ数:5
タグ数:281
ユーザー数:1
----------------------------------------------
子テーマ名:Cocoon Child
バージョン:1.0.8
style.cssサイズ:10833バイト
functions.phpサイズ:22701バイト
----------------------------------------------
Gutenberg:0
AMP:0
PWA:0
Font Awesome:4
Auto Post Thumbnail:0
Retina:0
ホームイメージ:/wp-content/themes/cocoon-master/screenshot.jpg
----------------------------------------------
ブラウザキャッシュ有効化:0
HTML縮小化:0
CSS縮小化:0
JavaScript縮小化:0
Lazy Load:0
WEBフォントLazy Load:0
JavaScript(フッター):1
----------------------------------------------
利用中のプラグイン:
All-in-One WP Migration 7.61
Custom Permalinks 2.4.0
Google XML Sitemaps 4.1.3
Media Library Folders for WordPress 7.1.0
PHP Compatibility Checker 1.5.2
Really Simple CSV Importer 1.3
Search Regex 2.4.1
Throws SPAM Away 3.3.2
WP-Optimize - Clean, Compress, Cache 3.2.3
WP Multibyte Patch 2.9
----------------------------------------------
Topic starter2022年6月6日 23:15
一部ですが、コードを張っておきます
上記添付ファイルの地図表示部分のコードの一部です
<投稿ページ>
[insert_pagehead]
[insert_ct4]
[insert_ctmap ctid=58 mid=111"]
<function.php>
function print_pagehead(){
return "
<link rel='stylesheet' href='//unpkg.com/leaflet@1.7.1/dist/leaflet.css' integrity='sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==' crossorigin=''/>
<script src='//unpkg.com/leaflet@1.7.1/dist/leaflet.js' integrity='sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==' crossorigin=''></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/1.5.1/gpx.min.js'></script>
<meta http-equiv='content-type' content='text/html; charset=SHIFT-JIS' />
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link href='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/leaflet.fullscreen.css' rel='stylesheet' />
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'></script>
<script src='//code.highcharts.com/highcharts.js'></script>";
}
add_shortcode('insert_pagehead','print_pagehead');
//コースタイム呼び出し用4
function print_ct4(){
print "
<div id='ct'><h2>コースタイム</h2>
<div id='cttext'></div>
<div id='ctmap' style='width: 100%; height: 400px'></div>
<div id='ctgrf' style='height: 250px; '></div>
<div id='cttbl'></div>
<div id='ctlink'></div>
</div>";
}
add_shortcode('insert_ct4','print_ct4');
//コースタイム表示用
function print_ctmap($atts){
global $wpdb;
$ctid = $atts['ctid'];
$mid = $atts['mid'];
$result = $wpdb->get_results("SELECT * FROM wp_mountaintime WHERE mt_id = '$ctid'");
foreach($result as $row) {
$value = $row->mt_url;
}
$cttext = "<p>" . $row->mt_route . "<BR>" . $row->mt_route2 . "</p>";
$cttime = $row->mt_time;
if (empty($row->mt_url)){}else{
$ctlink = "<p><a href=\"/wp-content/uploads/gpx/" . $row->mt_gpx . ".gpx\" target=\"_blank\">download GPX</a> (右クリックで名前を付けて保存)<BR><a href=\"https://www.yamareco.com/modules/yr_plan/step1_planner.php?planid=" . $row->mt_url . "\" target=\"_blank\">ヤマプラで見る</a></p>";}
$result2 = $wpdb->get_results("SELECT * FROM wp_mountain WHERE m_id = '$mid'");
foreach($result2 as $row2) {
$value2 = $row2->m_name;
}
$mlat = $row2->m_lat;
$mlon = $row2->m_lon;
$ctgpx = $row->mt_gpx;
return"
<script type='text/javascript'>
var cttext = '" . $cttext . "';
var cttime = '" . $cttime . "';
var ctlink = '" . $ctlink . "';
var ctgpx = '" . $ctgpx . "';
var lat = '" . $mlat . "';
var lon = '" . $mlon . "';
console.log(lon);
displayCtMap(cttext, cttime, ctgpx, ctlink, lat, lon);
</script>
";}
add_shortcode('insert_ctmap','print_ctmap');
<javascript.js>
function displayCtMap(cttext, cttime, ctgpx, ctlink, mlat, mlon) {
ctgpx = '/wp-content/uploads/gpx/' + ctgpx + '.gpx';
document.getElementById("cttext").innerHTML = cttext;
document.getElementById("ctlink").innerHTML = ctlink;
ctmap = L.map('ctmap');
ctmap.setView([mlat, mlon], 15);
var cjm = L.tileLayer("https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png", {
minZoom: 2,
maxZoom: 18,
attribution: '© <a href="https://maps.gsi.go.jp/development/ichiran.html" target="_blank">国土地理院</a>'
});
var pale = L.tileLayer("https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png", {
minZoom: 2,
maxZoom: 18,
attribution: '© <a href="https://maps.gsi.go.jp/development/ichiran.html" target="_blank">国土地理院</a>'
});
var air = L.tileLayer("https://cyberjapandata.gsi.go.jp/xyz/seamlessphoto/{z}/{x}/{y}.jpg", {
minZoom: 2,
maxZoom: 18,
attribution: '© <a href="https://maps.gsi.go.jp/development/ichiran.html" target="_blank">国土地理院</a> | データソース: Landsat8画像(GSI,TSIC,GEO Grid/AIST,courtesy of USGS), 海底地形(GEBCO), <a href="https://lpdaac.usgs.gov/data_access" target="_blank">世界衛星モザイク画像</a>(LP DAAC, USGS/EROS Center)'
});
var baseMaps = {"地理院地図(標準)": cjm, "地理院地図(淡色)": pale, "空中写真": air};
L.control.layers(baseMaps).addTo(ctmap);
cjm.addTo(ctmap);
//ファイル名に0が入ってるかで分岐
if (ctgpx.indexOf('0') !== -1) {
//ctgpxのなかに'0'が含まれている場合
var ctanalysisText = "<table border='0'><tr><td class='itemname'>コースタイム</td><td class='itemvalue'>" + cttime + "</td><td class='itemname'>水平距離</td><td class='itemvalue'>-km</td></tr><tr><td class='itemname'>総上昇量</td><td class='itemvalue'>-m</td><td class='itemname'>総下降量</td><td class='itemvalue'>-m</td></tr><tr><td class='itemname'>最高高度</td><td class='itemvalue'>-m</td><td class='itemname'>最低高度</td><td class='itemvalue'>-m</td></tr></table>";
document.getElementById("cttbl").innerHTML = ctanalysisText;
document.getElementById("ctgrf").innerHTML = "標高グラフはありません";
}else{
new L.GPX(ctgpx, {
async: true,
marker_options: {
endIconUrl: '/wp-content/uploads/image/goal.png',
startIconUrl: '/wp-content/uploads/image/start.png',
shadowUrl: false,
iconSize: [40, 40],
iconAnchor: [20, 40]
}
}).on('loaded', function(e) {
ctmap.fitBounds(e.target.getBounds());
}).addTo(ctmap);
ctgetGpxData(ctgpx, cttime);
}
}
2022年6月6日 23:29
以下の親テーマの変更が原因だと思います。
Cocoon 設定 > 高速化にあった「JavaScriptをフッターで読み込む」をお使いであったのなら確実です。
地図や標高グラフが表示されないのは、displayCtMap、displayLogMap をお書きになっている子テーマの javascript.js が読み込まれる前に、displayCtMap や displayLogMap をお使いのためです。
解決方法は 2 つあります。
① 投稿ページの「カスタム JavaScript」にコードを書く。
投稿ページの本文にお書きになっている JavaScript を「カスタム JavaScript」に移します。
※ 「カスタム JavaScript」には、JavaScript のみを書きます。HTML の script タグは不要です。
② 以前の Cocoon にあった「JavaScriptをフッターで読み込む」を復活させる
以下リンク先のコードを子テーマの functions.php に追加します。
https://notepad.pw/share/21z1y36i3
ただし、何かしらの不都合なケースがあったため、この機能は廃止されたはずです。それを復活させる場合は、相応のリスクがあるとお考えください。私であれば、① の方法で解決を目指します。
sakusakujzz20 と わいひら 件のいいね!
2022年6月6日 23:39
あとショートコードの中身は見直しが必要です。
print_pagehead() には、head タグの中に追加しないといけないものが書かれています。また、2 つの meta タグは不要に思えます。
sakusakujzz20 と わいひら 件のいいね!
2022年6月7日 12:56
3 つ目の解決方法を思いついたので書いておきます。
ショートコードにある script タグに type="module" を付けます。
例えば、print_ctmap() にある以下の script タグを…
<script type='text/javascript'> var cttext = '" . $cttext . "'; var cttime = '" . $cttime . "'; var ctlink = '" . $ctlink . "'; var ctgpx = '" . $ctgpx . "'; var lat = '" . $mlat . "'; var lon = '" . $mlon . "'; console.log(lon); displayCtMap(cttext, cttime, ctgpx, ctlink, lat, lon); </script>
このように変更します。
<script type='module'> var cttext = '" . $cttext . "'; var cttime = '" . $cttime . "'; var ctlink = '" . $ctlink . "'; var ctgpx = '" . $ctgpx . "'; var lat = '" . $mlat . "'; var lon = '" . $mlon . "'; console.log(lon); displayCtMap(cttext, cttime, ctgpx, ctlink, lat, lon); </script>
※ 注 1: console.log(lon); はご必要でしょうか?
※ 注 2 :子テーマの javascript.js が async や defer の非同期で読み込まれる場合は、問題が再発します。
sakusakujzz20 と わいひら 件のいいね!
Topic starter2022年6月7日 21:23
Akiraさま
色々とありがとうございます
結論から言いますと
<script type='module'>
で解決しました
本当にありがとうございました
あとショートコードの中身は見直しが必要です。
見直しを行います
※ 注 1: console.log(lon); はご必要でしょうか?
色々とコードの検証をしていたものが残ってました
色々とありがとうございました
わいひら 件のいいね!