Hide widget or shortcode for subscribed users

Is is possible to hide e-newsletter widget or shortcode after users subscribed to it?

  • Nastia

    Hello there us-Sunnah !

    Hope all is well!

    It is not possible out of the box and will require custom coding. It will be easier to achieve if a user is registered on a site. If a user is not logged in, cookies need to be used.

    I've asked our second level support developers to have a closer look at this and provide you some feedback. Please note, our developers' response time is longer-than-normal compared to support staff response times.

    Have a nice day and take care!

    Cheers,
    Nastia

  • Panos

    Hey there us-Sunnah ,

    Of what I see there is no option to hide the widget if user already subscribed but there are some hooks which should help :slight_smile:

    Could you please add the following snippet in your child theme's functions.php or a mu-plugin:

    add_action( 'wp_login', 'wpmudev_reset_cookie_enewsletter_subscribed', 10, 2 );
    add_action( 'wp_logout', 'wpmudev_rm_cookie_enewsletter_subscribed', 10 );
    
    function wpmudev_reset_cookie_enewsletter_subscribed( $user_login, $user ){
    
    	wpmudev_rm_cookie_enewsletter_subscribed();
    
    	global $wpdb;
    	$enewslatter_members_table = "{$wpdb->prefix}enewsletter_members";
    	$member_meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$enewslatter_members_table} WHERE wp_user_id=%d", $user->ID ) );
    
    	if( isset( $member_meta->unsubscribe_code ) && $member_meta->unsubscribe_code != '' ){
    		setcookie( 'wpmudev_enewsletter_subscribed', 'YES', 30 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
    	}
    
    }
    
    function wpmudev_rm_cookie_enewsletter_subscribed(){
    
    	unset( $_COOKIE[ 'wpmudev_enewsletter_subscribed' ] );
      	setcookie( 'wpmudev_enewsletter_subscribed', '', time() - ( 15 * 60 ) );
    
    }
    
    add_action( 'enewsletter_user_subscribe', 'wpmudev_enewsletter_loggedin_user_subscribe', 10, 1 );
    
    function wpmudev_enewsletter_loggedin_user_subscribe( $member_id ){
    
    	setcookie( 'wpmudev_enewsletter_subscribed', 'YES', 30 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
    
    }
    
    add_action( 'enewsletter_after_user_add', function( $member_id ){
    	setcookie( 'wpmudev_enewsletter_subscribed', 'YES', 1 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
    
    }, 10 );
    
    add_action( 'wp_head', function(){
    
    	if( ! isset( $_COOKIE[ 'wpmudev_enewsletter_subscribed' ] ) || $_COOKIE[ 'wpmudev_enewsletter_subscribed' ] != 'YES' ) {
    	  return;
    	}
    
    	?>
    	<style>
    	.widget_e_newsletter_subscribe{
    		display: none;
    	}
    	</style>
    
    	<?php
    
    }, 10 );

    and let us know if it helps?

    Thanks!

  • Panos

    Hi us-Sunnah ,

    Sorry to hear it's not working for you.

    You are talking about the mopa subdomain right? I tested by subscribing to newsletter and both were hidden after refreshing page. It needs one small addition for that specific subsite to hide the "SUBSCRIBE TO OUR NEWSLETTER" title in the footer. For this you can change:

    .widget_e_newsletter_subscribe{
    		display: none;
    	}

    to

    .widget_e_newsletter_subscribe,
    #row-1164432217{
    	display: none;
    }

    In the above snippet?

    Regarding:

    and in the sidebar widgets of the blog page, etc.

    I checked on the mopa subdomain and the widget was hidden as I mentioned. Could you please let me know if I should be looking elsewhere?

    Thanks!

  • us-Sunnah

    The solution above was always hiding the widget. I want to show it to people who can still subscribe. Adding this to the bottom of my home page worked for me:

    <script>if (document.getElementById('manage_subscriptions').offsetParent !== null) { document.getElementById('manage_subscriptions').parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = "none"; }</script>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.