Wordpress plugin - how to disable js/css per page - php

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.

You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

Related

Include PHP files depending on Wordpress Templates being used

I am using WordPress for my site. The site uses many different PHP page templates to display information to the user. At the moment I include all PHP files necessary for posting data to the backend of the site in a common functions.php file e.g. require_once("function1_functions.php"); require_once("functions2_functions.php"); etc
The issue I am having is all of my functions#_functions.php files are loaded on every page, I don't want this. There is an IF statement you can use in WordPress that will allow you to put call certain files on a page depending on the template (page) the user is currently on but this doesn't work for PHP files.
if (is_page_template( array('pagetemplate-about-us.php') )) {
// call JS files here that will only show on the "About us" page
require_once("functions_about_us.php"); <-- This doesn't work.
}
Any ideas?
The Conditional Tags which control what content is displayed with conditions (such as is_page_template(). Note these only work after the posts_selection action. So here the setup function is actioned at wp action, just after posts_selection.
More info here on Conditional Tags.
if (!function_exists('theme_php_include_setup')):
function theme_php_include_setup() {
if (is_page_template( array('pagetemplate-about-us.php') )) {
require_once("functions_about_us.php");
}
}
// apply conditions at appropriate WP action
add_action('wp', 'theme_php_include_setup');

Conditionally Load Styles and Scripts in Wordpress

I'm looking for a solution to conditionally load scripts and styles on specific Wordpress pages. I've searched for solutions, but have hit a dead end. I haven't been able to use php selectors, such as is_page(), in the functions.php file to do this. Here is an example of my past attempt:
function blog_style() {
if ( is_page('blog') ) {
wp_enqueue_style( 'blog' );
}
}
add_action( 'wp_print_styles', 'blog_style' );
I have a feeling this is because the functions.php file is loaded before the page has been identified. Any advice is helpful. I have a basic understanding of php and Wordpress codex. Thanks!
I understand your confusion in this matter and must confess that WordPress sometimes has strange ways to handle things.
First of all; you should enqueue scripts and styles in the wp_enqueue_scripts hook:
add_action('wp_enqueue_scripts', 'themeslug_styles');
function themeslug_styles() {
wp_enqueue_style(...);
}
Or in shorthand-/anonymous-function-format, if you prefer:
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style(...);
});
The basic idea of your is_page('blog') in an if-statement is correct. But also check out this detailed info about conditional tags - maybe there are better ways to target the blog-page of your website (see "The Blog Page" on the linked resource).
You must use the if-statement inside your function, because it is only at the point of wp_enqueue_scripts, where the function is run, that WordPress has information about the actual content ready.
add_action('wp_enqueue_scripts', function () {
if (is_home()) {
wp_enqueue_style('themeslug-stylename', URL-to-your-style);
wp_enqueue_script('themeslug-scriptname', URL-to-your-script);
}
});
Addition: You are right, the functions.php file is mainly used to set everything up and connect functions as actions to WordPress hooks. Therefore, the code is executed quite early in the WordPress execution flow to give you access to as many hooks as possible. The downside is, as you experienced, that many things are not directly accessable at this point.
I think you are using the wrong hook here. Try
add_action( 'wp_enqueue_scripts', 'blog_style' );
You can check this function. I had used this function to dynamically load scripts on different post types and pages. I used the screen_id to specify the current page.

How to hide plugins style sheets in wordpress

i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.
we have used following code in plugin, but it doesn't work. please help how to use it.
if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');
If you are modifying your own plugin I see no reason your code wouldn't work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don't intend.
However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:
if (is_singular('post')) {
wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
}
However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update... it is very difficult to maintain that sort of modifications in the long run.
Instead make a new plugin and modify whatever you need from there.
What you want to achieve can be accomplished by:
Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.
Inside that folder create a new file called my_load_reducer.php
Paste this in the file:
<?php
/*
Plugin Name: My Load Reducer
Description: Removes unneeded and unwanted stylesheets from other plugins
Version: 0.1
*/
//Use a class to avoid conflicts
class my_load_reducer {
function __construct() {
//Hook into wp_enqueue_scripts with a high priority
add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
}
function deregister_styles() {
//Check that current post is not a single post
if (!is_singular('post')) {
//deregister the stylesheet - this removes the twentyfifteen
//main stylesheet - obviously you need to substitute the handle
//of the stylesheet you actually want to remove
wp_deregister_style( 'twentyfifteen-style' );
}
}
}
//Instantiate the class
$my_load_reducer = new my_load_reducer();
Activate the plugin through the wordpress admin.
You can remove perticular plugin css on selected page.
below code is remove plugin css to other pages and display only on post pages:
/*disable loading plugin css to page and load on post page*/
add_action('wp_print_styles', 'my_deregister_styles', 99999);
function my_deregister_styles()
{
if(!is_single())
{
wp_dequeue_style('plugin-css-handle');
wp_deregister_style('plugin-css-handle');
}
}
where 'plugin-css-handle' is perticular plugin's css handle which you want to remove.

How does wordpress wp_enqueue_script() does work more exactly

wp_enqueue_script() as I read about so far and went through the source code instantiates new wp_scripts object 'representing' the script.
My question is how does wordpress know when to load the the script source when needed ?
For example, on index I need bootstrap and jquery, I enqueue bootstrap with jquery dependency in functions.php. How does wordpress know to automatically load the bootstrap on the first page ? What I want to understand is the logic behind it.
I have to create a new plugin and I need some scripts for a slideshow, what I want is to create a quality plugin, optimized using wp_enqueue_scripts, but I don't really understand the concept in-depth so I can use it properly (to load the scripts only when the plugin is activated)
You can treat wp_enqueue_scripts as a kind of action_hook. I.e. when it is time to create pages, it will include the file. You can also make it dependant on another file being loaded, so if jQuery is needed, you can stipulate this in the function and it will load after jQuery. This maintains seperation of your code.
If you want to optimize the code, you can make the function dependant on the page you are loading.
Forexample:
function prepare_scripts() {
if(is_front_page() )
wp_enqueue_scripts('myscript', 'path to script', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'prepare_scripts' );
As #knoblik suggests, you can set the priority for the add_action() if needed. This will probably enqueue the script in the order of the priority.

Checking for buddypress from a theme

How can I check for buddypress from a theme? I've found this page for BP Plugin development
but this action never gets loaded if I hook from a theme. Why does it not work?
P.S. I need call some BP's functions from a theme, like: Show "BP's Activity Stream" at specific places.
What I mean with "never gets loaded" is:
(File in subdirectory of theme, and included in functions.php)
function sometestfunction() {
exit();
}
add_action ('bp_include', 'sometestfunction');
This must make wordpress show a blank page, won't ?
Simple check if function 'bp_is_active' is defined:
if ( function_exists('bp_is_active') ) {
// do something here...
}
see shanebp answer and comments.
this action never gets loaded
You mean never gets called?
Not sure why that would be, since you don't provide any code.
But it's meant for loading plugins.
If BP is installed, it is loaded by the time theme templates are parsed.
Have you tried calling BP functions in your theme?
I've never had a problem doing that.
Put your function(s) in one of these places:
a plugin
theme/functions.php
bp-custom.php
http://codex.buddypress.org/plugindev/bp-custom-php/
If your code will be used on installs you don't control, you should check for a BuddyPress component:
if( bp_is_active( 'activity' ) {
http://codex.buddypress.org/developer/bp-is-active/

Categories