This is probably a simple question to answer but my Jquery wont work on my theme at all no matter what tutorial ive followed.
my js file is located as /js/jquery.js and the code inside is just for testing purposes:
jQuery(document).ready(function($) {
$('#banner').hide();
});
my functions.php file is located in the same location as the index is and the code ive done for that is:
<?php
function add_my_script() {
wp_enqueue_script(
'jquery', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . 'js/jquery.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
?>
and nothing happens... can anyone correct me in what I am doing wrong? This is what puts me off using wordpress.
Chances are you need to have a slash before "js":
get_template_directory_uri() . '/js/jquery.js'
With that said, you could check your site using the network monitor in your browser's developer tools. It will show you what URLs are being fetched. Or maybe just check your developer tool's console. It could show you a 404 error if it is unable to fetch your jQuery file.
wp_enqueue_script(
'call this something else!', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/jquery.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
you had a couple of typos.....also check if jquery is available by using your dev.console in chrome or firefox(right click on page, click inspect element, and then console) you'll get a error jquery undefined or similiar.
Also on your page in frontend:
get_header();
get_footer();
you dont use the head tags as these functions pull in the header file & footer files. without get_header() & get_footer - you'll have issues :)
Related
Ok, I'm not really sure how to do this so maybe someone can help me out.
I am trying to create a function in my plugin that calls CSS to be used on the front-end of the site. Sounds simple... but there's a catch.
Rather than calling a CSS file, I want to be able to call a PHP file that outputs the CSS styles from the database (plugin saved variable)..
For example... the standard method:
<?php
function register_style(){
wp_register_style( 'front_stylesheet', plugins_url( '/css/custom.css', __FILE__ ) );
wp_enqueue_style( 'front_stylesheet' );
}
add_action( 'enqueue_scripts', 'register_style' );
?>
but I know I can't simply change "/css/rs.css" to the PHP file path. So how would I refer to that PHP file instead?
The PHP file is basically just a simple page that obtains the CSS styles from the database and outputs that content "without the STYLE tags because the tags are already echoed.
Any help would be appreciated.
You can add the file as normal using the register_style method as you said and have the path as "/css/custom.php", and in the top of your custom.php file that generates the css, add:
header("Content-type: text/css");
This has to be the first line of the file with no whitespace before it.
I've created a child theme and am running my site on that so that I can customise a form that I'm including on certain pages using the plugin 'contact-form-7". In my child folder I've placed a style.css, functions.php and js/custom_script.js. the style sheet is being imported fine but I can't seem to get my validation jquery file working on that form. This is the code I've been using:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_register_script( 'custom_script', get_stylesheet_directory_uri() . '/js/custom_script.js' ,array( 'jquery'));
wp_enqueue_script( 'custom_script' );
}
console isn't logging any errors but I can't seem to find it in the web tools side bar UNLESS I remove "js/" when the custom_script is called, in which case a GET error pops up.
I'm guessing I'm incorrectly importing the file and honestly I can't quite get my head around these hooks and importing files just yet- only been using wordpress a short time.
Any help much appreciated.
I believe that you haven't included the JQuery file? You have to download it first https://jquery.com/download/, and then register it as you did with "custom_script", you just have to put it before the "custom_script" in functions.php, inside "custom_script.js" you could also do:
$(document).ready(function(){
});
Go to your console and if you see:
Uncaught ReferenceError: $ is not defined
It's because you haven't included the JQuery file as needed.
Hope this helps!
Leo.
I have this weird problem.
I have a link to js file in my header.php and I want to move them to the functions.php. Problem is that I get this error in the console when I do. (And something don't work)
Uncaught TypeError: Object [object Object] has no method 'sharrre'
So I use this link in the header.php now.
<script src="http://sharrre.com/js/jquery.sharrre-1.3.4.min.js"></script>
And I use this code to move it to the functions.php
// load the theme JS
function theme_scripts() {
wp_enqueue_script( 'sharrre', 'http://sharrre.com/js/jquery.sharrre-1.3.4.min.js' );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
What can the problem be?
There are a few things you can try to get this working.
Be ABSOLUTELY sure your script is being pulled into the page, one way to check is by using the 'sources' tab in the Chrome Debugger and searching for the file.
Be sure that you've included the script after you've included
jQuery, as it is most certainly dependant upon that.
Rename your function name to something more unique. It could be
causing a conflict with another call. Try naming it
sharrre_jquery_script()
I'm working on a Wordpress plugin and a I have to use jquery.
In my plugin file (call it 'my_p.php') I register my .js file (my_js.js) in this way:
add_action('admin_enqueue_scripts', 'my_script');
function my_script() {
wp_register_script( 'my_jquery', plugins_url( '/js/my_js.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
wp_enqueue_script( 'my_jquery' );
}
Code in my_js.js is:
jQuery(document).ready(function($){
jQuery(document.body).on('click','#button_my_p', function(e){
alert('hello!');
});
});
my problem is the following:
if 'button_my_p' is an element (in my case a button) in *my_p.php* file, it works fine and I see the alert when I click on the button. But, if 'button_my_p' is an element of another .php file (for example 'other.php' in my plugin folder), jquery code doesn't work.. I don't see the alert..
What am I missing???
My plugin's structure is:
js folder in which I put my_js.js.
my_p.php file
other.php file
Thanks for your help!
jQuery does not care if you have one file or a thousand. It simply searches through the DOM (which may be created by one .php file or by a thousand included) for a matched element.
My advice for debugging this issue will be:
firstly, to check that my_js.js is present on the page generated by other.php - if it's not - then you use wp_enqueue_script incorrectly
If I have the following code before </head> the php form sends but the css crashes and doesn't display the dropdown menu.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
If I don't have the code, the submit button refreshes the page if blank and never sends anything but the dropdown menu works. If any of the fields are filled, the submit button returns with 404.
How can I keep both?
Additional details:
Wordpress CMS
The JQuery script was inserted in a field for customized js in WP.
I know JS is default, but try putting the type="text/javascript" in the <script> tag. Also, firebug for firefox has a NET tab that will most likely tell you what is going on. I would suggest if you can't figure it out by yourself, to post a screen shot of what is loading.
Wordpress can get finicky with manually embedded js and style embeds. The correct way to inject them is to add_actions to init, print_scripts and print_sytles. Add the following, or something like it to your Functions.php:
// register scripts
if (! function_exists(register_scripts){
function register_scripts() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery','http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
}
}
add_action('init', 'register_scripts');
//print the now registered scripts
if (! function_exists(print_scripts){
function print_scripts() {
wp_enqueue_script( 'jquery' );
}
}
add_action('wp_print_scripts', 'print_scripts');
// do the same for css
if (! function_exists(print_styles){
function print_styles() {
wp_register_style( 'stylesheet', 'path to css'.stylesheet.css ); //bloginfo(url); or something like it to obtain your path
wp_enqueue_style( 'stylesheet' );
}
}
add_action('wp_print_styles', 'print_styles');
Its a little more work, but ensures scripts and styles get inserted correctly, and at the same time wordpress chooses to do so...
I'm not positive if this is causing your issue, but its a great place to start.