0

I'm using this code to create shortcode. when I Place shortcode in page, that appear at the top of content.

<?php function SkypeChat(){ ?>
 <script type="text/javascript" src="http://www.skypeassets.com/i/scom/js/skype-uri.js"></script>
<div id="SkypeButton_Call_merchando.group_1">
<script type="text/javascript">
Skype.ui({
"name": "chat",
"element": "SkypeButton_Call_merchando.group_1",
"participants": ["merchando.group"],
"imageSize": 32
});
</script>
</div>
<?php } add_shortcode( 'SkypeButton', 'SkypeChat' ); ?>

I already tried return method but still same output.

0

1 Answer 1

1

Your function prints the content to the screen instead of returning it. So when the shortcode is called, which happens while the page content is being built in a filterable variable, your function gets called and prints the content before the rest of the page content is rendered.

If you modify your function like this, you should have better results:

<?php function SkypeChat() {
    return <<<HTML
<script type="text/javascript" src="http://www.skypeassets.com/i/scom/js/skype-uri.js"></script>
<div id="SkypeButton_Call_merchando.group_1">
<script type="text/javascript">
  Skype.ui({
    "name": "chat",
    "element": "SkypeButton_Call_merchando.group_1",
    "participants": ["merchando.group"],
    "imageSize": 32
  });
</script>
</div>
HTML;
}
add_shortcode( 'SkypeButton', 'SkypeChat' ); ?>

An even better approach would be to just include the div using your shortcode, and enqueue the JavaScript using the wp_enqueue_scripts action hook.

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