Created
January 24, 2023 14:35
-
-
Save christianwach/931bdf52803a35e363043ad0302fd4be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Remove CiviCRM Base Page from Menu | |
* Plugin URI: https://gist.github.com/christianwach/931bdf52803a35e363043ad0302fd4be | |
* Description: Removes the CiviCRM Base Page from a front-end menu. | |
* Author: Christian Wach | |
* Version: 1.0 | |
* Author URI: https://haystack.co.uk | |
*/ | |
// Exit if accessed directly. | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Remove CiviCRM Base Page from Menu class. | |
* | |
* @since 1.0 | |
*/ | |
class Remove_CiviCRM_Base_Page_From_Menu { | |
/** | |
* Constructor. | |
* | |
* @since 1.0 | |
*/ | |
public function __construct() { | |
// Filter menu. | |
add_action( 'wp_nav_menu_objects', [ $this, 'filter_menu' ], 20, 2 ); | |
} | |
/** | |
* Filter the main menu on the root site. | |
* | |
* @since 1.0 | |
* | |
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. | |
* @param array $args Array of wp_nav_menu() arguments. | |
* @return $sorted_menu_items The filtered menu items. | |
*/ | |
public function filter_menu( $sorted_menu_items, $args ) { | |
// Only on front end. | |
if ( is_admin() ) { | |
return $sorted_menu_items; | |
} | |
// Bail if there's no CiviCRM. | |
if ( ! function_exists( 'civi_wp' ) ) { | |
return $sorted_menu_items; | |
} | |
// Bail if there's no CiviCRM base page. | |
$basepage = civi_wp()->basepage->basepage_get(); | |
if ( ! ( $basepage instanceof WP_Post ) ) { | |
return $sorted_menu_items; | |
} | |
// Remove item from array. | |
$this->remove_item( $sorted_menu_items, 'post_type', get_permalink( $basepage->ID ) ); | |
// --< | |
return $sorted_menu_items; | |
} | |
/** | |
* Filters the menu array to exclude a matched item. | |
* | |
* @since 1.0 | |
* | |
* @param array $sorted_menu_items The menu items, sorted by each menu item's menu order. | |
* @param str $type The type of menu item we're looking for. | |
* @param array $url The URL we're looking for in the menu item's target URL. | |
*/ | |
private function remove_item( &$sorted_menu_items, $type, $url ) { | |
// Loop through the menu and get the menu item's key. | |
foreach ( $sorted_menu_items as $key => $item ) { | |
if ( $item->type === $type && $item->url === $url ) { | |
$found = $key; | |
break; | |
} | |
} | |
// Remove it if we find it. | |
if ( isset( $found ) ) { | |
unset( $sorted_menu_items[ $found ] ); | |
} | |
} | |
} | |
/** | |
* Utility to get a reference to this plugin. | |
* | |
* @since 1.0 | |
* | |
* @return object $plugin The plugin reference. | |
*/ | |
function remove_civicrm_base_page_from_menu() { | |
// Return instance. | |
static $plugin; | |
if ( ! isset( $plugin ) ) { | |
$plugin = new Remove_CiviCRM_Base_Page_From_Menu(); | |
} | |
return $plugin; | |
} | |
// Instantiate the class. | |
remove_civicrm_base_page_from_menu(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment