0

I wrote this code in my functions file to automatically add page links to the nav menu if a plugin is activated. It works perfectly on my test server but pages don't load any content below the header on a live multisite installation.

add_filter('wp_nav_menu_items','add_about_page_to_menu', 10, 2);
function add_about_page_to_menu( $items, $args ) {

$url = home_url( 'about', 'relative' );
if( $args->theme_location == 'primary' )
    if ( is_plugin_active( 'about-page-extension/about-page-extension.php' ) 
) {
    return $items."<li><a href='$url'>About</a></li>";
}
return $items;
}

The main culprit seems to be if ( is_plugin_active( 'about-page-extension/about-page-extension.php' ) as the same issue isn't apparent when I change the code to exclude that part, although non functional.

Does anyone know how I can fix this?

1 Answer 1

1

Turns out is_plugin_active is defined in wp-admin/includes/plugin.php so it's only available within the admin unless I call it.

I needed to add include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); so my final code was as shown below which I also tidied up a bit too.

add_filter('wp_nav_menu_items','add_about_page_to_menu', 10, 2);
function add_about_page_to_menu( $items, $args ) {

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$url = home_url( 'about', 'relative' );
if( $args->theme_location == 'primary' && is_plugin_active( 'about-page-extension/about-page-extension.php' )) {
    return $items."<li><a href='$url'>About</a></li>";
}
return $items;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.