Can you please add a hook to _add_js_css_hooks (), so other components can also use it?

Hi,

I want to register your plugin with other components which are not part of BuddyPress core.
Currently, the function _add_js_css_hooks () doesn't allow it ( It only checks for the Activity/Group components).

I have added this filter to the code's 'if' statement:
|| apply_filters( 'bpfb_register_other_components', false )

Can you please add this filter to your next version, so I wont have to update my code on each version?

The complete function now looks like this:

function _add_js_css_hooks () {
		global $bp;

			if ( (
				// Load the scripts on Activity pages
				(defined('BP_ACTIVITY_SLUG') && bp_is_activity_component())
				||
				// Load the scripts when Activity page is the Home page
				(defined('BP_ACTIVITY_SLUG') && 'page' == get_option('show_on_front') && is_front_page() && BP_ACTIVITY_SLUG == get_option('page_on_front'))
				||
				// Load the script on Group home page
				(defined('BP_GROUPS_SLUG') && bp_is_groups_component() && 'home' == $bp->current_action)
				)
			||  apply_filters( 'bpfb_register_other_components', false )
		){
			// Step1: Load JS/CSS requirements
			add_action('wp_print_scripts', array($this, 'js_plugin_url'));
			add_action('wp_print_scripts', array($this, 'js_load_scripts'));
			add_action('wp_print_styles', array($this, 'css_load_styles'));

			do_action('bpfb_add_cssjs_hooks');
		}
	}

Regards,
Roei.

  • Paul

    @rbahumi, I'm trying to understand your need for a new filter as part of the conditional statement. It appears you want to control if the following add_action statements are executed.

    But you should not need us to make changes to our logic. You should be able to control this easily outside of our plugins. Here is how.

    During the init process we register the action to call our '_add_js_css_hooks' function. See the line 328 in class_bpfb_binder.php

    add_action('init', array($this, '_add_js_css_hooks'));

    In your own theme/plugin you can provide your own conditional logic. Then if the conditions are not met you can simple remove or deregister our action ( http://codex.wordpress.org/Function_Reference/remove_action )

    I'm assuming you understand PHP and WordPress hook enough since you provided some code suggestions.

  • rbahumi

    Hi @PaulM

    My intentions were not to control whether or not the 'add_action' statements are executed. They were to expend its conditions, so in addition, the 'BuddyPress Activity Plus' will work with my own plugin.

    First, I want to ask how do I use remove_action outside the class scope?
    What will replace the $this variable?

    I tried replacing the '$this' with a string of the class name:

    function remove_function_add_js_css_hooks(){
    	remove_action('init', array('BpfbBinder', '_add_js_css_hooks'));
    }
    add_action('bpfb_add_ajax_hooks', 'remove_function_add_js_css_hooks' );

    It didn't work.

    The second thing is, that even if this will work, this is not an elegant solution because, it requires code duplication, keeping up with your implementation and the included files of '_add_js_css_hooks()':

    function new_add_js_css_hooks () {
    	global $bp;
    
    		if (
    			// Load the scripts on Spots pages
    			(defined('BP_SPOTS_SLUG') && bp_is_spots_component())
    			||
    			// Load the scripts on Activity pages
    			(defined('BP_ACTIVITY_SLUG') && bp_is_activity_component())
    			||
    			// Load the scripts when Activity page is the Home page
    			(defined('BP_ACTIVITY_SLUG') && 'page' == get_option('show_on_front') && is_front_page() && BP_ACTIVITY_SLUG == get_option('page_on_front'))
    			||
    			// Load the script on Group home page
    			(defined('BP_GROUPS_SLUG') && bp_is_groups_component() && 'home' == $bp->current_action)
    		) {
    		// Step1: Load JS/CSS requirements
    		add_action('wp_print_scripts', array('BpfbBinder', 'js_plugin_url'));
    		add_action('wp_print_scripts', array('BpfbBinder', 'js_load_scripts'));
    		add_action('wp_print_styles', array('BpfbBinder', 'css_load_styles'));
    
    		do_action('bpfb_add_cssjs_hooks');
    	}
    }
    add_action('init', 'new_add_js_css_hooks');
  • Paul

    @rbahumi, Looking at the plugin code I see your issue. The class is not public. You you would need to first instantiate your own instance. BUT the secondary issue is you would also need to include the file where the class lives. So not an easy task.

    I did not mean to imply you should duplicate the code from the plugin. But more that you setup your own action which would run first. Then is your condition is met you can then call the plugin action directly. But per me previous point you would need to include the class file yourself.

    Maybe something like this.

    function my_function() {
    
    	require_once(BPFB_PLUGIN_BASE_DIR . '/lib/class_bpfb_binder.php');
    	$bpfbbinder = new BpfbBinder();
    
    	// Your own custom conditional statement
    	if ("East" != "West") {
    		remove_action('init', array($bpfbbinder, '_add_js_css_hooks'));
    	} else {
    		$bpfbbinder->_add_js_css_hooks();
    	}
    }
  • rbahumi

    @PaulM, I think I didn't explain myself correctly... I'll try to explain again.

    Calling the action '_add_js_css_hooks()' doesn't help me. The action has an 'if' condition, that is only satisfied if the current component is either 'Activity' or 'Group';

    I want my own BuddyPress component, named 'Spots', to satisfy the 'if' condition. That is why, I need to expend the 'if' statement, so it will also be satisfied by my personal component.

    My idea for doing so (in my initial post), was to add this filter to the 'if' statement, that will enable users to hook this plugin elegently:
    || apply_filters( 'bpfb_register_other_components', false )

    So, removing the action is not what I need. I want it to execute when the component is either 'Activity', 'Groups' or my own 'Spots'.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.