S
S
svkalashnikov2016-03-30 11:15:45
WordPress
svkalashnikov, 2016-03-30 11:15:45

How to subscribe to categories and tags like in wordpress habré?

Hello.
I want to make the functionality of subscribing to categories, tags and other taxonomies on wordpress (similar to how it was done on Habré). Those. if the user is logged in, then his feed of posts is formed from those categories and tags to which he is subscribed.
To do this, I added a field for the user in which category identifiers are stored (I think the approach will be the same for tags and other taxonomies). The formation of the feed of posts based on this field is correct, but it is not possible to make the subscription button for the category displayed on the category page and implementing the addition of the ID of the category on which it is clicked to the user field.
An example category.php code is as follows:

get_header(); ?>

  <main id="main" class="site-main col-md-9 col-md-push-3" role="main">

    <?php if ( have_posts() ) : ?>

      <header class="page-header">
        <?php
          the_archive_title( '<h1 class="page-title">', '</h1>' );
        ?>
      <form method = "post">
        <input type = "submit" name = "button1" value = "Подписаться">
      <form>
      <?php
        if($_POST['button1']) { 
          $user_ID = get_current_user_id();
          $NewCategoryID = get_the_category();
          $NewCategories =  the_author_meta('user_categories').','.$NewCategoryID->cat_ID;
          update_user_meta( $user_ID, 'user_categories', $NewCategories );				}
      ?>
      </header><!-- .page-header -->

      <div id="posts-container">
      ......

It doesn't work: for some reason the title changes and the same posts of the category in which the button was clicked are displayed.
Please advise how to implement the subscription functionality.
UPDATE:
It turned out to add the category number to the custom field. But it is not possible to get this action on the button. Those. there is a button:
<input name="action" type="hidden" id="action" value="update-user" />

The button handler is:
if ( isset( $_POST['action'] ) ) {
        	                	$NewCategoryID = get_the_category();        
        	                	$user_info = get_the_author_meta('user_categories',$current_user->ID);
                $NewUserCategories = $user_info.','.$NewCategoryID[0]->term_id;
                update_usermeta( $current_user->ID, 'user_categories', $NewUserCategories);					
        	                }

The 'user_categories' user profile field stores the IDs of the categories this user should see, separated by commas.
Clicking the button opens the first post of the category on the page of which the button was clicked. At the same time, the 'user_categories' identifier of this category is not added to the field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
svkalashnikov, 2016-04-11
@svkalashnikov

And did not understand why the redirect is carried out. Made it a crutch. When you open a page with a post, you are redirected back to the page with the category.
Part of the code in single.php:

if ( $_POST['action'] == 'update-user' )  {
  //var_dump($_POST['action']);


  if (!($_POST['updateuser'] == 'Отписаться')) {


  $NewCategoryID = get_the_category();       
  $user_info = get_the_author_meta('user_categories',$current_user->ID);
  $NewUserCategories = $user_info.','.$NewCategoryID[0]->term_id;
  update_usermeta( $current_user->ID, 'user_categories', $NewUserCategories);

  $LinkToPage=get_category_link($NewCategoryID[0]->term_id);
  wp_redirect( $LinkToPage );
  exit;
    } else {
      $NewCategoryID = get_the_category();       
      $user_info = get_the_author_meta('user_categories',$current_user->ID);			

      $intNumCategory = strpos($user_info, (string)$NewCategoryID[0]->term_id);
                        
      if ($intNumCategory == 0) {
          $StrNewCategoryID = $NewCategoryID[0]->term_id.',';
      } else {
          $StrNewCategoryID = ','.$NewCategoryID[0]->term_id;
        }
    
      $NewUserCategories = str_replace($StrNewCategoryID,'',$user_info);
      update_usermeta( $current_user->ID, 'user_categories', $NewUserCategories);

      $LinkToPage=get_category_link($NewCategoryID[0]->term_id);
      wp_redirect( $LinkToPage );
      exit;
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question