Skip to:
Content
Pages
Categories
Search
Top
Bottom

Conditional enqueue in header


  • aaclayton
    Participant

    @aaclayton

    Hi bbPress community,

    Thank you for all your collective hard work on this great plugin. I’m working on an integration of the new 2.1 plugin into my own theme, and I had a question about scripts and styles that bbPress enqueues into the wp_head.

    In general, for plugins which are highly localized on the site (ones that only get used in very specific places), I prefer to conditionally enqueue styles and scripts to keep the head section of the main site relatively clean. bbPress currently initializes the following code site-wide:

    /** Scripts ***********************************************************/
    
    add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
    add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
    add_filter( 'bbp_enqueue_scripts',      array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
    add_action( 'bbp_head',                 array( $this, 'head_scripts'          ) ); // Output some extra JS in the 
    

    Normally I’m fairly handy with actions and filters in my functions.php file, but I’ve had a load of trouble getting these scripts to conditionally enqueue. Ideally, I would want to condition them on is_bbpress(). Can anyone offer a snippet to filter these unless is_bbpress() is true?

    Thanks in advance, and let me know if my question is unclear in any way.

    Andrew

Viewing 11 replies - 1 through 11 (of 11 total)

  • Jared Atchison
    Moderator

    @jaredatch

    You don’t need to worry about using that conditional because those actions only fire on bbPress sections of the site :)


    aaclayton
    Participant

    @aaclayton

    Hey Jared, thanks for the reply. Unfortunately (perhaps due to misconfiguration on my part) these actions are firing on all site pages. Specifically I believe that bbPress is responsible for:

    link rel='stylesheet' id='bbp-child-bbpress-css'
    

    and

      /*  */
    

    I have bbPress configured to use theme compatibility base settings, upon which I am hand tuning templates within my theme directory. Any ideas why the child-css is getting called ubiquitously? The AJAX reference also showed up only after I installed bbPress, and I’m not convinced that it’s necessary…

    Any ideas would be great, thanks again. Andrew

    EDIT: Wow, I’m having a lot of trouble getting this code to display in blocks without getting stripped, so I had to throw in some unneccesary single quotes, but you get the idea…

    • This reply was modified 2 years, 1 month ago by  aaclayton.
    • This reply was modified 2 years, 1 month ago by  aaclayton.
    • This reply was modified 2 years, 1 month ago by  aaclayton.
    • This reply was modified 2 years, 1 month ago by  aaclayton.
    • This reply was modified 2 years, 1 month ago by  aaclayton.

    aaclayton
    Participant

    @aaclayton

    Ok….so clearly I can’t effectively display what is actually getting put in my header section without the forum thinking I’m embedding live html or something. Just to clarify, bbpress is enqueing a stylesheet, bbp-child-bbpress-css from my theme’s /css directory regardless of the site page in question. Furthermore, it’s adding a script to the header defining a js variable:

    var ajaxurl = 'http://localhost/tamrielfoundry/wp-admin/admin-ajax.php';
    

    which I’m not sure is necessary?


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    Jared was actually incorrect. :)

    bbPress actions are piggy-backed on top of WordPress actions, there’s no condition to them.

    1. If you don’t want bbPress to enqueue it’s own CSS, dequeue it, or make sure your theme has /css/bbpress.css in it.
    2. bbPress uses admin-ajax.php for a few ajax bits that happen theme-side. They degrade properly, so if you do not want to include the ajax bits, you can safely unhook that too.

    aaclayton
    Participant

    @aaclayton

    Hey JJJ, thanks for the advice. I am using /css/bbpress.css in my theme, the trouble I’m having is actually getting the proper syntax for unhooking these elements when they aren’t needed.

    Essentially, what I’m trying to do is something like this:

    add_action( 'get_header', 'remove_bbpress_scripts' );
    function remove_bbpress_scripts() {
        if ( !is_bbpress() ) {
            remove_action ( 'bbp_enqueue_scripts' , 'enqueue_styles' );
            remove_action ( 'bbp_enqueue_scripts' , 'enqueue_scripts' );
        }
    }
    

    Something isn’t working with this syntax though, I suspect I need to prefix ‘enqueue_styles’ with something, but I’m not sure what the proper prefix is.


    aaclayton
    Participant

    @aaclayton

    I am still having this issue, unfortunately. Although I worked out enough of the technical details of bbPress 2.1 to port the plugin/templates/css over to the live (development) site.

    If it would be helpful to see what I’m talking about you can access http://tamrielfoundry.com with username: test, password: test.

    The bbpress CSS + some other scripts are being loaded in the head site-wide, and I can’t figure out the correct conditional syntax to remove them unless is_bbpress() is true.


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    There are a few tags you could use, but the normal bbPress CSS isn’t written to be broken up and conditionally loaded; you’ll need to do that yourself. They are conveniently called is functions:

    http://bbpress.trac.wordpress.org/browser/branches/plugin/bbp-includes/bbp-common-template.php#L91


    aaclayton
    Participant

    @aaclayton

    Sorry, I guess I’m not being very clear. The is_bbpress() function already seems to deliver the conditional logic I need. That (I don’t think) isn’t my problem. My problem is getting the proper syntax for removing the actions that add styles and scripts to the head area.

    remove_action ( 'bbp_enqueue_scripts' , 'enqueue_styles' );
    remove_action ( 'bbp_enqueue_scripts' , 'enqueue_scripts' );
    

    This doesn’t work, nor do some variants that I have tried. Could you suggest the proper syntax for removing the relevant actions, regardless of the conditioning logic employed?


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    Since those actions happen inside of the BBP_Admin class, you’ll need to hook a function on to the ‘bbp_theme_compat_actions’ action, and remove them from there.

    function jjj_blah( $admin ) {
        remove_action( 'bbp_enqueue_scripts', array( $admin, 'enqueue_styles'  ) );
        remove_action( 'bbp_enqueue_scripts', array( $admin, 'enqueue_scripts' ) );
    }
    add_action( 'bbp_theme_compat_actions', 'jjj_blah' );
    

    aaclayton
    Participant

    @aaclayton

    Thanks a bunch for sharing this snippet. It works perfectly when not conditioned. It’s not behaving nicely with the is_bbpress() conditional logic, so I’ll have to delve a bit deeper into the set of is_ functions you mentioned above, but I should be able to work something out. Thanks again for the help, and for the great forum software.


    alexis.nomine
    Participant

    @alexisnomine

    As is_bbpress() doesn’t work within the bbp_theme_compat_actions hook, the solution would be to conditionnaly remove bbp_head and bbp_enqueue_scripts. It’s a bit more extreme but it works:

    `add_action( ‘wp_head’, ‘conditional_bbpress_head’, 9 );
    function conditional_bbpress_head(){
    if( !is_bbpress() )
    remove_action( ‘wp_head’, ‘bbp_head’ );
    }
    add_action( ‘wp_enqueue_scripts’, ‘conditional_bbpress_scripts’, 9 );
    function conditional_bbpress_scripts(){
    if( !is_bbpress() )
    remove_action( ‘wp_enqueue_scripts’, ‘bbp_enqueue_scripts’ );
    }`

Viewing 11 replies - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.