How To Add Custom Taxonomies to Advanced Sermons #
Summary
In this online documentation, we’ll guide you on how to add custom taxonomies to Advanced Sermons. The method has undergone changes with the introduction of Advanced Sermons 3.0. This guide is intended to help those who had created custom taxonomies prior to the release of Advanced Sermons 3.0 and want to integrate them into the updated version.
Option 1. Download & Install Advanced Sermons Taxonomy Addons
For a streamlined experience, we’ve crafted custom taxonomy plugins that are ready for download and installation on your WordPress site. Once activated, no further steps are required. The new taxonomy will be visible under the dashboard menu item ‘Sermons’. Assign a sermon to the taxonomy, and it will be displayed on the frontend in the filter bar, grid view, list view, and individual sermon template. To ensure the proper functioning of these addons, please ensure that you’ve updated Advanced Sermons to version 3.0.
Campus Addon | 1.1Service Type Addon | 1.1
Option 2. Add and Customize Your Own Code
For developers who prefer a hands-on approach to integrating customizations, use the code provided below. Add this to your own plugin or the functions.php file of your child theme. This code works seamlessly with all the built-in hooks available in Advanced Sermons.
// Create custom sermon campus taxonomy
add_action('init', 'asp_register_taxes_campus');
function asp_register_taxes_campus() {
// Get Advanced Sermons global variables
global $asp_archive_slug;
$labels = array(
"name" => __("Campuses", "advanced-sermons"),
"singular_name" => __("Campuses", "advanced-sermons"),
"menu_name" => __("Campuses", "advanced-sermons"),
"search_items" => __("Search Campuses", "advanced-sermons"),
"popular_items" => __("Most popular Campuses", "advanced-sermons"),
"all_items" => __("All Campuses", "advanced-sermons"),
"edit_item" => __("Edit Campuses", "advanced-sermons"),
"update_item" => __("Update Campuses", "advanced-sermons"),
"add_new" => __("Add New Campuses", "advanced-sermons"),
"add_new_item" => __("Add New Campuses", "advanced-sermons"),
'view_item' => __("View Campuses", "advanced-sermons"),
'parent_item' => null,
'parent_item_colon' => null,
);
$args = array(
"label" => __("Campuses", "advanced-sermons"),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => false,
"query_var" => true,
"has_archive" => true,
"rewrite" => array("slug" => "/$asp_archive_slug/", "with_front" => false),
"show_admin_column" => true,
"show_in_rest" => false,
"show_in_quick_edit" => true,
);
register_taxonomy("sermon_campus", array("sermons"), $args);
}
// Archive Filter Dropdown Campus. Utilizing Hook from documentation.
add_action( 'asp_hook_filter_bar_fields', 'asp_archive_filter_campus', 110 );
function asp_archive_filter_campus() {
$taxonomies = array( 'sermon_campus' );
$args = array( "orderby" => "name", "hide_empty" => true, "order" => "ASC" );
asp_filter_terms_dropdown_campus($taxonomies, $args);
}
// Dynamically populate campus data into sermon archive filter
function asp_filter_terms_dropdown_campus($taxonomies, $args) {
$asp_archive_filter_sermon_count = get_option('asp_archive_filter_sermon_count');
$asp_campus_terms = get_terms($taxonomies, $args);
echo '<div class="sermon-field-container topic-container">';
echo "<select name='sermon_campus' class='asp-filter-topic'>";
echo '<option value="">' . __("All Campuses", "advanced-sermons") . '</option>';
foreach ($asp_campus_terms as $term) {
$term_slug = $term->slug;
$term_name = $term->name;
if (empty($asp_archive_filter_sermon_count)) {
$term_count = "($term->count)";
} else {
$term_count = null;
}
$link = $term_slug;
if (!empty($_REQUEST['sermon_campus'])) {
echo "<option " . selected($_REQUEST['sermon_campus'],
$link) . " value='$link'>$term_name $term_count</option>";
} else {
echo "<option value='$link'>$term_name $term_count</option>";
}
}
echo "</select>";
echo "</div>";
}
// Front page sort by menu_order for archive page
add_filter( 'asp_allowed_taxonomies_filter', function( $taxonomies ) {
$taxonomies[] = 'sermon_campus';
return $taxonomies;
} );
// Checks if the campus taxonomy is selected in the filter
add_filter('asp_filter_selected_taxonomy', 'asp_is_campus_selected');
function asp_is_campus_selected($asp_filter_selected_taxonomy) {
$selected_campus = $_REQUEST['sermon_campus'] ?? '';
// If any other criteria is selected or if the campus is selected, return true.
return $asp_filter_selected_taxonomy || !empty($selected_campus);
}
// Add selected campus to criteria box results
add_filter('asp_custom_filter_criteria', 'asp_add_campus_filter_criteria');
function asp_add_campus_filter_criteria($html) {
$sermon_campus = $_REQUEST['sermon_campus'] ?? '';
if (!empty($sermon_campus)) {
$selected_campus_slug = get_term_by('slug', "$sermon_campus", 'sermon_campus');
$selected_campus_name = $selected_campus_slug->name;
$html .= "<p class='asp-selected-campus'>" . __("Campus", "advanced-sermons") . ": " . __("$selected_campus_name", "advanced-sermons") . "</p>";
}
return $html;
}
// Add campus taxonomy data to sermon details on grid view, list view, and single sermon templates
function asp_sermon_header_details_campus() {
global $post, $asp_archive_slug;
$asp_sermon_campus = wp_get_post_terms($post->ID, 'sermon_campus', array("fields" => "names"));
$asp_sermon_campus_slug = wp_get_post_terms($post->ID, 'sermon_campus', array("fields" => "slugs"));
if (isset($asp_sermon_campus[0])) { ?>
<div class='sermon-campus'><p>
<?php _e('Campus', 'advanced-sermons'); ?>:
<?php
$count = count($asp_sermon_campus);
$i = 0;
foreach ($asp_sermon_campus as $index => $campus) {
$i++;
if (get_option('permalink_structure')) {
echo "<a href='" . get_home_url() . "/" . $asp_archive_slug . "/?sermon_campus=" . $asp_sermon_campus_slug[$index] . "'>" . $campus . "</a>";
} else {
echo "<a href='" . get_home_url() . "/?post_type=sermons&sermon_campus=" . $asp_sermon_campus_slug[$index] . "'>" . $campus . "</a>";
}
if ($i < $count) {
echo ", ";
}
}
?>
</p></div>
<?php }
}
add_action('asp_hook_sermon_archive_header_details', 'asp_sermon_header_details_campus');
add_action('asp_hook_sermon_single_header_details', 'asp_sermon_header_details_campus');