bp_core_register_common_scripts()

Register scripts commonly used by BuddyPress.

Description

Source

File: bp-core/bp-core-cssjs.php

function bp_core_register_common_scripts() {
	$min = bp_core_get_minified_asset_suffix();
	$url = buddypress()->plugin_url . 'bp-core/js/';

	/*
	 * Moment.js locale.
	 *
	 * Try to map current WordPress locale to a moment.js locale file for loading.
	 *
	 * eg. French (France) locale for WP is fr_FR. Here, we try to find fr-fr.js
	 *     (this file doesn't exist).
	 */
	$locale = sanitize_file_name( strtolower( get_locale() ) );
	$locale = str_replace( '_', '-', $locale );
	if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
		$moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";

	/*
	 * Try to find the short-form locale.
	 *
	 * eg. French (France) locale for WP is fr_FR. Here, we try to find fr.js
	 *     (this exists).
	 */
	} else {
		$locale = substr( $locale, 0, strpos( $locale, '-' ) );
		if ( file_exists( buddypress()->core->path . "bp-core/js/vendor/moment-js/locale/{$locale}{$min}.js" ) ) {
			$moment_locale_url = $url . "vendor/moment-js/locale/{$locale}{$min}.js";
		}
	}

	// Set up default scripts to register.
	$scripts = array(
		// Legacy.
		'bp-confirm'        => array( 'file' => "{$url}confirm{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),
		'bp-widget-members' => array( 'file' => "{$url}widget-members{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),
		'bp-jquery-query'   => array( 'file' => "{$url}jquery-query{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),
		'bp-jquery-cookie'  => array( 'file' => "{$url}vendor/jquery-cookie{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),
		'bp-jquery-scroll-to' => array( 'file' => "{$url}vendor/jquery-scroll-to{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => false ),

		// Version 2.1.
		'jquery-caret' => array( 'file' => "{$url}vendor/jquery.caret{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => true ),
		'jquery-atwho' => array( 'file' => "{$url}vendor/jquery.atwho{$min}.js", 'dependencies' => array( 'jquery', 'jquery-caret' ), 'footer' => true ),

		// Version 2.3.
		'bp-plupload' => array( 'file' => "{$url}bp-plupload{$min}.js", 'dependencies' => array( 'plupload', 'jquery', 'json2', 'wp-backbone' ), 'footer' => true ),
		'bp-avatar'   => array( 'file' => "{$url}avatar{$min}.js", 'dependencies' => array( 'jcrop' ), 'footer' => true ),
		'bp-webcam'   => array( 'file' => "{$url}webcam{$min}.js", 'dependencies' => array( 'bp-avatar' ), 'footer' => true ),

		// Version 2.4.
		'bp-cover-image' => array( 'file' => "{$url}cover-image{$min}.js", 'dependencies' => array(), 'footer' => true ),

		// Version 2.7.
		'bp-moment'    => array( 'file' => "{$url}vendor/moment-js/moment{$min}.js", 'dependencies' => array(), 'footer' => true ),
		'bp-livestamp' => array( 'file' => "{$url}vendor/livestamp{$min}.js", 'dependencies' => array( 'jquery', 'bp-moment' ), 'footer' => true ),

		// Version 3.1.1
		'bp-jquery-validate' => array( 'file' => "{$url}vendor/jquery.validate{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => true ),
        'jquery-mask'        => array( 'file' => "{$url}vendor/jquery.mask{$min}.js", 'dependencies' => array( 'jquery' ), 'footer' => true ),

        'giphy'        => array( 'file' => "{$url}vendor/giphy{$min}.js", 'dependencies' => array(), 'footer' => true ),

		'emojione'     => array( 'file' => "{$url}emojione-edited.js", 'dependencies' => array(), 'footer' => true ),
		'emojionearea' => array( 'file' => "{$url}emojionearea-edited.js", 'dependencies' => array( 'emojione' ), 'footer' => true ),

        'bp-medium-editor'        => array( 'file' => "{$url}vendor/medium-editor{$min}.js", 'dependencies' => array(), 'footer' => false ),

		'isInViewport'        => array( 'file' => "{$url}vendor/isInViewport{$min}.js", 'dependencies' => array(), 'footer' => true ),
		'bp-tagify'        => array( 'file' => "{$url}vendor/tagify{$min}.js", 'dependencies' => array(), 'footer' => false ),

	);

	// Version 2.7 - Add Moment.js locale to our $scripts array if we found one.
	if ( isset( $moment_locale_url ) ) {
		$scripts['bp-moment-locale'] = array( 'file' => esc_url( $moment_locale_url ), 'dependencies' => array( 'bp-moment' ), 'footer' => true );
	}

	/**
	 * Filters the BuddyBoss Core javascript files to register.
	 *
	 * Default handles include 'bp-confirm', 'bp-widget-members',
	 * 'bp-jquery-query', 'bp-jquery-cookie', and 'bp-jquery-scroll-to'.
	 *
	 * @since BuddyPress 2.1.0 'jquery-caret', 'jquery-atwho' added.
	 * @since BuddyPress 2.3.0 'bp-plupload', 'bp-avatar', 'bp-webcam' added.
	 * @since BuddyPress 2.4.0 'bp-cover-image' added.
	 * @since BuddyPress 2.7.0 'bp-moment', 'bp-livestamp' added.
	 *              'bp-moment-locale' is added conditionally if a moment.js locale file is found.
	 *
	 * @param array $value Array of javascript file information to register.
	 */
	$scripts = apply_filters( 'bp_core_register_common_scripts', $scripts );


	$version = bp_get_version();
	foreach ( $scripts as $id => $script ) {
		wp_register_script( $id, $script['file'], $script['dependencies'], $version, $script['footer'] );
	}
}

Changelog

Changelog
Version Description
BuddyPress 2.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.