An affiliate may have access to the WordPress admin. The following code snippet will show you how to add an Affiliate Area link to the WordPress admin bar. When an affiliate clicks this link, they are taken to the front-end Affiliate Area provided by AffiliateWP.

<?php

/**
 * Add an "Affiliate Area" link to the WordPress admin bar menu.
 * 
 * @link https://andrew.dev/snippets/add-an-affiliate-area-link-to-the-wordpress-admin-bar-menu/
 */
function am_affwp_admin_bar_affiliate_area_link( $wp_admin_bar ) {

	// Bail if AffiliateWP is not active.
	if ( ! function_exists( 'affiliate_wp' ) ) {
		return;
	}

	// Get the current user ID.
	$user_id = get_current_user_id();

	// Return if the user is not an affiliate, or an active affiliate.
	if ( ! ( affwp_is_affiliate( $user_id ) && affwp_is_active_affiliate( affwp_get_affiliate_id( $user_id ) ) ) ) {
		return;
	}

	$args = array(
		'id'    => 'affiliate_area',
		'title' => 'Affiliate Area',
		'href'  => affwp_get_affiliate_area_page_url()
	);

	// Add the node to the admin bar menu.
	$wp_admin_bar->add_node( $args );

}
add_action( 'admin_bar_menu', 'am_affwp_admin_bar_affiliate_area_link', 9999 );

Leave a Reply

Your email address will not be published. Required fields are marked *