Visual Composer conflicting with enqueued custom script - wordpress

I have a wordpress page with Visual composer and a custom theme.
The custom theme contains a script. Everything works fine until i use wp_enqueue_script for my custom script. All js-related functions by VC cease to work.
Do i have to enqueue VC scripts? How can i achieve this?
require_once('wp-bootstrap-navwalker.php');
add_theme_support('post-thumbnails');
add_action("wp_enqueue_scripts", "adapt_enqueue");
add_action('init', 'register_main_menu');
function adapt_enqueue()
{
//VC Works if i remove this line:
wp_enqueue_script('app', get_stylesheet_directory_uri() . '/js/app.js', ['jquery'], 5.0);
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/style.css', array(),5.0);

Related

How can I create a WordPress Plugin with custom CSS?

I want to create a WordPress Plugin where I want to add some css, and upon activation, those css will be in effect and be viewing in the website. Upon deactivation, the css will also be out of effect.
Any how I can do that? Or any documentation I can take help from?
You can follow Plugin Handbook for creating a plugin https://developer.wordpress.org/plugins/.
create a CSS file in plugin directory then upon plugin activation hook you can enqueue the CSS file using action 'wp_enqueue_style'.
You can add this to your main file to load your css file:
function add_my_css() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'style', $plugin_url . "/css/plugin-style.css");
}
add_action( 'wp_enqueue_scripts', 'add_my_css' );
Then create your plugin-style.css file and write your css.
Hope this helps

CSS won't connect to my WordPress site when using wampserver, I can't seem to find an answer anywhere

I am learning wordpress through a learning website and I've got everything written exactly as they do, yet I just can't seem to get my stylesheet to connect. I'm currently using wampserver and my editor is visual studio code. I've looked through the codex, stackoverflow, and whatever else I can find out there to help me but nothing seems to be working.
Functions.php:
<?php
function learningwordpress_styles() {
//add the stylesheets
wp_register_style('style', get_template_directory_uri() . '/style.css', array('normalize'), '1.0.0');
wp_register_style('normalize', get_template_directory_uri() . '/css/normalize.css', array(), '8.0.1');
//enqueue the style
wp_enqueue_style('style');
wp_enqueue_style('normalize');
};
add_action('wp_enqueue_scripts', 'learningwordpress_styles');

Adding scrollr in wordpress

I'm fairly new to wordpress and custom templates.
I'm trying to add parallax to a site I'm working on and want to use 'skrollr'.
I've downloaded it and have the skrollr.min.js file, which I've placed in my projects js/vendors/ directory.
What I'm trying to figure out in Wordpress is the proper way to add this to your project.
How do you typically add Jquery and other js libraries?
you can include your js and jquery files into your wordpress by simply adding the code
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/assets/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
Just simply put your jquery file inside the assets/js folder. from there on use the path that your jquery file makes.
Enter the above code in your functions.php
That should include your above file in the wordpress.
If you further have any issues or problems let me know

can't change version using wp_enqueue_style or anything

I've enqueued a style sheet using
wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() .
'/css/style.css');
in my child theme
then when I tried to modify it, I've realised that wordpress gave it an automatic version, which now I can't change.
I have tried modifying the enqueue code adding a version,
wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() .
'/css/style.css' , array(), '30140222');
also tried dequeuing it and enqueuing it again, and deleting the file and readding it in the folder but nothing is working.
except null.
For now I made a hard refresh (bypassing my cache) pressing Ctrl + F5 in the browser, cause I'm on the local host, but still need an answer to use it on the server.
heads-up the following applies to scripts too, just replace style with script and you're good to go
The problem is that you're trying to modify an already registered style. See, when you call wp_enqueue_style(), the style is registered and enqueued at the same time(vs calling wp_register_style() and then wp_enqueue_style() which I recommend).
However, if a style has already been registered, when you try to enqueue it the extra parameters are not used, they're just ignored.
What you need to do is the following( make sure to fill in the correct parameters in the wp_register_style() below):
wp_deregister_style( 'my-style' );
wp_register_style( 'my-style', ... );
wp_enqueue_style( 'my-style' );
This first deregisters the still you want to modify and then registers it again with the modified parameters. This will work with styles that are used as a dependency or have dependencies of their own, because dependencies are resolved at the time the style is being loaded on the page and not when it's registered/enqueued.
For what you're trying to achieve though, I would personally just register whatever scripts and styles I want to override in the child theme before the parent theme does it. That way you won't have to deregisters and register them. Here's an example(part of it is the sample parent theme's code):
function parent_theme_enqueue_scripts(){
wp_enqueue_style( 'example-style', get_bloginfo( 'template_directory' ) . '/css/example.css', array(), 'v1.0.0' );
}
add_action( 'wp_enqueue_scripts', 'parent_theme_enqueue_scripts', 10 );
function child_theme_enqueue_scripts(){
wp_register_style( 'example-style', get_bloginfo( 'stylesheet_directory' ) . '/css/example.css', array(),'v1.2.0' );
}
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_scripts', 5 );
The important thing to notice here is the priority on child_theme_enqueue_scripts - it's lower than that of parent_theme_enqueue_scripts. Also note that we're not directly enqueue-ing the style, but instead we're just registering it. That's in order to not mess up the loading order of styles(in case the parent theme enqueues multiple styles but doesn't use dependencies to do that).
Just add this function in function.php
function wpa() {
wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() . '/css/style.css' , array(), '30140222' );
}
add_action('wp_enqueue_scripts', 'wpa');
You should try to search your themes & plugins folders for functions that modify your enqueued styles and scripts.
I was looking for lines containing style_loader_src or ver= and discovered a plugin called "Child Theme Configuratior" that was the culprit.
Turned that plugin off and everything started to work as expected.

I'm stuck trying to use wp_enqueue and Checking if the toolbar is activated on frontend

I am currently developing a theme and i am having issues when it comes to wp_enqueue. This is what i got but it isn't working.
function theme_name_styles() {
wp_enqueue_script( 'style-name',get_template_directory_uri() . '/css/dropdown.css' );
}
add_action( 'wp_enqueue_scripts', 'theme_name_styles' );
secondly i am trying to tell if the toolbar is active on the frontend of wordpress or not and using conditions to display something if it is not active.
wp_enqueue_script is used to enqueue script , use wp_enqueue_style instead
other thing is its better to register script or style first before using
wp_register_script // to register script
wp_register_style // To register style
then enqueue using wp_enqueue_script wp_enqueue_style // learn more about it on codex
Try this :
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );
function register_plugin_styles() {
wp_register_style( 'style-name', get_template_directory_uri() . '/css/dropdown.css' );
wp_enqueue_style( 'style-name' );
}
More info : http://codex.wordpress.org/Function_Reference/wp_register_style
For the toolbar, maybe you can find something here: http://digwp.com/2011/04/admin-bar-tricks/
There are specific functions to enqueue scripts and styles in wordpress. wp_enqueue_script is used to enqueue scripts only and wp_enque_style is used to enqueue stylesheets. These enqueued files will be attached at the point where you called the wordpress function wp_head. Note that these functions check for MIME type so that if you trying to enqueue a script using the wp_enqueue_style then it'll assume your stylesheet as a script and vice versa. You can see the type and explanation of the error if you use a debugger tool. The developers tool in google chrome is quite awsome. You can use this.
There is a function is_admin_bar_showing which can tell explicitly whether the admin bar is showing in the front end or not. http://codex.wordpress.org/Function_Reference/is_admin_bar_showing

Resources