WordPress.org

Forums

Subscribe2
One-click subscription / unsubscription on separate page (9 posts)

  1. snowbeachking
    Member
    Posted 8 months ago #

    Hi,

    First of all thank you very much for this great plugin. It exactly does that what I need.

    I am running a small private site for our family and I only need to provide the "One Click Subscription / Unsubscription" buttons from the Your Subscription backend on a separate page within the frontend.

    I found the relevant code from your_subscriptions.php and created something like this:

    <h2><?php _e('One Click Subscription / Unsubscription', 'subscribe2') ?></h2>
    <p class="submit"><input type="submit" class="button-primary" name="subscribe" value=" <?php _e("Subscribe to All", 'subscribe2') ?>" />  
    <input type="submit" class="button-primary" name="unsubscribe" value="<?php _e("Unsubscribe from All", 'subscribe2') ?>" /></p>

    But the buttons do not initiate any action. What am I missing?

    Thanks for your help in advance.

    Kind regards.

    https://wordpress.org/plugins/subscribe2/

  2. snowbeachking
    Member
    Posted 8 months ago #

    Tried this one directly in your_subscriptions.php, as I prefer to use a short code - e.g. [one_click_subscription]:

    if ( $this->subscribe2_options['one_click_profile'] == 'yes' ) {
    		function one_click_subscription( $atts ) {
    			// One-click subscribe and unsubscribe buttons
    			echo "<h2>" . __('One Click Subscription / Unsubscription', 'subscribe2') . "</h2>\r\n";
    			echo "<p class=\"submit\"><input type=\"submit\" class=\"button-primary\" name=\"subscribe\" value=\"" . __("Subscribe to All", 'subscribe2') . "\" />&nbsp;&nbsp;";
    			echo "<input type=\"submit\" class=\"button-primary\" name=\"unsubscribe\" value=\"" . __("Unsubscribe from All", 'subscribe2') . "\" /></p>";
    			}
    			add_shortcode( 'one_click_subscription', 'one_click_subscription' );
    		}

    Unfortunately it doesn't work. Any help is much appreciated.

  3. mattyrob
    Member
    Posted 8 months ago #

    @snowbeachking,

    It won't work because the 'handler' code that processes the clicks is only loaded on the admin side in the class-s2-core.php class file. You'd need to either add in that code when adding the button to the front side of your site or edit the core class file to achieve the same.

  4. snowbeachking
    Member
    Posted 8 months ago #

    Hi Matty,

    Thank you for the hint. I put the following to my functions.php:

    /**
    Subscribe/unsubscribe user from one-click submission
    */
    function one_click_handler($user_ID, $action) {
    	if ( !isset($user_ID) || !isset($action) ) { return; }
    		$all_cats = $this->all_cats(true);
    		if ( 'subscribe' == $action ) {
    		// Subscribe
    		$new_cats = array();
    		foreach ( $all_cats as $cat ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id);
    			$new_cats[] = $cat->term_id;
    		}
    
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(',', $new_cats));
    
    		if ( 'yes' == $this->subscribe2_options['show_autosub'] && 'no' != get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true) ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'yes');
    		}
    	} elseif ( 'unsubscribe' == $action ) {
    		// Unsubscribe
    		foreach ( $all_cats as $cat ) {
    			delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id);
    		}
    
    		delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'));
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'no');
    	}
    } //end one_click_handler()
    
    function one_click_subscription( $content ) {
    	// One-click subscribe and unsubscribe buttons
    	echo "<h2>" . __('One Click Subscription / Unsubscription', 'subscribe2') . "</h2>\r\n";
    	echo "<span class=\"submit\"><input type=\"submit\" class=\"button-primary\" name=\"subscribe\" value=\"" . __("Subscribe to All", 'subscribe2') . "\" />  ";
    	echo "<input type=\"submit\" class=\"button-primary\" name=\"unsubscribe\" value=\"" . __("Unsubscribe from All", 'subscribe2') . "\" /></span>";
    	}
    	add_shortcode( 'one_click_subscription', 'one_click_subscription' );

    But still no success, which means that the clicks do not work. Just fyi. I am pretty new to all this and not a coder, but am willing to learn and have a very basic understanding of coding.

    Thanks and kind regards.

  5. mattyrob
    Member
    Posted 8 months ago #

    @snowbeachking,

    Things I immediately notice:

    1/ The one_click_handler() function is being declared but isn't beeing hooked into WordPress anywhere with an add_action(); call.
    2/ The one_click_subscription() function appears to be attempting to add the buttons to your content on all posts/pages and yet you are registering it as a shortcode so that strikes me as unlikely to work.

  6. snowbeachking
    Member
    Posted 8 months ago #

    Hi Matty,

    thanks for your feedback. Ok, I decided to build a template for my subscription page, where our family members can also manage their comments subscriptions. Therefore ignore the "if". :)

    But I am still struggling to get the two subscribe / unsubscribe buttons to work. :(

    That is what I tried now...

    <?php
    /**
     * Template Name: Subscribtion Management
     * Description: The template for displaying standard pages with sidebar.
     */
    
    /**
    Subscribe/unsubscribe user from one-click submission
    */
    function one_click_handler($user_ID, $action) {
    	if ( !isset($user_ID) || !isset($action) ) { return; }
    
    	$all_cats = $this->all_cats(true);
    
    	if ( 'subscribe' == $action ) {
    		// Subscribe
    		$new_cats = array();
    		foreach ( $all_cats as $cat ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id);
    			$new_cats[] = $cat->term_id;
    		}
    
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(',', $new_cats));
    
    		if ( 'yes' == $this->subscribe2_options['show_autosub'] && 'no' != get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true) ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'yes');
    		}
    	} elseif ( 'unsubscribe' == $action ) {
    		// Unsubscribe
    		foreach ( $all_cats as $cat ) {
    			delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id);
    		}
    
    		delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'));
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'no');
    	}
    } //end one_click_handler()
    
    add_action( 's2_one_click_subscription', 'one_click_handler' );
    
    if (isset($wp_subscribe_reloaded)) {
    	global $posts; $posts = $wp_subscribe_reloaded->subscribe_reloaded_manage();
    	} 
    
    get_header(); ?>
    
    	<div id="primary" class="site-content" role="main">
    
    		<?php
    			do_action( 's2_one_click_subscription' );
    			// One-click subscribe and unsubscribe buttons
    			echo "<h2>" . __('One Click Subscription / Unsubscription', 'subscribe2') . "</h2>\r\n";
    			echo "<p class=\"submit\"><input type=\"submit\" class=\"button-primary\" name=\"subscribe\" value=\"" . __("Subscribe to All", 'subscribe2') . "\" />&nbsp;&nbsp;";
    			echo "<input type=\"submit\" class=\"button-primary\" name=\"unsubscribe\" value=\"" . __("Unsubscribe from All", 'subscribe2') . "\" /></p>";
    
    			// Start the Loop.
    			while ( have_posts() ) : the_post();
    
    				// Include the page content template.
    				get_template_part( 'content', 'page' );
    
    				// If comments are open or we have at least one comment, load up the comment template.
    				if ( comments_open() || get_comments_number() ) {
    					comments_template();
    				}
    			endwhile;
    		?>
    
    	</div><!-- end #primary -->
    
    <?php get_footer(); ?>

    Thanks for your patient assistance so far.

    Kind regards.

  7. mattyrob
    Member
    Posted 8 months ago #

    @snowbeachking,

    If you are going to try this approach then you should probably just call the function directly passing in the user ID and button action rather than hooking with the do_action( 's2_one_click_subscription' ); line.

  8. snowbeachking
    Member
    Posted 8 months ago #

    Hi Matty,

    Thank you again for the support.

    That is what I currently have:

    <?php
    /**
     * Template Name: Subscribtion Management
     * Description: The template for displaying standard pages with sidebar.
     */
    
    // One-click subscribe and unsubscribe buttons
    // echo "<h2>" . __('One Click Subscription / Unsubscription', 'subscribe2') . "</h2>\r\n";
    // echo "<p class=\"submit\"><input type=\"submit\" class=\"button-primary\" name=\"subscribe\" value=\"" . __("Subscribe to All", 'subscribe2') . "\" />  ";
    // echo "<input type=\"submit\" class=\"button-primary\" name=\"unsubscribe\" value=\"" . __("Unsubscribe from All", 'subscribe2') . "\" /></p>";
    
    /**
    Subscribe/unsubscribe user from one-click submission
    */
    function one_click_handler($user_ID, $action) {
    	if ( !isset($user_ID) || !isset($action) ) { return; }
    
    	$all_cats = $this->all_cats(true);
    
    	if ( 'subscribe' == $action ) {
    		// Subscribe
    		$new_cats = array();
    		foreach ( $all_cats as $cat ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id);
    			$new_cats[] = $cat->term_id;
    		}
    
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(',', $new_cats));
    
    		if ( 'yes' == $this->subscribe2_options['show_autosub'] && 'no' != get_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), true) ) {
    			update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'yes');
    		}
    	} elseif ( 'unsubscribe' == $action ) {
    		// Unsubscribe
    		foreach ( $all_cats as $cat ) {
    			delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id);
    		}
    
    		delete_user_meta($user_ID, $this->get_usermeta_keyname('s2_subscribed'));
    		update_user_meta($user_ID, $this->get_usermeta_keyname('s2_autosub'), 'no');
    	}
    } //end one_click_handler()
    
    get_header(); ?>
    
    	<div id="primary" class="site-content" role="main">
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    			<header class="entry-header">
    				<h1 class="entry-title"><?php the_title(); ?></h1>
    			</header><!-- end .entry-header -->
    
    			<div class="entry-content clearfix">
    				<?php // the_content(); ?>
    
    					<!-- One-click subscribe and unsubscribe buttons -->
    					<p class="submit"><input type="submit" class="button-primary" name="subscribe" value="Subscribe to All" />  
    					<input type="submit" class="button-primary" name="unsubscribe" value="Unsubscribe from All" /></p>
    
    			</div><!-- end .entry-content -->
    
    		</article><!-- end post-<?php the_ID(); ?> -->
    
    	</div><!-- end #primary -->
    
    <?php get_footer(); ?>

    Your hint

    call the function directly passing in the user ID and button action

    brought me to some articles where it states that I need to use AJAX to use function for button click. Is that correct? If so, this doesn't make it really easier for me. :(

    Is my approach of having everything onto one site the easiest? Or what would you recommend for my goal of having just the two buttons on a page at the frontend?

    I really appreciate your help.

    Kind regards.

  9. mattyrob
    Member
    Posted 8 months ago #

    @snowbeachking,

    You don't need to use AJAX for this. With the code you have all you really need to do now is add some code to detect if the button has been clicked and then process that click appropriately.

    It'll be something along the lines of:

    if ( isset( $_POST['subscribe'] ) || isset( $_POST['unsubscribe'] ) ) {
    	// these are comments - you'll need to write this code as per your requirements but it'll be something like:
    	//check again if it's 'subscribe' or 'unsubscribe' - this is the 'action'
    	// get the user id
    	// call the function: one_click_handler($user_ID, $action)
    }

Reply

You must log in to post.

About this Plugin

  • Subscribe2
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.