WordPress.org

Ready to get started?Download WordPress

Forums

WP Social Bookmarking Light
Only load the plugin when it is used on the page (2 posts)

  1. thisebum
    Member
    Posted 1 year ago #

    Hello,

    I tried a lot of social sharing plugins and yours is the best!

    The only problem I have is that the plugin (the js files and css) are loaded even if they are not used on the page, which increase the loading time of the page.

    Could you please update it so the plugin does not load on the page where it is not used?

    Thanks a lot for your support.

    http://wordpress.org/extend/plugins/wp-social-bookmarking-light/

  2. thisebum
    Member
    Posted 1 year ago #

    I found a solution myself (I don't know if it is clean but it seems to work).

    It works in 2 steps:

    In the main plugin file called "wp-social-bookmarking-light.php", I changed the last lines (lines 52 to 60) :

    // initialize
    function wp_social_bookmarking_light_init()
    {
        add_action('wp_head', 'wp_social_bookmarking_light_wp_head');
        add_action('wp_footer', 'wp_social_bookmarking_light_wp_footer');
        add_filter('the_content', 'wp_social_bookmarking_light_the_content');
        add_action('admin_menu', 'wp_social_bookmarking_light_admin_menu');
    }
    add_action( 'init', 'wp_social_bookmarking_light_init' );

    to:

    // initialize
    function wp_social_bookmarking_light_init()
    {
        add_action('admin_menu', 'wp_social_bookmarking_light_admin_menu');
    }
    add_action( 'init', 'wp_social_bookmarking_light_init' );
    
    // enqueue scripts
    function wp_social_bookmarking_light_enqueue_scripts()
    {
        add_action('wp_head', 'wp_social_bookmarking_light_wp_head');
        add_action('wp_footer', 'wp_social_bookmarking_light_wp_footer');
        add_filter('the_content', 'wp_social_bookmarking_light_the_content');
    }
    add_action( 'wp_enqueue_scripts', 'wp_social_bookmarking_light_enqueue_scripts' );

    With this change the action for the admin menu is still loaded on "init" but the other actions of the plugin are loaded on "wp_enqueue_scripts".

    Then in my "functions.php" file in my theme folder, I added the following lines:

    function remove_wp_social_bookmarking_light() {
        if( ( !is_single() ) ) {
           remove_action( 'wp_enqueue_scripts', 'wp_social_bookmarking_light_enqueue_scripts' );
        }
    }
    
    add_action('template_redirect','remove_wp_social_bookmarking_light');

    This code remove the WP Social Bookmarking Light scripts everywhere on the website except on single posts.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic