admin管理员组文章数量:1277314
From our CRM I can generate URL's for customers that includes parameters. I want to use the information stored in this parameters to show dynamic content to the customers.
Example: www.example/page/?firstname=John&lastname=Doe&age=22
I need to display this information as the following:
Firstname: John
Lastname: Doe
Age: 22
For this I used the following code in my functions.php
<?php
function dynamicParameters($atts){
//Allow to call a parameter within a shortcode
$a = shortcode_atts( array(
'dynamic' => ''), $atts);
//Store the parameter in variable
$content = $atts['dynamic'];
//Create a span and add a piece of javascript that fills the span with the right parameter
$result = '<span id="dynamic' . $content . '">' . $content . '</span><script> const currentURL = window.location.search; const url = new URLSearchParams(currentURL); let ' . $content . ' = url.get("' . $content . '"); document.getElementById("dynamic' . $content . '").innerHTML = ' . $content . ';</script>';
return $result;
}
//Add shortcode so I can use a shortcode like [DynamicContent dynamic="exampleparameter"]
add_shortcode('DynamicContent', 'dynamicParameters');
By this I can just type the following input in my Wysiwyg-editor:
Firstname: [DynamicContent dynamic="firstname"]
Lastname: [DynamicContent dynamic="lastname"]
Age: [DynamicContent dynamic="age"]
The script writes it all as it should be, but it will only work the first time a shortcode is used. In this case the output is
Firstname: John
Lastname: lastname
Age: age
At least it transforms the shortcodes to a certain value, but it seems like the javascript can just used one time, even though the shortcodes generates a new script for every parameter I use in a shortcode.
Does anyone know what I do wrong?
本文标签: phpUnable to display multiple parameters from url by javascript through shortcodes
版权声明:本文标题:php - Unable to display multiple parameters from url by javascript through shortcodes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219484a2360691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论