Notifications
Clear all

wpForo 1.x.x [Closed] Remove CSS and JS on non wpForo page

11 Posts
3 Users
3 Reactions
7,572 Views
BigBatT
Posts: 12
Topic starter
(@bigbatt)
Eminent Member
Joined: 5 years ago

Hi guys,

I am a new user happy to use wpForo. Great job, great plugin.

I have to ask one thing: Is it possible to remove all CSS and Js files from pages that are not forums?

Thanks in advance and best regards

Topic Tags
10 Replies
Sofy
Posts: 4900
 Sofy
Admin
(@sofy)
Support Team
Joined: 7 years ago

Thank you @bigbatt!

The wpForo scripts and CSS are being loaded only on the wpForo pages.

Probably you see the wpForo JS and CSS because you've added the wpForo widgets on that pages? 

2 Replies
BigBatT
(@bigbatt)
Joined: 5 years ago

Eminent Member
Posts: 12

@sofy

I don't use widgets wpForo in any pages
If you want see my site here

The forum page is full width but without any widgets

Other plugins I use:

Akismet Anti-Spam 4.1.3

TinyMCE Advanced 5.3.0

Ultimate Member 2.1.4

Ultimate Member - Maybe load assets 1.0.2

wpDiscuz 5.3.5

 

Sofy
 Sofy
Admin
(@sofy)
Joined: 7 years ago

Support Team
Posts: 4900

ok @bigbatt, thank you!

let me ask the developers about this. I'll back to you asap. 

 

Sofy
Posts: 4900
 Sofy
Admin
(@sofy)
Support Team
Joined: 7 years ago

@bigbatt,

The CSS and JS files belong to the wpForo widgets. I'm really sorry, but currently, there is no way to not load the files if the widgets are not included on that page. However, the developers are going to optimize this in future updates. 

1 Reply
BigBatT
(@bigbatt)
Joined: 5 years ago

Eminent Member
Posts: 12

@sofy,

Below is the code used by Ultimate Member.
Maybe it can serve as an example.

Best regards


<?php

/*
Plugin Name: Ultimate Member - Maybe load assets
Description: Remove CSS and JS on non UM pages
Version: 1.0.2
Author: Ultimate Member
Author URI: http://ultimatemember.com/
*/

add_action( 'wp_print_footer_scripts', 'um_remove_scripts_and_styles', 9 );
add_action( 'wp_print_scripts', 'um_remove_scripts_and_styles', 9 );
add_action( 'wp_print_styles', 'um_remove_scripts_and_styles', 9 );
add_action( 'dynamic_sidebar', 'um_remove_scripts_and_styles_widget' );

/**
* Maybe remove Ultimate Member CSS and JS
* @global WP_Post $post
* @global bool $um_load_assets
* @global WP_Scripts $wp_scripts
* @global WP_Styles $wp_styles
* @return NULL
*/
function um_remove_scripts_and_styles() {
global $post, $um_load_assets, $wp_scripts, $wp_styles;

// Set here IDs of the pages, that use Ultimate Member scripts and styles
$um_posts = array(0);

// Set here URLs of the pages, that use Ultimate Member scripts and styles
$um_urls = array(
'/account/',
'/activity/',
'/groups/',
'/login/',
'/logout/',
'/members/',
'/my-groups/',
'/password-reset/',
'/register/',
'/user/',
);

if ( is_admin() || is_ultimatemember() ) {
return;
}

$REQUEST_URI = $_SERVER['REQUEST_URI'];
if ( in_array( $REQUEST_URI, $um_urls ) ) {
return;
}
foreach ( $um_urls as $key => $um_url ) {
if ( strpos( $REQUEST_URI, $um_url ) !== FALSE ) {
return;
}
}

if ( !empty( $um_load_assets ) ) {
return;
}

if ( isset( $post ) && is_a( $post, 'WP_Post' ) ) {
if ( in_array( $post->ID, $um_posts ) ) {
return;
}
if ( strpos( $post->post_content, '[ultimatemember_' ) !== FALSE ) {
return;
}
if ( strpos( $post->post_content, '[ultimatemember form_id' ) !== FALSE ) {
return;
}
}

if ( empty( $wp_scripts->queue ) || empty( $wp_styles->queue ) ) {
return;
}

foreach ( $wp_scripts->queue as $key => $script ) {
if ( strpos( $script, 'um_' ) === 0 || strpos( $script, 'um-' ) === 0 || strpos( $wp_scripts->registered[$script]->src, '/ultimate-member/assets/' ) !== FALSE ) {
unset( $wp_scripts->queue[$key] );
}
}

foreach ( $wp_styles->queue as $key => $style ) {
if ( strpos( $style, 'um_' ) === 0 || strpos( $style, 'um-' ) === 0 || strpos( $wp_styles->registered[$style]->src, '/ultimate-member/assets/' ) !== FALSE ) {
unset( $wp_styles->queue[$key] );
}
}
}

/**
* Check whether Ultimate Member widget was used
* @param array $widget
*/
function um_remove_scripts_and_styles_widget( $widget ) {
if ( strpos( $widget['id'], 'um_' ) === 0 || strpos( $widget['id'], 'um-' ) === 0 ) {
$GLOBALS['um_load_assets'] = TRUE;
}
}

 

Posts: 1602
(@anonymous20)
Noble Member
Joined: 9 years ago

I don't see any .js from wpForo in your page, only 2 css.

Still, what is the exact problem you are having with that?

1 Reply
BigBatT
(@bigbatt)
Joined: 5 years ago

Eminent Member
Posts: 12

@anonymous20

You sure didn't get a good look at her.
wpForo loads 3 CSS and 1 JS in every part of the site.
This causes slowdowns and conflicts with other CSS/JS.

If you don't understand what I'm asking, read the previous post where I indicate how Ultimate Member solved this issue by creating an ad-hoc plugin that loads UM files only in the dedicated pages and not in the whole website

 

Regards

 

Posts: 1602
(@anonymous20)
Noble Member
Joined: 9 years ago

@bigbatt

No conflicts from wpForo unless you optimize things. I have a tone load of plugins/js/css in my pages, i even optimize them, merge them, deffere them, i haven't seen any conflict.

Also i see no slowdowns or anthything bad from your site:

agetest.org/result/200309_FF_b760189de055f2e4d42e42dc07e9d350/

And there is a widget.js, you are correct, still i fail to see any problems.

 

2 Replies
BigBatT
(@bigbatt)
Joined: 5 years ago

Eminent Member
Posts: 12

@anonymous20

I hope you don't want to make a problem from a simple request, beacouse I don't care.

I just asked the wpForo Team if it was possible to load files CSS and JS only in the forum page; if possible same to the Ultimate Member.

https://developers.google.com/speed/pagespeed/insights/?hl=IT&url=https%3A%2F%2Fwww.bigbatt.it%2F&tab=desktop

That's all.

 

Sofy
 Sofy
Admin
(@sofy)
Joined: 7 years ago

Support Team
Posts: 4900

@bigbatt,

I've already answered your question here: https://wpforo.com/community/how-to-and-troubleshooting-2/remove-css-and-js-on-non-wpforo-page/#post-37748

Thank you for the code you've provided us. I'll inform the developers for sure. Currently, I close this topic.