Add WordPress jQuery to cutom page template

2019-03-02 17:48发布

问题:

I have a plugin and it has a below custom page template. This template requires jQuery. I've a variable $link_to_js liked to google library but I want it to link it to the WordPress internal jQuery library?

 <?php
/*
Template Name: Checkout Template
*/


// Need to replace below with WordPress library???
//$link_to_js = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link href="<?php echo $link_to_css; ?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo $link_to_js; ?>"></script>
</head>

<body>
<div id="authnet_container"><!--CONTAINER START-->

</div><!--FOOTER END-->

</body>
</html>

I've added below code in main plugin file but it does not add?

function my_init() {
    if (!is_admin()) {
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'my_init');

Please advice the best way to add jQuery library to the custom plugin page template? Thanks

回答1:

You should include <?php wp_head(); ?> and <?php wp_footer(); ?> immediately before </head> and </body> respectively.

These functions are what, among other things, include scripts and styles that have been queued.

References:

  • http://codex.wordpress.org/Function_Reference/wp_head
  • http://codex.wordpress.org/Function_Reference/wp_footer

At the very least add <?php do_action("wp_enqueue_scripts"); ?> as that will output all the queued scripts.