0

I am attempting to install unslider jquery plugin for my WordPress website. I enter the below code inside my WordPress text editor and I don't get the expected result. I changed $ to jQuery and that didn't fix it either.

<!-- The barebones HTML required for Unslider -->
<div class="banner">
    <ul><li>This is my slider.</li><li>Pretty cool, huh?</li></ul>
</div>

<!-- And the relevant JavaScript -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="/path/to/unslider.js"></script> <!-- but with the right path! -
->
<script>$(function() { $('.banner').unslider() })</script>

Output:

This is my slider. Pretty cool, huh?

$(function() { $(‘.banner’).unslider() })

  • You have a broken end of comment -> with a \n in between - and the ->, move it up to the end of <-- but with the right path! -->. – hcheung Apr 17 '17 at 0:09
  • We don't know if it's pretty cool, it's not working – user3135691 Apr 17 '17 at 0:15
  • 1
    You can't and shouldn't put arbitrary HTMLin the post/page editor, it gets stripped out by the security filters. There are ways of removing those, but it will make your site much easier to hack. If you need to put scripts and embed codes in the middle of content then use a shortcode – Tom J Nowell Apr 17 '17 at 1:09
  • @TomJNowell Where can I put this code? functions.php? – Alt-Rock Ninja Apr 17 '17 at 2:01
1

You can use this PHP code to output your slider's initiator in your themes footer:

add_action('wp_footer','initiate_unslider');
function initiate_unslider () {
    ?>
    <script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="/path/to/unslider.js"></script>
    <script>
        $(document).ready(function() { 
            $('.banner').unslider();
        });
    </script>
    <?php
}

Also it's safer to add a $(document).ready before initiating your slider, in case some necessary components of your page has not been loaded yet.

| improve this answer | |
  • This breaks my site when added to footer.php or functions.php. Where did you want me to add this? – Alt-Rock Ninja Apr 17 '17 at 4:10
  • In your functions.php file. Did you open and close the php tags properly? I added the opening and closing tags to the code. BTW, what kind of break? Fatal php error? – Jack Johansson Apr 17 '17 at 5:34
  • I didn't open and close the php tags properly. I got a Server 500 error – Alt-Rock Ninja Apr 17 '17 at 5:40
  • Sorry my bad. I forgot to use the word function. It should work now. – Jack Johansson Apr 17 '17 at 5:40
  • I got another serve 500 error. Trying your updated code! – Alt-Rock Ninja Apr 17 '17 at 5:43

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.