简体   繁体   中英

How can I run WordPress-plugin “Data Tables” on a WordPress-site and start the script from footer.php?

So I don't study anything that has anything to do with code but we had a small web production course and I got a residue task from that time. Basically we learned the basics of setting up a homepage (html, css, FTP-servers etc) and then installed WordPress on that site and made a child theme. So far so good.

But in the task I have left we're supposed to install a javascript-plugin called Data Tables ( http://datatables.net ) on that site. I'm sure this is very easy but like I said I know very little about code and imo there is information missing in the task description. My teacher is very unhelpful when I ask for advice.

I got the plugin to work locally on a simple html-page, no problem, but I don't understand how to go about it to get it to work on the WordPress-site. In the task description we're asked to add this code to the site's functions.php:

wp_register_script( 'ScriptName_1', 'https://cdn[...]', null, null, true ); 
wp_enqueue_script(' ScriptName_1');



 wp_register_style( 'StyleName_1', 
'https://cdn[...]' ); 
wp_enqueue_style('StyleName_1'); 

I understand CDN-links and had no problem linking it in locally but then I never had to do any PHP. When I just paste this into functions.php(but with the right links of course) it doesn't seem to work. This is the first thing I'd really appreciate some help with. How should the code go into functions.php?

Then the task says to put the code that starts the script in the footer.php and the code looks like this:

(function($) {
$(document).ready(function(){ $('#correct_id').codeThatStartsPlugin ;
}); }(jQuery));

I understand this code, the #correct_id is to be replace with an ID for tables that we want to be affected by the Data Tables plugin and the codeThatStartsPlugin is "dataTable" according to their guide.

But my site's child theme doesn't have a footer.php and even if I create one I don't understand in which format I should put it there. How do I write JS in PHP, or rather just how do I start this plugin from footer.php? I tried to copy the footer.php from the mother theme and just add it within a -tag but that didn't seem to do it either.

So like I said, this is probably very easy for someone who knows code but I don't and my teacher is refusing to give me any advice. I get that coders don't like being asked to solve other people's problems all the time, but I am humbly asking for help and hoping this is so easy that some coder out there will have mercy on me.

In your ChilTheme there should be php file with the name functions.php where you place your following code.

  1. You need to change the CDN Links with the right ones you need to link to. They are now empty(placeholder)

      wp_register_script( 'ScriptName_1', 'https://cdn[...]', null, null, true); wp_enqueue_script(' ScriptName_1'); 

    This functions tells us that we have to put the corrosponding JavaScript function in the footer, the lates argument in the functions is set to true

      wp_register_style( 'StyleName_1', 'https://cdn[...]' ); wp_enqueue_style('StyleName_1'); 

    create a footer file with the following name footer.php under your child theme folder and then put the code in the footer.php file you created.

    (function($) { $(document).ready(function(){ $('#correct_id').codeThatStartsPlugin ; }); }(jQuery));

As you said you need to put the correct id selecter here ('#correct_id')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM