Following this guide, I am trying to create my own anonymous user submitted post plugin.
Below is my code so far:
function suq_form_shortcode() {
if (
isset($_POST['suq_form_create_quote_submitted'])
&& wp_verify_nonce($_POST['suq_form_create_quote_submitted'],'suq_form_create_quote')
) {
$suq_quote_title = trim($_POST['suq_quote_title']);
$suq_quote_author = trim($_POST['suq_quote_author']);
$suq_quote_text = trim($_POST['suq_quote_text']);
if ($suq_quote_author != '' && $suq_quote_text != '') {
$quote_data = array(
'post_title' => $suq_quote_title,
'post_content' => $suq_quote_text,
'post_status' => 'pending',
'post_author' => $suq_quote_author,
'post_type' => 'post'
);
if($quote_id = wp_insert_post($quote_data)){
echo '<p>Quote created and awaiting moderation!</p>';
}
} else { // author or text field is empty
echo '<p>Quote NOT saved! Who said it? and Quote must not be empty.</p>';
}
}
echo suq_get_create_quote_form($suq_quote_author, $suq_quote_text, $suq_quote_category);
}
function suq_get_create_quote_form ($suq_quote_author = '', $suq_quote_text = '', $suq_quote_category = 0) {
$out .= '<form id="create_quote_form" method="post" action="">';
$out .= wp_nonce_field('suq_form_create_quote', 'suq_form_create_quote_submitted');
$out .= '<label for="suq_quote_author">Name</label><br/>';
$out .= '<input type="text" id="suq_quote_author" name="suq_quote_author" value="' . $suq_quote_author . '"/><br/>';
$out .= '<label for="suq_quote_title">Title</label><br/>';
$out .= '<input type="text" id="suq_quote_title" name="suq_quote_title" value=""/><br/>';
$out .= '<label for="suq_quote_text">Quote</label><br/>';
$out .= '<textarea id="suq_quote_text" name="suq_quote_text" />' . $suq_quote_text . '</textarea><br/><br/>';
$out .= '<input type="submit" id="suq_submit" name="suq_submit" value="Submit Quote For Publication">';
$out .= '</form>';
return $out;
}
It works fine as I can see the submitted posts in my admin area, but it one of info is missing which is the author name that user submitted.
Any ideas how I can show the author name that provided by the user themselves?
I have passed the author name to WP:
'post_author' => $suq_quote_author,
But WP does not seem to pick it up…
post_author
Take (int) The ID of the user who added the post. Default is the current user IDIn your case “Anonymous user” can submit the post. No user related to your post save in user table so save this “Anonymous user name” in post meta table and ‘post_author’ = admin user id (1)
To display this fields in post listing at backend. Add this function in
function.php
fileFor get the value of Anonymous user name add this function in
function.php
fileIf you remove the default “Author” from the post listing used
unset($columns['author']);
inadd_anonymous_user_column()
function