News Feed Follow, Subscribe User ( ok)
https://github.com/iantsch/mbtNewsFeed
Ví dụ 1: Sử dụng trên theme (ok)
C:\xampp\htdocs\style2\wp-content\themes\gutener\functions.php
// follow
function mbt_news_feed_init() {
if(array_key_exists('action', $_REQUEST) && in_array($_REQUEST['action'], mbt_news_feed_valid_actions())) {
do_action( 'wp_ajax_' . $_REQUEST['action'] );
}
}
function mbt_news_feed_pre_get_posts($query) {
if (is_user_logged_in()) {
if ( $query->is_main_query() && $query->is_home() && !is_admin()) {
$current_user = wp_get_current_user();
$followers = get_user_meta( $current_user->data->ID, 'following', true);
if (is_array($followers) && !empty($followers)) {
$followers = array_keys($followers);
$followers[] = $current_user->data->ID;
$query->set('author__in', $followers);
}
}
}
return $query;
}
function mbt_news_feed_is_following($user_id) {
$user_id_current = get_current_user_id();
$followers = get_user_meta( $user_id_current, 'following', true);
if (empty($followers)) {
return false;
}
if (array_key_exists($user_id, $followers)) {
return true;
}
return false;
}
function mbt_news_feed_helper_check_data() {
if(!wp_verify_nonce( $_POST['security'], 'file_upload' )) {
die(-1);
}
if (!array_key_exists('user_id', $_POST)) {
die(-1);
}
$user_id = absint($_POST['user_id']);
if ($user_id < 1) {
die(-1);
}
return $user_id;
}
function mbt_following() {
$user_id = mbt_news_feed_helper_check_data();
$current_user_id = get_current_user_id();
$followers = get_user_meta( $current_user_id, 'following', true);
if (!is_array($followers)){
$followers = array();
}
$followers[$user_id] = $current_user_id;
update_user_meta( $current_user_id, 'following', $followers );
echo mbt_news_feed_button($user_id, false);
exit;
}
function mbt_follow() {
$user_id = mbt_news_feed_helper_check_data();
$current_user_id = get_current_user_id();
$followers = get_user_meta( $current_user_id, 'following', true);
if (is_array($followers) && !empty($followers)){
unset($followers[$user_id]);
} else {
$followers = array();
}
update_user_meta( $current_user_id, 'following', $followers );
echo mbt_news_feed_button($user_id, true);
exit;
}
function mbt_news_feed_button($user_id, $follow=true) {
$label = __('Follow','newsfeed');
$class = 'follow';
if (false === $follow) {
$class = 'following';
$label = __('Following','newsfeed');
}
return "<a href='#' class='button {$class}' data-user_id='{$user_id}'>{$label}</a>";
}
function mbt_news_feed_valid_actions() {
return array(
'mbt_following',
'mbt_follow',
);
}
function mbt_news_feed_wp_footer() {
if (is_user_logged_in()) {
$render_button = false;
$user_id = 0;
if (is_singular()) {
$render_button = true;
$post_id = get_queried_object_id();
$user_id = get_post_field( 'post_author', $post_id );
}
if (is_author()) {
$render_button = true;
$user_id = get_queried_object_id();
}
if (true === $render_button && $user_id > 0) {
$follow = mbt_news_feed_is_following($user_id );
echo "<div id='mbt-news-feed'>";
echo mbt_news_feed_button($user_id, !$follow);
echo "</div>";
}
}
}
if (is_admin()){
foreach (mbt_news_feed_valid_actions() as $action) {
add_action('wp_ajax_'.$action, $action);
}
}
add_action( 'init', 'mbt_news_feed_init');
add_action( 'pre_get_posts', 'mbt_news_feed_pre_get_posts');
add_action( 'wp_footer', 'mbt_news_feed_wp_footer');C:\xampp\htdocs\style2\wp-content\themes\gutener\assets\js\newsfeed.js
C:\xampp\htdocs\style2\wp-content\themes\gutener\assets\css\newsfeed.css

Ví dụ 2: Sử dụng trên plugin
C:\xampp\htdocs\hanam\wp-content\plugins\mbtNewsFeed\newsfeed.php
C:\xampp\htdocs\hanam\wp-content\plugins\mbtNewsFeed\newsfeed.js
C:\xampp\htdocs\hanam\wp-content\plugins\mbtNewsFeed\newsfeed.css


PreviousWooCommerce Combo Offers (ok)Nextget post field get_post_field Retrieve data from a post field based on Post ID (ok)
Last updated
Was this helpful?