PHP function to hide CSS and Javascript does not work - php

Remove CSS and JavaScript with a PHP function like this:
function disable_files() {
wp_dequeue_style('blocks');
wp_dequeue_style('library');
wp_dequeue_script( 'jquery-js' );
add_action('wp_enqueue_scripts', 'disable_files', 100); }
When I use this function in function.php nothing happens. Is there something missing in the script?

deregister was missing
function disable_files() {
wp_dequeue_style('blocks');
wp_dequeue_style('library');
wp_dequeue_script('jquery-js');
wp_deregister_style('blocks');
wp_deregister_style('library');
wp_deregister_script('jquery-js');
}
add_action( 'wp_enqueue_scripts', 'disable_files', 9999 );
add_action( 'wp_head', 'disable_files', 9999 );

Related

How do I correctly remove a JavaScript file in php?

I have a problem with removing JavaScript file in functions.php in WordPress.
This is the file I want to remove:
../js/scripts/wc/productFilters.min.js?ver=6.1.4' id='wd-product-filters-js'></script>
And this is my code. I put it on functions.php but it didn't work:
function dequeue_filter_js() {
wp_dequeue_script( 'wd-product-filters' );
wp_deregister_script( 'wd-product-filters' );
}
add_action( 'wp_enqueue_scripts', 'dequeue_filter_js', 1000 );
Thank You in Advance

How to get access to jquery in wordpress?

In my theme header.php i added:
<?php wp_enqueue_script("jquery"); ?>
And try to use jquery in functions.php:
function remove_xprofile_links() {
remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 2 );
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$field = xprofile_get_field_data(3, $user_id);
if($field="Покупатель")
{
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
$("#nickname,#display_name").parent().parent().hide();
});
</script>
<?php
}
}
add_action( 'bp_init', 'remove_xprofile_links' );
But in console still ReferenceError: jQuery is not defined
How to use jquery correctly?
Probably you added the wp_enqueue_script line in your header.php after the wp_head() function has already been executed, but that's not the real issue.
The problem is that you shouldn't enqueue your scripts in header.php, but in your theme's functions.php file, adding an action to the wp_enqueue_scripts hook, like explained in this example:
/**
* Proper way to enqueue scripts and styles
*/
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
In your case this should be enough:
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script('jquery');
} );

Enqueing a jQuery script in Wordpress

I am trying to enqueue a jQuery script to my Wordpress site but for some reason it is not adding. Below is my code I've added to my child theme's functions.php file.
<?php
function itm_adding_scripts() {
wp_register_script('my_amazing_script', '/wp-content/themes/italic-child/js/jQuery.equalHeights.js/js/jQuery.equalHeights.js', array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( '_enqueue_scripts', 'itm_adding_scripts' );
?>
You have a typo in the action missing wp prefix
Change
add_action( '_enqueue_scripts', 'itm_adding_scripts' );
To
add_action( 'wp_enqueue_scripts', 'itm_adding_scripts' );
Reference: wp_enqueue_scripts docs
You have a typo in your action. It should be wp_enqueue_scripts
add_action( 'wp_enqueue_scripts', 'itm_adding_scripts' );

How do I add Typekit Script to Wordpress?

Is it possible to add the the typekit embed code to wordpress by using wp_enqueue_script()?
Here is what I have so far but I'm not seeing any results:
function typekit_enqueue_script() {
wp_enqueue_script(
'typekit',
'//use.typekit.net/weq7wou.js',
array(),
null,
true
);
}
add_action( 'wp_enqueue_script', 'typekit_enqueue_script' );
Are you loading Typekit afterwards?
function load_typekit() {
echo '<script>try{Typekit.load();}catch(e){}</script>';
}
add_action( 'wp_head', 'load_typekit', 300 );
Or just add the load script in one of your js:
try{Typekit.load();}catch(e){}
This should work:
wp_enqueue_script( 'typekit','//use.typekit.net/weq7wou.js',array() );

Wordpress init remove_action with conditional tags

I need to do this for a theme:
remove_action( 'wp_head', 'rel_canonical' );
BUT I need to do that with conditional tags. The code below don't work.
if(is_page('comment'))
{
remove_action( 'wp_head', 'rel_canonical' );
}
AND I need to do this with a plugin.
I tried to hook the if statement into the function test, like this:
add_action('init', 'test');
function test()
{
if(is_page('comment'))
{
remove_action( 'wp_head', 'rel_canonical' );
}
}
Because it is run before anything else the conditional tags don't work, I guess.
Any ideas?
I found out that instead of using init as an action, I should use this:
add_action('template_redirect', 'test');
Then it runs before the header.php but after the conditional tags are set.
Try replacing the rel_canonical action with your own function containing the condition, something like this:
remove_action('wp_head', 'rel_canonical');
function my_rel_canonical() {
if (!is_page('comment')) rel_canonical();
}
add_action('wp_head', 'my_rel_canonical');

Categories