You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中PHP整合JS广告代码报错,求PHP端解决方案

问题解决:WordPress短码整合JS广告代码报错修复

你的代码存在两个核心问题,直接导致了报错和功能失效:

1. PHP字符串引号冲突

你用单引号'包裹返回的JS代码,但JS对象里的'key'这类单引号会提前闭合PHP的字符串,触发语法解析错误。解决思路是对JS中的单引号进行转义,或者换用更友好的PHP语法避免转义操作。

2. 短码回调函数名不匹配

你定义的函数是jsad_code_shortcode,但注册短码时写的是ad_code_shortcode,两者名称不一致会导致短码无法被正确调用。

修正后的代码(转义引号版)

function jsad_code_shortcode() {
    return '<script type="text/javascript">
                atOptions = {
                    \'key\' : \'9f8c74bccbdb424a067d31a8a20551a3\',
                    \'format\' : \'iframe\',
                    \'height\' : 90,
                    \'width\' : 728,
                    \'params\' : {}
                };
                document.write(\'<scr\' + \'ipt type="text/javascript" src="http\' + (location.protocol === \'https:\' ? \'s\' : \'\') + \'://versatileadvancement.com/9f8c74bccbdb424a067d31a8a20221c6/invoke.js"></scr\' + \'ipt>\');
            </script>';
}
add_shortcode( 'jsad_code', 'jsad_code_shortcode' );

更易读的写法(HEREDOC语法)

如果觉得转义引号麻烦,可以用PHP的HEREDOC语法直接输出代码,无需转义:

function jsad_code_shortcode() {
    ob_start();
    ?>
    <script type="text/javascript">
        atOptions = {
            'key' : '9f8c74bccbdb424a067d31a8a20551a3',
            'format' : 'iframe',
            'height' : 90,
            'width' : 728,
            'params' : {}
        };
        document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://versatileadvancement.com/9f8c74bccbdb424a067d31a8a20221c6/invoke.js"></scr' + 'ipt>');
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode( 'jsad_code', 'jsad_code_shortcode' );

内容的提问来源于stack exchange,提问作者ZeusDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 18:11:09