Created
May 15, 2011 07:38
-
-
Save mrlannigan/972950 to your computer and use it in GitHub Desktop.
WPquestions.com #2235 rev1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // answer to wpquestions.com #2235 by Julian Lannigan (julianlannigan.com) | |
function coauthor_add_to_wardrobe_link() { | |
$currentUrl = "http".(($_SERVER["HTTPS"] == "on") ? "s" : "")."://".$_SERVER["SERVER_NAME"].(($_SERVER["SERVER_PORT"] != "80") ? ":".$_SERVER["SERVER_PORT"] : "").$_SERVER["REQUEST_URI"]; //Gets current page url | |
if (defined('COAUTHORS_PLUS_VERSION')) { | |
global $current_user; | |
if (is_user_logged_in()) { | |
//Check if they have requested to be an author | |
if (isset($_REQUEST['addToWardrobe']) && is_numeric($_REQUEST['addToWardrobe']) && $_REQUEST['addToWardrobe'] > 0) { | |
if (!is_coauthor_for_post($current_user->user_login, $_REQUEST['addToWardrobe'])) { | |
$cAuthors = array(); | |
foreach (get_coauthors($_REQUEST['addToWardrobe']) as $theAuthor) { | |
$cAuthors[] = $theAuthor->user_login; | |
} | |
$cAuthors[] = $current_user->user_login; | |
$cObj = new coauthors_plus(); | |
$cObj->add_coauthors($_REQUEST['addToWardrobe'], $cAuthors); | |
$statusMsg = "This has been added to your wardrobe!"; | |
} | |
} | |
//Display link | |
if (isset($statusMsg) && $statusMsg != "" && isset($_REQUEST['addToWardrobe']) && $_REQUEST['addToWardrobe'] == $post->ID) { | |
//They just added it to the wardrobe | |
echo $statusMsg; | |
} elseif (is_coauthor_for_post($current_user->user_login, $post->ID)) { | |
//They are already a coauthor. | |
echo "This is already in your wardrobe."; | |
} else { | |
//They are not a coauthor yet. | |
$currentUrl = preg_replace('/([?&])addToWardrobe=[^&]+(&|$)/','$1',$url); //remove the get var in the url | |
echo "<a href=\"".$currentUrl."?addToWardrobe={$post->ID}\">Add this to your wardrobe.</a>"; | |
} | |
} else { | |
//This is totally optional, but you could output | |
// something here that says "Login to add to your wardrobe" | |
echo "<a href=\"".wp_login_url($currentUrl)."\">Login to add to your wardrobe</a>"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment