Skip to content

Wordpress functions.php File

The functions.php file is the place that any additional functions (or overwrites of core Wordpress functionality) should be placed. To keep the file from becoming a giant mess of random code, we've grouped common functionality into various files and included them into the functions.php file. You should see a list of require_once statements at the top of the functions.php file where the files are being included.

Note

If you need to add new functions, do not add them to the main functions.php file directly. Instead, add them to the appropriate included file (or create a new include if there is not one that is a good fit for your function(s) )

Include files

The following is a basic breakdown of what each of the files included into the functions.php file contain. We will try to keep this list up to date, but if you're unsure, just consult the actual files as they should have fairly descriptive comments in the files themselves.

assets.php

This file is responsible for ensuring that the proper static assets (such as Javascript and CSS files) are included into the site. The script will automatically look for production assets and inject them into the page. Note that the script requires very specific naming conventions for asset files. These conventions should automatically be handled by Webpack as long as you're properly adding / updating files. Consult the CSS and JS (Webpack) page for more info.

Not surprisingly, this file is responsible for generating the breadcrumbs for the current page.

excerpt.php

This file modifies the default Wordpress functions responsible for printing a page/post excerpt and the Read More link generated when the excerpt is auto-trimmed.

This file registers menus. Including a menu on the page is a three-part process: 1. Register the menu (done in this file) 2. Include the registered menu somewhere in a template file using the wp_nav_menu function 3. Managing the content of the menu in the Admin Panel More info on the Menus page.

In addition to registering menus, this file also contains Menu Walker functions which are referenced when rendering a menu with wp_nav_menu. The Menu Walkers are responsible for actually rendering the HTML of the menus.

register.php

Includes logic to handle registration of users and automatic inclusion into specific groups (for when people register for AAC By the Bay).

shortcodes.php

Includes all the code to enable the various custom shortcodes created for the site. Visit the Shortcodes page or look to the comments in shortcodes.php for guidance on usage.

sidebars.php

Registers sidebar areas for later inclusion. Rendering sidebars has the same three steps as rendering menus: 1. Register the sidebar (in this file). 2. Include the sidebar in one or more template files using the Wordpress dynamic_sidebar() function. 3. Manage the sidebar via the Admin Panel.

utils.php

Contains various utility functions used throughout the site. In particular, there are two very important functions that occur here:

  1. The bs_set_site() function is responsible for determining the current site (www, cvi, etc). The function takes into account the environment, meaning that it will correctly identify the current site on bridgeschool.org and bridgeschool2.org as well as in your local development environment (as long as you followed the setup and naming conventions outlined in the Local Environment.

  2. The bs_get_url_tokens() function parses the current URL to determine the various parts of the path (again, taking into account the MAMP folder in local dev). This function is then used to do things like build links, or add CSS classes to elements (e.g. for theming the different sections of the main site like about)

Warning

The utils.php file sets several PHP constants, like SITE_SLUG. These constants should never be accessed directly in other parts of the code (such as other functions or templates). Instead, a getter function should be created (such as bs_get_site()). This allows consistent access to the constant while allowing for a single code point for future updates to the constant or the logic around it.