Skip to:
Content
Pages
Categories
Search
Top
Bottom

jquery loading


  • yoyopop
    Member

    @yoyopop

    I wanted to use a simple jquery script that I could implement around the forum when I wanted to toggle the visibility of sections of text (eg: hide and show some help information). I assumed I could use this wherever I wanted but it seems that bbpress only loads jquery on certain pages. It loads up for topics but not on pages such as login.php (where I was hoping to use said script).

    Can anyone shed some light on where and when and why jquery is loaded? And how could I get it to load up for the login.php page as well? (rather than simply putting the jquery call in header.php, which would mean it gets called twice for some pages!)

    I looked through a load of bb-includes but didn’t get too far!

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

  • psycheangels
    Member

    @psycheangels

    you can use bb_get_location()

    like

    if ( bb_get_location() == ‘login-page’ {

    your jquery script

    }


    yoyopop
    Member

    @yoyopop

    Maybe I wasn’t clear – basically I have a script that I wrote myself that I have put in header.php so that it gets loaded on every page. However, this script needs the jquery script to load as well or it won’t work. BBPress has jquery built-in but it only loads it on certain pages. I wanted to see what defines where the jquery script that is built into BBpress is called…

    But I can use this technique to do the same thing, just the opposite way around, so thanks for the help! I did it like this:

    <?php if ( in_array( bb_get_location(), array( 'login-page', 'register-page' ) ) ) echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>n"; ?>

    I tried to use bb_enqueue_script( 'jquery' ); in there but couldn’t get that to work!


    psycheangels
    Member

    @psycheangels

    <?php bb_enqueue_script(‘jquery’); ?>


    chrishajer
    Participant

    @chrishajer

    > I tried to use bb_enqueue_script( ‘jquery’ ); in there but couldn’t get that to work!

    You *need* to get it to work, that’s the proper way of doing it. You should figure out what you were doing wrong.


    zaerl
    Participant

    @zaerl

    Check this micro-plugin. It loads jquery in every page.

    <?php
    /*
    Plugin Name: jQuery please
    Plugin URI: http://www.the.url.com
    Description: I really need jQuery
    Version: 1.0
    Author: Me
    Author URI: http://www.the.url.com
    */

    add_action('bb_init', 'jquery_please_initialize');
    add_action('bb_head', 'jquery_please_js', 100);

    function jquery_please_initialize()
    {
    bb_enqueue_script('jquery');
    }

    function jquery_please_js()
    { ?>
    <script type="text/javascript">//<![CDATA[
    // Your script goes here
    //]]></script>
    <?php
    }

    ?>


    chrishajer
    Participant

    @chrishajer

    awesome :-)


    zaerl
    Participant

    @zaerl

    You put on the “jQuery please” tag? :-)


    yoyopop
    Member

    @yoyopop

    thanks for the input guys. The only thing I was worried about was it would be loading jquery on every page, even when it won’t be used – or do you think it’s not a large enough file to worry about?


    zaerl
    Participant

    @zaerl

    It’s not a big deal. You can change the plugin in order to prevent the inclusion of jQuery on those pages that you know that will not need the special treatment. Change line:

    add_action('bb_init', 'jquery_please_initialize');

    with:

    // Add here all the template files that need special treatment
    add_action('bb_topic.php', 'jquery_please_initialize');
    add_action('bb_edit-post.php', 'jquery_please_initialize');
    // et cetera


    yoyopop
    Member

    @yoyopop

    cool, thanks zaerl – I had it working before but your solution is much more elegant!


    mr_pelle
    Participant

    @mr_pelle

    May I suggest another solution? bb_enqueue_script() is deprecated, you should first check if script is already included using wp_script_is() and then call wp_enqueue_script().

    add_action( 'bb_init', 'enqueueJQuery' );

    function enqueueJQuery() {
    if ( !wp_script_is( 'jquery' ) )
    wp_enqueue_script( 'jquery' );
    }

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar