I try without success to send the post id to a page where a front end edit form is displayed by a shortcode and to insert the title of that post in the form. Where am I wrong? Any suggestions?
The form to send the post id to the edit page:
<form class="edit-user-post" action="<?php echo home_url( '/edit'); ?>" method="post">
<!-- get the post ID into "postid" and pass it to "edit" page -->
<input type="hidden" name="postid" value="<?php the_ID(); ?>" />
<input type="submit" value="Edit" />
</form>
The front end edit post code (adapted from here):
class WPSE_Edit_From_Front {
const NONCE_VALUE = 'front_end_edit_post';
const NONCE_FIELD = 'feep_nonce';
protected $pluginPath;
protected $pluginUrl;
protected $errors = array();
protected $data = array();
function __construct() {
$this->pluginPath = plugin_dir_path( __file__ );
$this->pluginUrl = plugins_url( '', __file__ );
add_shortcode( 'front_post_edit', array( $this, 'post_shortcode' ) );
// Listen for the form submit & process before headers output
add_action( 'template_redirect', array( $this, 'handleForm' ) );
}
/**
* Shortcodes should return data, NOT echo it.
*
* @return string
*/
function post_shortcode() {
if ( ! current_user_can( 'publish_posts' ) )
return sprintf( '<p>You must to <a href="%s">login</a>.</p>', esc_url( wp_login_url( get_permalink() ) ) );
elseif ( $this->isFormSuccess() )
return '<p class="alert-box success"><span>Excellent: </span> The post was saved!</p>';
else
return $this->getForm();
}
/**
* Process the form and redirect if sucessful.
*/
function handleForm() {
...
}
/**
* Use output buffering to *return* the form HTML, not echo it.
*
* @return string
*/
function getForm() {
ob_start();
?>
<?php foreach ( $this->errors as $error ) : ?>
<p class="error alert-box"><span>Error: </span><?php echo $error ?></p>
<?php endforeach ?>
<form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">
<fieldset>
<!-- Item name -->
<label for="item_name">Title<span class="required">*</span></label>
<input type="text" name="item_name" id="item_name" title="Edit the title" value="<?php
// "Sticky" field, will keep value from last POST if there were errors
if ( isset( $this->data['item_name'] ) ) {
echo esc_attr( $this->data['item_name'] );
} else {
echo $data->post_title;
}
?>" />
<!-- Submit button -->
<label for="submitForm"></label>
<div class="submitForm-wrapper">
<button type="submit" name="submitForm" id="submitForm" title="Save the post">Save</button>
</div>
</fieldset>
<?php wp_nonce_field( self::NONCE_VALUE , self::NONCE_FIELD ) ?>
</form>
<?php
return ob_get_clean();
}
/**
* Has the form been submitted?
*
* @return bool
*/
function isFormSubmitted() {
return isset( $_POST['submitForm'] );
}
/**
* Has the form been successfully processed?
*
* @return bool
*/
function isFormSuccess() {
return filter_input( INPUT_GET, 'success' ) === 'true';
}
/**
* Is the nonce field valid?
*
* @return bool
*/
function isNonceValid() {
return isset( $_POST[ self::NONCE_FIELD ] ) && wp_verify_nonce( $_POST[ self::NONCE_FIELD ], self::NONCE_VALUE );
}
}
new WPSE_Edit_From_Front;