Forums

Trying To Do This - Please Help (2 posts)

  1. Seans0n
    Member
    Posted 3 years ago #

    I've written a few plugins so I know my way around WordPress, but I'm trying to do something just now and have a lot of trouble with it.

    I want to include some javascript code on every page that includes a shortcode. I'm currently attempting it using this code:

    <?php
    
    add_shortcode('theshortcodehere', 'wbbb_shortcodes');
    
    function wbbb_shortcodes($atts){
    	return 'Hello There.';
    	add_action('wp_head','wbbb_echohead');
    }
    
    function wbbb_echohead(){
    	echo '<script type="text/javascript"><!-- Lots of JavaScript here--></script>';
    }
    
    ?>

    So what should happen is the shortcode should activate the wbbb_shortcodes() function which in turn adds the javascript wp_head action. Unfortunately it doesn't trigger. The shortcode works and returns Hello There. but the javascript isn't included in the head section (I am viewing the generated HTML source).

    What am I doing wrong, or is there an easier way to accomplish this?

    Cheers

  2. Seans0n
    Member
    Posted 3 years ago #

    I tried echoing out the javascript in place of the shortcode, but unfortunately it needs to go between the head tags.

    Anyone have any ideas on how I could do this? I.e. place some javascript between the head tags of any page or post which has [theshortcodehere] on it?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.