Home / Admin / disable OptinMonster campaigns for visitors with specific user roles
Duplicate Snippet

Embed Snippet on Your Site

disable OptinMonster campaigns for visitors with specific user roles

Code Preview
php
<?php
function om_docs_hide_optin_for_user_roles( $campaigns ) {
    // Do nothing if no user is logged in.
    if ( ! is_user_logged_in() ) {
	return $campaigns;
    }
    // Change these as necessary.
    $roles_to_check = array( 'administrator', 'subscriber' );
    // Get the current user.
    $user = wp_get_current_user();
    if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
        $has_role = array_intersect( $roles_to_check, $user->roles );
	if ( ! empty( $has_role ) ) {
		// Remove all campaigns for these particular roles.
		$campaigns = array();
	}
    }
    // Return the potentially modified campaigns.
    return $campaigns;
    }
add_filter( 'optin_monster_api_final_output', 'om_docs_hide_optin_for_user_roles' );

Comments

Add a Comment