WordPress.org

Support

Support » Plugins and Hacks » WP Social Bookmarking Light » Only load the plugin when it is used on the page

Only load the plugin when it is used on the page

Viewing 1 replies (of 1 total)
  • 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.

Viewing 1 replies (of 1 total)
  • The topic ‘Only load the plugin when it is used on the page’ is closed to new replies.
Skip to toolbar