Home > Enterprise >  Right integration of complex JavaScript into PHP code
Right integration of complex JavaScript into PHP code

Time:01-29

After hours and hours I can't implement JavaScript code into PHP. JavaScript code is an ad code which I need to implement in WordPress, using functions.php, but every-time I get following error:

identifier "key", expecting ";" in your code

I am trying to create shortcode first and then to use that shortcode anywhere on site, including also injection through PHP, but also directly in content. NOTICE: JavaScript code can not be changed, thus - any customization of JavaScript code is not an option, but I am out of PHP solutions.

My code:

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', 'ad_code_shortcode' );

Any guidance very appreciated, and please no Chat GPT :)

CodePudding user response:

try this code.

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', 'ad_code_shortcode' );

I think, there are some problems with your js code.

CodePudding user response:

Solution is to use HEREDOC syntax, so the shortcode with this kind of JS should look like as follows:

<?php

function shortcode() {
    return <<<EOT
    <script type="text/javascript">
                                    atOptions = {
                                    'key' : '9f8c74bccbdb424a067444665654445',
                                    '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>
EOT;
}
add_shortcode( 'shortcode', 'shortcode' );

Thank you all...

  • Related