Here's My first post!
I am a bit of a newb to coding, I know enough html and css, mixed with using frameworks such as bootstrap, to develop a basic website and have started learning how to build themes in wordpress using php.
My goal was to build a theme using bootstrap and javascript but I am having trouble with the simplest of tasks such as getting the scripts to work in functions.php. I have been following some tutorials online which just didn't work and after a lot of experimenting and researching I cant seem to get anything to work.
My functions.php code is:
<?php
function styles() {
wp_register_style('bootstrap', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css' );
$dependencies = array('bootstrap');
wp_enqueue_style( 'bootstrapstarter-style', get_stylesheet_uri(), $dependencies );
wp_register_style('style', get_template_directory_uri() . 'style.css');
$dependencies = array('style');
wp_enqueue_style('style', get_stylesheet_uri(), $dependencies);
}
add_action( 'wp_enqueue_scripts', 'styles' );
function scripts() {
wp_deregister_script('jquery3.4.1');
wp_enqueue_script('jquery3.4.1', get_template_directory_uri() . 'jquery-3.4.1.min.js', '', 1, true);
$dependencies = array('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri() .'/bootstrap/js/bootstrap.min.js', $dependencies, '3.3.6', true );
wp_enqueue_script('customjs', get_template_directory_uri() . 'scripts.js', '', 1, true);
}
add_action( 'wp_enqueue_scripts', 'scripts' );
function title(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'title' );
?>
and I have a test piece of code which displays a pop-up alert in the scripts.js (and the footer is calling the ?php wp_footer();? ):
$(document).ready(function(){
alert('test');
})
I have also experimented with the load order thinking that bootstrap js may be interfering with my own js but which ever way I position them I cannot get them to work.
Apologies if this is a dumb one and if this has been asked before but nothing I have looked up seems to work and any help would be greatly appreciated.
Many thanks!
You may have to replace your $ in your javascript with jQuery. jQuery is in compatibility mode by default in WP. That means that the typical $ shortcut for jQuery doesn't work.
jQuery(document).ready(function(){
alert('test');
})
Ok found the issue...
Using console I could see that it couldn't find the path to the files but I couldn't work out why as it was pulling through the styling but not the scripts.
I then went back to my code and realised there was no root on the files I was linking to.
Using atom as my text editor I was right clicking on the project path and copying the project path which didn't pull over the root of the path (I.E only pulling over scripts.js instead of /scripts.js).
Once I put the forward slash in on all linked files everything fixed.
Such a simple fix that a newbie like me will not now forget!
Here's the final working code for anyone who is interested:
<?php
function styles() {
wp_register_style('bootstrap', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css' );
$dependencies = array('bootstrap');
wp_enqueue_style( 'bootstrapstarter-style', get_stylesheet_uri(), $dependencies );
wp_register_style('style', get_template_directory_uri() . '/style.css');
$dependencies = array('style');
wp_enqueue_style('style', get_stylesheet_uri(), $dependencies);
}
add_action( 'wp_enqueue_scripts', 'styles' );
function scripts() {
wp_deregister_script('jquery3.4.1');
wp_enqueue_script('jquery3.4.1', get_template_directory_uri() . '/jquery-3.4.1.min.js', '', 1, true);
$dependencies = array('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri() .'/bootstrap/js/bootstrap.min.js', $dependencies, '3.3.6', true );
wp_enqueue_script('customjs', get_template_directory_uri() . '/scripts.js', '', 1, true);
}
add_action( 'wp_enqueue_scripts', 'scripts' );
function title(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'title' );
?>
Related
So i'm trying to tweak my wordpress site by tweaking the javascript needed to reduce the image sizes on the page. I have a js folder within my child themes folder structure that has one js file in it (which has the same name as the file it is meant to be overriding). Below is the code i'm using for the functions.php file which is meant to queue up the files. In my head it might have something to do with maybe how wordpress reads file, like maybe i have to do dequeue the parents javascript file or something?
Below is the functions.php file my child theme is using
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'bootstrap','font-awesome' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
// END ENQUEUE PARENT ACTION
Below is the part of the parents function.php file containing how it loads the scripts
add_action('wp_enqueue_scripts', 'simpleshift_public_scripts');
function simpleshift_public_scripts() {
if (!is_admin()) {
wp_enqueue_script('bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array('jquery'), '3.0.0',true);
wp_enqueue_script('waypoints',get_template_directory_uri() . '/assets/js/jquery.waypoints.min.js','','3.1.1',true);
wp_enqueue_script('scrollreveal',get_template_directory_uri() . '/assets/js/scrollReveal.min.js','','2.3.2',true);
wp_enqueue_script('easing',get_template_directory_uri() . '/assets/js/jquery.easing.min.js','','1.3',true);
wp_enqueue_script('waypoints-sticky',get_template_directory_uri() . '/assets/js/sticky.min.js','','3.1.1',true);
wp_enqueue_script('nicescroll',get_template_directory_uri() . '/assets/js/nicescroll.min.js','','3.1.1',true);
wp_enqueue_script('parallax',get_template_directory_uri() . '/assets/js/parallax.min.js','','3.1.1',true);
wp_enqueue_script('simpleshift_public',get_template_directory_uri() . '/assets/js/public.js','','1.0.0',true);
wp_enqueue_script('html5shiv',get_template_directory_uri() . '/assets/js/html5shiv.js','','1.0.0',false);
wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
wp_enqueue_script('respondjs',get_template_directory_uri() . '/assets/js/respond.js','','1.0.0',false);
wp_script_add_data( 'respondjs', 'conditional', 'lt IE 9' );
}
}
You should first find your script in .php files. Depending on how its printing, you can use solution:
Its using some action, like
add_action( 'wp_enqueue_scripts', '/*some function*/' );
And in the function you'll find wp_register_script, or wp_enqueue_script:
wp_enqueue_script( $handle //you'll need handle name, ... )
After founding you need to add this code:
add_action( 'wp_enqueue_scripts', 'theme_name_scripts', 20 );
function theme_name_scripts() {
wp_dequeue_script( 'simpleshift_public' );
wp_enqueue_script( 'myscript', get_stylesheet_directory_uri() .'/js/public.js', array( 'jquery' ), '1.0', true);
}
Or the script in your theme can be printed using just <script> html tag(not recommended way ). In this case you need to comment or delete this tag from your parent theme, if you don't need it. After you just need to add your js file, using this code
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
function theme_name_scripts() {
wp_enqueue_script( 'myscript', get_stylesheet_directory_uri() .'/js/public.js', array( 'jquery' ), '1.0', true);
}
if you has active child theme, then put the code at the end of /your_child_theme_folder/functions.php file. If not, then put it at the end of your parent theme functions.php file. Be sure, that there isn't ?> closing tag. If its there, then just delete it and put the code instead of it.
I am trying to link my css file to WordPress through a functions.php file. I am not getting any errors but my page isn't showing up styled at all. Am I missing something?
Here is my code:
<?php
function my_own_styles() {
wp_enqueue_style( 'portfolio_theme', get_template_directory_uri() .
'/css/portfolio.css' );
}
add_action( 'wp_enqueue_scripts', 'my_own_styles()' );
?>
When delcaring a function in the add_action function you don't need to use the (), just the name of the function. See how your code should look below.
<?php
function my_own_styles() {
wp_enqueue_style( 'portfolio_theme', get_template_directory_uri() .
'/css/portfolio.css' );
}
add_action( 'wp_enqueue_scripts', 'my_own_styles' );
?>
If you are using a child theme - which I assume you are when you're making changes such as adding CSS and changing functions.php - then you need to use get_stylesheet_directory_uri instead of get_template_directory_uri, i.e.
function my_own_styles() {
wp_enqueue_style( 'portfolio_theme', get_stylesheet_directory_uri() .
'/css/portfolio.css' );
}
add_action( 'wp_enqueue_scripts', 'my_own_styles' );
get_template_directory_uri() returns the path to the current theme unless a child them is being used. In that case it returns the path to the parent theme.
get_stylesheet_directory_uri returns the path to the current theme even if it is a child theme.
I'm trying to create a wordpress theme and start learning the best way to include javascript and css files using wp_enqueue. After couple hours, I still can't load the files. Here is the code I use
function compro_scripts() {
wp_register_style( 'style', get_template_directory_uri() . '/css/materialize.css', array(), '20150208', 'all');
wp_enqueue_script( 'style' );
}
add_action( 'wp_enqueue_scripts', 'compro_scripts' );
It looked simple on every tutorial I've read, but it still not work for me. Would anyone mind helping me solve this ?
This very basic knowledge of Wordpress, but as a newbie I found out this is pretty confusing. If you want to use the best practice to include javascript and css files make sure to add wp_head() and wp_footer(). The rest of it is described clearly here How to Load Javascript like Wordpress Master
You've registered the "style" handle with wp_register_style() and you're enqueuing it by using wp_enqueue_script() which is incorrect. wp_enqueue_script() in your code needs to be replaced by wp_enqueue_style(), since it's a style that you are including, not a script.
Alternatively, you can simply enqueue scripts and styles directly without the need to register them separately - like this
function compro_scripts() {
$template_dir = get_template_directory_uri();
//enqueue a style
wp_enqueue_style( 'style', $template_dir . '/css/materialize.css', array(), '20150208', 'all');
//enqueue a script
wp_enqueue_script( 'custom', $template_dir . '/js/custom.js', array( 'jquery' ), false, false );
}
add_action( 'wp_enqueue_scripts', 'compro_scripts' );
No matter how hard I try, I can't successfully register my function with WordPress.
I have currently done the following:
Added code in themes functions.php
function test_my_script() {
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/gls-enable.js', array( 'jquery' ) );
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'test_my_script' );
Added my script in themes folder /js/gls-enable.js
jQuery(document).ready(function($) {
$("shipping_method_0_flat_rate").change(function () {
if ($("#shipping_method_0_flat_rate").is(":checked")) {
window.alert("sometext");
});
});
Called my script on the page where I want it to work
<?php test_my_script(); ?>
When I view the source on the mentioned page, the script isn't to be found anywhere.
You're going to want to get rid of the register_script:
function test_my_script() {
wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/gls-enable.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'test_my_script' );
Also, by doing this, your script will be automatically enqueued when the 'wp_enqueue_scripts' action hook is called. You don't need to explicitly call it in your template via php.
There's also an error in your jQuery syntax. It should be:
jQuery(document).ready(function(){
$("#shipping_method_0_flat_rate").change(function(){
if ( $("#shipping_method_0_flat_rate").is(":checked") ) {
window.alert("sometext");
}
});
});
First of all you don't need to call that test_my_script() inside your page. It's a hook to wp_enqueue_scripts and it is automatically loaded by wordpress and this means the error should be on javascript.
Here : $("shipping_method_0_flat_rate")., add a # or . ( if that is an id or a class name );
Maybe the problem is abouve registering that action in php, so better set WP_DEBUG to true ( root of your wordpress and edit wp-config.php )
If you get some errors after setting WP_DEBUG to true solve them and also post the error you get inside console ( you should have :D )
And is a good practice to load al javascript files in footer ( this file is loaded in header ). Maybe the jquery is in footer and gls-enable.js is loaded before jquery.
To load this in footer :
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/gls-enable.js', array( 'jquery' ), '1.0', true );
I'm trying to get unlider.js working on Wordpress. My guess is that the registration of the script is not working properly.
I am using a child theme, and this is the code I put in functions.php to register the script (taken from WP guide).
Clearly, I am doing something wrong, perhaps the script is being inserted before jQuery.
What is the best way to attach a script from a child theme, after jQuery?
One I have so far is this:
<?php
add_action( 'wp_enqueue_scripts', 'child_add_scripts' );
function child_add_scripts() {
wp_register_script(
'google-analytics',
get_stylesheet_directory_uri() . '/unslider.js',
array( 'jquery' ),
false,
'1.0',
true
);
wp_enqueue_script( 'google-analytics' );
}
?>