Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Script to add current author to post from front end WordPress

  • SOLVED

I need to find a way to let the users (logged in only) add them selves as an author to a post from the front end.

I am developing a fashion community (in phase 1 at the moment) and I would like for people to be able to search through the community and then click a button ('Add this to your wardrobe') on an item (post) they like and have them added as an author as well.

I am not using buddypress (and don't want to), and found two plugins for co authoring (but they are back end only).

I would also like to know what function would need to be called so I can create a sidebar widget of all the author avatars for that post (maybe capped at 15?)

Is this possible? thanks

My site is here for your reference

dropdeadgorgeousdaily.com

Answers (5)

2011-05-12

John Cotton answers:

Since each WordPress post can only have one author id in the database (one row, one field), the only way you're going to be able to do this is (probably) with a custom field.

That would be straightforward enough. Assuming your 'Add this to your wardrobe' button sat in a form that posted back, something along these lines in the template for that post_type or category would do the trick:


global $post, $current_user;

// We only want this for logged in users
if( is_user_logged_in() ) {
// is this a post back?
if( isset($_POST['add_to_wardrobe']) ) {
// Store the 'author' id as a custom field
add_post_meta($post->ID, '_co_author_id', $current_user->ID, false);
echo 'Added to your wardrobe';

} else {
$current_author = false;

// check they're not a co-author already
$authors = get_post_meta($post->ID, '_co_author_id');

if( !empty($authors) ) {
foreach( $authors as $id ) {
if( $id == $current_user->ID ) {
echo 'It\'s already in you\'re wardrobe!';
$current_author = true;
break;
}
}
}

// If they've not been found, output the form
if( !$current_author ) {
echo '<form method="post"><input type="submit" name="add_to_wardrobe" value="Add this to your wardrobe"></form>';
}
}
}


Your avatar code could then use something similar to the checking loop in the middle, but this time output the icon for each author found.

The code needs some more work - for example, what if the current user actually created the post? - but hopefully you'll get the idea.

<strong>PS Hand-typed code so I've not checked it for errors!</strong>

2011-05-15

Julian Lannigan answers:

Hi again Kate,

I wrote a snippet that should solve your problem. It piggy-backs on the [[LINK href="http://wordpress.org/extend/plugins/co-authors-plus/"]]Co-Authors Plus plugin[[/LINK]]. It was interesting to see how they made their plugin work. They effectively create a custom tax named 'author' and all the coauthors are saved using the taxonomies system of WP.

The script would be best if it was placed in your functions.php as a function that you could call anywhere in your template, like a loop template tag. I have attached a function that is named "coauthor_add_to_wardrobe_link()".

Example usage:
<?php coauthor_add_to_wardrobe_link(); ?>

Code to be placed in your functions.php file: ([[LINK href="https://gist.github.com/972950"]]it on gist[[/LINK]])
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>";
}
}
}


kateM82 comments:

Sounds promising thanks (AGAIN) Julian! I am too brain dead tonight to give a try, but I will try it tomorrow and let you know!

2011-05-12

Rashad Aliyev answers:

http://wordpress.org/extend/plugins/tdo-mini-forms/ Try this plugin.


kateM82 comments:

I already have a plugin that lets users submit posts, i want them to be able to add themselves as an author to existing posts (so that when you view their author archive you can see all the posts they have added AND have liked). thanks


kateM82 comments:

the plugin I use is Gravity forms to do this - fyi :)

2011-05-12

Just Me answers:

Maybe you need something else. Can you explain a bit more what you are looking for. To have people add themselves as authors to a post doesn't seem that logical.


kateM82 comments:

Ok it might not seem logical, but it actually makes the most if I can get it to work.

Currently when you click on an author/users name anywhere on the site it takes you to their author archive - ie http://dropdeadgorgeousdaily.com/author/lmihalj/
Which as you can see shows all the items a person has added. And I want to keep using that, rather than having two lots of listings (and having to change all those links in all their various forms).

In my community, users can add items they like and want to share, at the moment there is a lot of doubling up going on, and it's killing my server, so long story short what I want to do is if someone submits something that's already added, they will be prompted to just add themselves to the existing item (post). Gravity forms handles the duplicate check etc.. but I need people to be able to then add themselves as authors from the front end.

I know there are multi author plugins out there that allow for more than one person to be the author (http://wordpress.org/extend/plugins/co-authors-plus/ is one) but they are all back end based, I need something that will work from a simple button on the post.

Does that make it any clearer?


Just Me comments:

Uhm, a little, but still confusing. So I write a wonderful piece about the newest invention in fashion, post it (not sure how you manage the people who read, and the ones who post) and then someone else comes along likes the article and says "Hey I am the author too" ?!?!?

I am still under the impression you do not really want them added as author, maybe as supporter or something.

I read the other answers you gave. You want to show the posts I wrote (let's say I am one of the writers) and the ones I wanted to write about but were already there so I "liked" them.

What you need is what is described for the wordpress.com blogs here http://en.blog.wordpress.com/2010/08/03/like-a-post/ not sure if there is a plugin for that, that would work on other WordPress installations. They talk about displaying in the admin but I guess, once it is installed you can add it to the author's post listing as well.

Sorry for all the questions but someone with a flat tire could ask for air while the person would be better off if someone pointed out the tire needs to be fixed first.


kateM82 comments:

Hi Just Me,

I guess the why is kind of complicated if your not familiar with the site, but trust me when I say i want them to be the author.

When a user adds a post, it looks like this - http://dropdeadgorgeousdaily.com/2011/05/lbd/ - So as you can see, it's not really about the authors work or skill, they've just found a pic and added it with a link. Currently on the post it says 'added by' and has the authors details, I will change that to 'First found by' and then any other people who have added the item will show in a side bar widget.

So yes, I really do want them to be co authors. I promise.

I might have to look at getting a custom plugin made, but I thought i'd ask here first. thanks


Just Me comments:

I hope Julian's solution works. Although I am not sure it will take care of the listing.
Good Luck!

2011-05-13

Kristin Falkner answers:

Another route you could also try is to accomplish this with the "My Favorite Posts" plug-in:
http://www.kriesi.at/archives/wordpress-plugin-my-favorite-posts

Users could then click "Add to Wardrobe" (which would be "Add to Favorite" in terms of functionality) and you could, of course, change the title of My Favorite Posts to read whatever you want.

The plug-in supports passing a user ID to it to display that user's favorite posts, as described in the link above:
<?php mfp_display("order_by=post_title",13); ?>

Where 13 is the user ID.

You can easily pull an user's ID via the post meta on the author page:
http://codex.wordpress.org/Template_Tags/the_author_meta


kateM82 comments:

Hi Kristin,

I have looked at the favourites plugin and already have one being used for something slightly different. here's a bit more info on why I need it to be authors

Ok it might not seem logical, but it actually makes the most if I can get it to work.

Currently when you click on an author/users name anywhere on the site it takes you to their author archive - ie http://dropdeadgorgeousdaily.com/author/lmihalj/
Which as you can see shows all the items a person has added. And I want to keep using that, rather than having two lots of listings (and having to change all those links in all their various forms).

In my community, users can add items they like and want to share, at the moment there is a lot of doubling up going on, and it's killing my server, so long story short what I want to do is if someone submits something that's already added, they will be prompted to just add themselves to the existing item (post). Gravity forms handles the duplicate check etc.. but I need people to be able to then add themselves as authors from the front end.

I know there are multi author plugins out there that allow for more than one person to be the author (http://wordpress.org/extend/plugins/co-authors-plus/ is one) but they are all back end based, I need something that will work from a simple button on the post.

Does that make it any clearer?