0

How to insert this function inside <body>

<?php function Ciusan_NBar(){ global $options; $options = get_option('Ciusan_NBar');
    if($options['CNB_Showing']=='yes'){ ?>
    <div class="Ciusan_NBar" style=" <?php if($options[CNB_FixedBar]=='yes'){echo 'position: fixed;';}elseif($options[CNB_FixedBar] == 'no'){echo 'position: absolute;'; } ?>!important;">
        <input id="hide" type="radio" name="bar" value="hide">
        <input id="show" type="radio" name="bar" value="show" checked="checked">

        <label for="hide">hide</label>
        <label for="show">show</label>

        <div class="Ciusan_NText"><?php echo $options['CNB_Content']; ?></div>
    </div>
<?php }} ?>

EDITED:

I'm not want insert on wp_head or wp_footer but i want this function auto insert after <body> tag.

<html>
<head></head>    
<body>
    <?php echo Ciusan_NBar(); ?>
    blah...
    blah...
    blah...
    blah...
</body>
</html>
  • Where inside the body? Just before the closing tag, just after the opening tag or anywhere? – Saurabh Shukla Mar 1 '15 at 8:38
  • 1
    Just add that code in your functions.php and then add Ciusan_NBar() in your template files where needed. You can also hook your function to a specific filter if you need to add the code to something specific like before the loop you would use loop_start hook. If you are going to hook your function, then you should return your content, not echo it – Pieter Goosen Mar 1 '15 at 8:59
1

The answer is "it depends" as usual :)

You can do it with hooks and I would recommand action-hook in this case or you can do it on your own.

In both cases : define your function in a plugin, functions.php could be fine but be sure your function is not critical and can be dropped when you change theme.

If you do not want to use action-hooks be sure to test if function exists before using it in a template :

if( function_exists('Ciusan_NBar') ){
    Ciusan_NBar();
}

Regarding the function declaration I would recommand you some coding standards, for example :

if( 'yes' === $options['CNB_Showing'] )

It's better to put variable to the right in case you omit an equal sign.

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.