Answer the question
In order to leave comments, you need to log in
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">
......
<input name="action" type="hidden" id="action" value="update-user" />
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);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question