add action wp_head not working

I am trying to add some code to the head (namely a block of tracking script) via a plugin I am making.

The plugin has an interface where the user enters some details which are then added to the options table. This is all working perfectly fine so far. But then I want to write a conditional statement if there is an open then add to head. Basically I have this all in the main plugin file and it looks like this;

Read More
if(get_option( 'MyOptionName' )){
    function testingone(){ ?>
        <script>var Script = GoesHere; </script>
    <?php ;}
    add_action('wp_head','testingone');
}

I have tried placing this inside the actual block that pulls the data and then adds to options, but that didn’t work, then I decided to do it this way, where it looks to see if there is an option in the table but this also doesnt inject anything to the head. Some places I have seen people put the add_action above the function, but in the codex it shows an example of it below. but either way I have tried and failed.

Can anyone see where this is going wrong?

Cheers

Related posts

4 comments

  1. Pull the add_action() outside of the function, and put the conditional inside the callback. Also, if you’re printing a script directly, use wp_print_scripts instead of wp_head. You also have a syntax error.

    function testingone(){ 
        if( get_option( 'MyOptionName' ) ) {
            ?>
            <script>var Script = GoesHere; </script>
            <?php
        }
    }
    add_action( 'wp_print_scripts','testingone' );
    
  2. Try turning your code around:

    function testingone() {
        if( FALSE !== get_option( 'MyOptionName' ) ) {
            echo( 'ok, this is in the head!' );
        } 
    }
    add_action( 'wp_head', 'testingone' );
    

    As well, check in your theme’s files (most likely header.php) to make sure that the wp_head() function is being called, and make sure that the MyOptionName option is set for the site.

    Edited to add — You should also use wp_enqueue_script() to add Javascript to your site, rather than just dumping them into the <head>.

    References

  3. see below code.Add IF statement inside the function.

    function testingone(){ 
        if(get_option( 'MyOptionName' )){
        ?>
        <script>var Script = GoesHere; </script>
        <?php } 
          }
    add_action('wp_head','testingone');
    
  4. function plugin_script() {
           wp_enqueue_script( 'Jquery-min', WP_PLUGIN_URL. '/Test Plugin/jquery-min.js', false, '1.11.0' );<br/>
           wp_enqueue_style('pluginstyle', WP_PLUGIN_URL. '/Test Plugin/pluginstyle.css',false,'1.0',"all");<br/>
    }
    add_action( 'wp_head', 'plugin_script' );
    

    The “wp_head” action hook is triggered within the “head” section.

    wp_enqueue_script() is used for executing scripts and wp_enqueue_style is for stylesheet from Plugin.
    “WP_PLUGIN_URL.” will get data from plugin folder of themes.
    “Test plugin” is your plugin Name
    “pluginstyle.css” plugin stylesheet.
    “jquery-min.js” plugin script.

Comments are closed.