• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED How to use JS snippet

rssail

New member
Messages
5
Likes
0
Points
1
#1
I'm trying to create a javascript snippet but it doesn't seem to want to work. I created a snippet that just has:
(function(){
document.write(5+6);
console.log("help");
})();

I have a shortcode of [wbcr_js_snippet id="483"]; which I place in a custom page:


Code:
<?php /* Template Name: swaghome */ ?>

<?php
require( '../../../wp-load.php' );
get_header(); ?>

    <div id="primary" class="site-content">
        [wbcr_js_snippet id="483"];
        <div id="content" role="main">
         
            <?php
            print_r($_POST);
       
         
            if(isset($_POST["place"]) && $_POST["place"] === "foot") $place="foot";
            else if(isset($_POST["place"]) && $_POST["place"] === "involved")$place="involved";
            else $place="home";
         
            echo $place;
         
            ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php
require( '../../../wp-load.php' );
get_footer();
?>
This will work if I set it to automatic insertion but trying to use the shortcode doesn't work
 
Last edited:

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#2
Hello.

Shortcodes can be used only in the text of posts, pages and other WordPress materials. You are trying to use a shortcode in PHP code - it will not work.
You can use:
PHP:
do_shortcode("[wbcr_js_snippet id= '483']");
But using snippets and shortcodes in templates is wrong. You can write code in templates anyway. Why would you?
 

rssail

New member
Messages
5
Likes
0
Points
1
#3
Hello.

Shortcodes can be used only in the text of posts, pages and other WordPress materials. You are trying to use a shortcode in PHP code - it will not work.
You can use:
PHP:
do_shortcode("[wbcr_js_snippet id= '483']");
But using snippets and shortcodes in templates is wrong. You can write code in templates anyway. Why would you?

Thanks, although echo do_shortcode("[wbcr_js_snippet id= '483']"); works fine in a template too. Why is it wrong?