Show "Become an Affiliate" or "Affiliate Area" Menu Link (AffiliateWP)
Adds a menu item to a theme menu location that reads "Affiliate Area" for affiliates and "Become an affiliate" for everyone else, both pointing to your AffiliateWP page.
Opens WPIDE on
Code
$wpide_location = 'primary'; // Theme menu location.
add_filter( 'wp_nav_menu_items', function ( $items, $args ) use ( $wpide_location ) {
if ( ! function_exists( 'affiliate_wp' ) || empty( $args->theme_location ) || $args->theme_location !== $wpide_location ) {
return $items;
}
$page_id = (int) affiliate_wp()->settings->get( 'affiliates_page' );
if ( ! $page_id ) {
return $items;
}
$is_affiliate = is_user_logged_in() && affwp_get_affiliate_id( get_current_user_id() );
$label = $is_affiliate ? __( 'Affiliate Area' ) : __( 'Become an affiliate' );
return $items . sprintf( '<li class="menu-item menu-item-affiliate"><a href="%1$s">%2$s</a></li>', esc_url( get_permalink( $page_id ) ), esc_html( $label ) );
}, 10, 2 );
Notes
Set $wpide_location to your theme's menu location.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.