获取WordPress页面标题并传递到iframe作为URL参数。

huangapple go评论68阅读模式
英文:

Get WordPress page title and pass through to iframe as URL parameter

问题

我们正在通过WordPress页面模板在WordPress站点上的某些页面上嵌入Pardot表单,并且有一些URL参数从主窗口传递到iframe,以便在Pardot中捕获,这是有效的。

但是,我还需要获取WordPress页面标题(不是完整的元标题标记),并将其传递给iframe。我考虑将其附加到URL的末尾作为其他参数,但我不确定如何最好地实现这一点。

这是目前在WP页面模板中的代码:

<script type="text/javascript">
    var form = 'https://go.sample.com/XXX/YYY/';
    var params = window.location.search;
    var thisScript = document.scripts[document.scripts.length - 1];
    var iframe = document.createElement('iframe');

    iframe.setAttribute('src', form + params);
    iframe.setAttribute('width', '100%');
    iframe.setAttribute('height', 700);
    iframe.setAttribute('type', 'text/html');
    iframe.setAttribute('frameborder', 0);
    iframe.setAttribute('allowTransparency', 'true');
    iframe.setAttribute('id', 'formiFrame');
    iframe.style.border = '0';

    thisScript.parentElement.replaceChild(iframe, thisScript);
</script>

我应该如何修改上面的代码以实现这个目标?

这是我找到的用于现有URL参数的工作代码的来源:https://help.salesforce.com/s/articleView?id=000383147&type=1

英文:

We're using Pardot forms embedded on some pages on a WordPress site through a WP page template, and I have some URL parameters that are being passed from the main window into the iframe, so they can be captured in Pardot, which is working.

However I also need to get the WordPress page title (not the full meta title tag) and pass that to the iframe as well. I was thinking to append it to the end of the URL as a parameter like the others, but I'm not sure how best to do that.

This is the code that is currently in the WP page template:

    &lt;script type=&quot;text/javascript&quot;&gt;
        var form = &#39;https://go.sample.com/XXX/YYY/&#39;;
        var params = window.location.search;
        var thisScript = document.scripts[document.scripts.length - 1];
        var iframe = document.createElement(&#39;iframe&#39;);

        iframe.setAttribute(&#39;src&#39;, form + params);
        iframe.setAttribute(&#39;width&#39;, &#39;100%&#39;);
        iframe.setAttribute(&#39;height&#39;, 700);
        iframe.setAttribute(&#39;type&#39;, &#39;text/html&#39;);
        iframe.setAttribute(&#39;frameborder&#39;, 0);
        iframe.setAttribute(&#39;allowTransparency&#39;, &#39;true&#39;);
        iframe.setAttribute(&#39;id&#39;, &#39;formiFrame&#39;);
        iframe.style.border = &#39;0&#39;;

        thisScript.parentElement.replaceChild(iframe, thisScript);
    &lt;/script&gt;

How could I modify the above code to get that result?

This is where I got the code that is working for the existing URL parameters: https://help.salesforce.com/s/articleView?id=000383147&amp;type=1

答案1

得分: 0

所以,我通过添加以下代码成功使其工作:

var postTitle = '<?php echo urlencode(get_the_title()); ?>';
if (params) {
    params += '&post_title=' + postTitle;
} else {
    params = '?post_title=' + postTitle;
}

总结:

<script type="text/javascript">
    var form = 'https://go.sample.com/XXX/YYY/';
    var params = window.location.search;
    var postTitle = '<?php echo urlencode(get_the_title()); ?>';
    if (params) {
        params += '&post_title=' + postTitle;
    } else {
        params = '?post_title=' + postTitle;
    }
    var thisScript = document.scripts[document.scripts.length - 1];
    var iframe = document.createElement('iframe');

    iframe.setAttribute('src', form + params);
    iframe.setAttribute('width', '100%');
    iframe.setAttribute('height', 700);
    iframe.setAttribute('type', 'text/html');
    iframe.setAttribute('frameborder', 0);
    iframe.setAttribute('allowTransparency', 'true');
    iframe.setAttribute('id', 'formiFrame');
    iframe.style.border = '0';

    thisScript.parentElement.replaceChild(iframe, thisScript);
</script>

这个解决方案运行得非常完美。如果已经存在其他参数,它会将帖子标题添加到末尾。如果没有其他参数,它也会以正确的语法添加。

英文:

So I was able to make it work by adding this code:

    var postTitle = &#39;&lt;?php echo urlencode(get_the_title()); ?&gt;&#39;;
    if (params) {
        params += &#39;&amp;post_title=&#39; + postTitle;
    } else {
        params = &#39;?post_title=&#39; + postTitle;
    }

Put altogether:

&lt;script type=&quot;text/javascript&quot;&gt;
    var form = &#39;https://go.sample.com/XXX/YYY/&#39;;
    var params = window.location.search;
    var postTitle = &#39;&lt;?php echo urlencode(get_the_title()); ?&gt;&#39;;
    if (params) {
        params += &#39;&amp;post_title=&#39; + postTitle;
    } else {
        params = &#39;?post_title=&#39; + postTitle;
    }
    var thisScript = document.scripts[document.scripts.length - 1];
    var iframe = document.createElement(&#39;iframe&#39;);

    iframe.setAttribute(&#39;src&#39;, form + params);
    iframe.setAttribute(&#39;width&#39;, &#39;100%&#39;);
    iframe.setAttribute(&#39;height&#39;, 700);
    iframe.setAttribute(&#39;type&#39;, &#39;text/html&#39;);
    iframe.setAttribute(&#39;frameborder&#39;, 0);
    iframe.setAttribute(&#39;allowTransparency&#39;, &#39;true&#39;);
    iframe.setAttribute(&#39;id&#39;, &#39;formiFrame&#39;);
    iframe.style.border = &#39;0&#39;;

    thisScript.parentElement.replaceChild(iframe, thisScript);
&lt;/script&gt;

This solution worked perfectly. If there are other parameters already, then it adds the post title at the end. If there aren't other parameters, it adds it using the correct syntax as well.

huangapple
  • 本文由 发表于 2023年3月9日 14:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定