function my2_enqueue_scripts() {
if (is_author()) {
$author_id = get_the_author_meta('ID');
wp_enqueue_script('my-subscribe-script', get_template_directory_uri() . '/js/subscribe.js', array('jquery'), null, true);
wp_localize_script('my-subscribe-script', 'SubscribeData', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'authorId' => $author_id,
));
}
}
add_action('wp_enqueue_scripts', 'my2_enqueue_scripts');
add_action('wp_ajax_subscribe_author', 'subscribe_authore');
function subscribe_author_2() {
$user_id = get_current_user_id();
$author_id = $_POST['authorId'];
$subscribed_authors = get_user_meta($user_id, 'sub_authors', true);
if (!is_array($subscribed_authors)) {
$subscribed_authors = array();
}
if (!in_array($author_id, $subscribed_authors)) {
$subscribed_authors[] = $author_id;
update_user_meta($user_id, 'sub_authors', $subscribed_authors);
wp_send_json_success();
} else {
wp_send_json_error();
}
}
add_action('wp_ajax_unsubscribe_author', 'unsubscribe_authore');
function unsubscribe_author_2() {
$user_id = get_current_user_id();
$author_id = $_POST['authorId'];
$subscribed_authors = get_user_meta($user_id, 'sub_authors', true);
if (!is_array($subscribed_authors)) {
$subscribed_authors = array();
}
if (($key = array_search($author_id, $subscribed_authors)) !== false) {
unset($subscribed_authors[$key]);
update_user_meta($user_id, 'sub_authors', $subscribed_authors);
wp_send_json_success();
} else {
wp_send_json_error();
}
}