Skip to content

Instantly share code, notes, and snippets.

@yuptogun
Last active September 24, 2015 21:36
Show Gist options
  • Save yuptogun/11ff352e2f239213b93c to your computer and use it in GitHub Desktop.
Save yuptogun/11ff352e2f239213b93c to your computer and use it in GitHub Desktop.
wordpress category watchlist add/remove

watchlist.php

After finding out that nobody has ever built a "favorite category" plugin or anything like that, I just decided to write one by myself. 6 hours of sleepless var_dump() debugging, a sheet of paper with handwritten function drafts, no possible acknowledgement from the boss.

How it works

  1. You put it anywhere inside your Wordpress directory. I prefer the root.
  2. You create the dynamically generated checkin/checkout buttons in your Wordpress site. Well that's your homework.
  3. They click the button to go to somewhere like: /watchlist.php?user=16&check=out&program=2
  4. It add_user_meta() or update_user_meta() to record what they checked in or out to the program, or a certain taxonomy.

TO DO

  • Generalize the parameter namings
  • Reform it to a Wordpress plugin
<?php
/*
* watchlist.php
* : a custom php controller to let the wordpress users add/remove some categories to their watchlist.
* damn it nobody has done this? i better publish a FREE GPL licensed plugin
*/
/*
* step 0
* load the wordpress functionalities
*/
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path.'/wp-config.php';
include_once $path.'/wp-includes/wp-db.php';
include_once $path.'/wp-includes/pluggable.php';
/*
* step 1
* set the parameters
* $who (str) ID value in wp_users
* $how (str) 'in' or 'out'
* $what (str) term_id value in wp_term_taxonomy
* $why (str) 'watchlist', or meta_key in wp_usermeta
* $where (str) taxonomy value in wp_term_taxonomy
* $whothe (str) name value in wp_users
* $whatthe (str) name value in wp_terms
*/
$who = intval($_GET['user']);
$how = $_GET['check'];
$what = intval($_GET['program']);
$why = 'watchlist';
$where = 'categories';
$whatthe = $whothe = '';
if (!empty($who) && !empty($what)) {
$whothe = get_userdata($who)->display_name;
$whatthe = get_term_by('id',$what,$where)->name;
}
/*
* step 2
* define the functions
*/
// shows an alert message and immediatedly redirects to somewhere.
// by default it just goes back with a general error message below.
// pass a good href URI to $togo to get no error.
function alertToGo ($alert='뭔가 문제가 있습니다. 뒤로 돌아갑니다.', $togo='back') {
$script = '<script>window.alert("'.$alert.'"); window.';
if ($togo != 'back') {
$script .= 'location.href="'.$togo.'";</script>';
} else {
$script .= 'history.back();</script>';
}
echo $script;
}
// adds the category to the watchlist only if it has never been a watch.
function addIt() {
global $who, $what, $why, $whothe, $whatthe;
$watches = get_user_meta($who, $why); // array of all the watches
if (!$watches) {
$watches = array();
}
if (in_array($what, $watches)) { // which might match the required program number
alertToGo('오잉? '.$whatthe.'은(는) 이미 구독중이십니다.');
} else { // but which actually doesn't
$query = add_user_meta($who, $why, $what);
if ($query) {
alertToGo($whatthe.'을(를) 구독해 주셔서 감사합니다!');
} else {
alertToGo();
}
}
unset($watches);
}
// removes the category from the watchlist only if it really has been the watch.
function removeIt() {
global $who, $what, $why, $whothe, $whatthe;
$watches = get_user_meta($who, $why);
if (!$watches) {
alertToGo('헐~ '.$whothe.'님은 아무것도 구독하신 적이 없으신데요?\n지금 프로그램 구독을 시작하세요!', esc_url( home_url( '/' ) ));
} else {
if (!in_array((string)$what, $watches)) {
alertToGo('읭? '.$whatthe.'은(는) 원래부터 구독한 적이 없는 프로그램 같은데요?');
} else {
$query = delete_user_meta($who, $why, $what);
if ($query) {
alertToGo($whothe.'님께서 '.$whatthe.'의 구독을 잠시 멈추셨습니다.', esc_url( home_url( '/' ) ));
} else {
alertToGo();
}
}
}
unset($watches);
}
/*
* step 3
* rock and roll
*/
if (empty($who) || empty($what) || empty($how)) {
alertToGo('비정상적인 접근입니다!', esc_url( home_url( '/' ) ));
} else {
if ($how == 'in') {
addIt();
} elseif ($how == 'out') {
removeIt();
} else {
alertToGo('뭘 원하시는지 모르겠습니다.\n일단 홈페이지로 돌아갑니다.', esc_url( home_url( '/' ) ));
}
}
// end of watchlist.php
// handwritten by github.com/yuptogun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment