1

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;

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

2
  • Have you tried echoing or print_r the value to see what it's returning? Perhaps it's as simple as a typo in your option name compared to the wp_options entry Oct 18 '13 at 17:05
  • 2
    Change if(get_option( 'MyOptionName' )){ to if ( true ) {. If the code is added then you know that get_option( 'MyOptionName' ) is coming up as false. Oct 18 '13 at 17:06
8

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' );
1
  • This worked perfectly many thanks. the above method also worked, but if wp_print_scripts is a better practice I will use that.
    – robolist
    Oct 18 '13 at 17:36
3

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

1
  • Didn't quite work this way for me, but I did try wp_print_scripts as suggested by Chip Bennett and that worked. But thanks for the help and advice anyway. cheers
    – robolist
    Oct 18 '13 at 17:37
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');
1
  • This worked perfectly, although I was sure I tried this method before, obviously not :) Many thanks.
    – robolist
    Oct 18 '13 at 17:34
0
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.

6
  • Please explain how it answers, just dumping code is not a good answer
    – bravokeyl
    Jul 4 '16 at 11:04
  • it is not dumped type of code i have already used in my plugin.what is your doubt you ask me i will clarify in that code.
    – Praveen
    Jul 4 '16 at 11:08
  • did you try using that code @ bravokey
    – Praveen
    Jul 4 '16 at 11:10
  • 1
    I didn't try it, I was suggesting you to write some explanation in addition to the code posted as how it answers the original question.
    – bravokeyl
    Jul 4 '16 at 11:11
  • Thanks for your sugeestion actually i am new to this @ bravokeyl
    – Praveen
    Jul 4 '16 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.