0

need some help on thi script

(function($) {
    var oldHide = $.fn.popover.Constructor.prototype.hide;

    $.fn.popover.Constructor.prototype.hide = function() {
        if (this.options.trigger === "hover" && this.tip().is(":hover")) {
            var that = this;
            // try again after what would have been the delay
            setTimeout(function() {
                return that.hide.call(that, arguments);
            }, that.options.delay.hide);
            return;
        }
        oldHide.call(this, arguments);
    };
})(jQuery);

$(document).ready(function () {
    $('#example').popover({
        html: true,
        trigger: 'hover',
        container: '#example',
        placement: 'bottom',
        content: function () {
            return '<div class="box">here is some content</div>';
        },
        animation: false,
        delay: {
            hide: 1500
        }
    }).on({
        show: function () {
            console.log("shown");
        },
        hide: function () {
            console.log("hidden");
        }
    });
});

this is a bootstrap popover and all I want is to add a php script inside

<div class=box><?php php_here(); ?></div>

because I need to output a wordpress script which is

<?php wp_nav_menu(); ?>

is this possible?

4
  • Yes it is possible.?Have you tried it.? Commented Dec 22, 2013 at 8:32
  • no I can't because I don't know how to... :-( Commented Dec 22, 2013 at 8:33
  • How do you include the script via script tag or in the document itself? Commented Dec 22, 2013 at 8:35
  • I just want it to be like these <div class="box"><?php wp_nav_menu(); ?></div> that could output the list Commented Dec 22, 2013 at 8:37

4 Answers 4

1

As long as the file you try to write php in is a .php file or the server handles it like one, you can write php in it.
If this is a .js file, you might have to make it a .php file for the server to handle it correctly. But other than that, it should work as you want it to (there are ways to make the server handle javascript files as php files, but it might not be the best idea and its a bit more work than just renaming the js file).
If you change it to a .php file, you will want to send content-type headers with the type of application/javascript for the client to handle it correct.

Example:

test.php

<?php header('Content-Type: application/javascript'); ?>
alert('This is a .php file with javascript in it!');

index.html

<script src="test.php"></script>
//Should show an alert with "This is a .php file with javascript in it!" when opening index.html 

edit: added example

Sign up to request clarification or add additional context in comments.

3 Comments

Then its likely that your server handles it like a js file and php will not be handled correctly :)
@FrancisAlvinTan Added a small example snippet showing how it works. Feel free to mark answer as correct if it gave you the information you needed!
I just happen to make the changes the way you said it, but now when I add <?php wp_nav_menu(); ?> it shows blanks but when I add <?php echo 'hello'; ?> it appears, is this something to do with wordpress
1

Yes, it is possible:

content: function () {
     return '<div class="box">'<?php wp_nav_menu(); ?>'</div>';
}

Comments

1

I'm not sure if it's the best way to do this.. but you can move your javascript code into your php file (make a <script> tag) like:

<script type="text/javascript">
(function($) {
    var oldHide = $.fn.popover.Constructor.prototype.hide;

    $.fn.popover.Constructor.prototype.hide = function() {
        if (this.options.trigger === "hover" && this.tip().is(":hover")) {
            var that = this;
            // try again after what would have been the delay
            setTimeout(function() {
                return that.hide.call(that, arguments);
            }, that.options.delay.hide);
            return;
        }
        oldHide.call(this, arguments);
    };
})(jQuery);

$(document).ready(function () {
    $('#example').popover({
        html: true,
        trigger: 'hover',
        container: '#example',
        placement: 'bottom',
        content: function () {
            return '<div class="box"><?php wp_nav_menu(); ?></div>';
        },
        animation: false,
        delay: {
            hide: 1500
        }
    }).on({
        show: function () {
            console.log("shown");
        },
        hide: function () {
            console.log("hidden");
        }
    });
});
</script>

since php is server-side and javascript is client-side php will output what you wan't inside your javascript code, hope it helps!

Comments

0

Is this your expected one ?

content: function () {
     var wp_nav_menu ="<?php wp_nav_menu(); ?>";
     return '<div class="box">'+wp_nav_menu+'</div>';
}

3 Comments

it does not output anything, it should show some lists I created in wordpress
but that is what Im trying to achieve
@Francis Alvin Tan, I have updated answer, can you check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.