将帖子ID发送到前端编辑帖子表单

时间:2015-12-16 作者:Iurie

我尝试将帖子id发送到一个页面,在该页面上,前端编辑表单以快捷码显示,并在表单中插入该帖子的标题,但没有成功。我错在哪里?有什么建议吗?

将帖子id发送到编辑页面的表单:

<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>
前端编辑邮编(改编自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;

1 个回复
SO网友:Iurie

post id发送正确。我需要做的是在ob_start():

$post_to_edit = array();
$post_to_edit = get_post( $_POST[\'postid\'] );
$this->data[\'item_name\'] = $post_to_edit->post_title;

相关推荐

是否可以取消对特定帖子类型的POSTS_PER_PAGE限制?

我想知道我是否可以取消特定帖子类型的posts\\u per\\u页面限制。在存档中。php页面我显示不同的帖子类型,对于特定的“出版物”帖子类型,我想显示所有帖子。我如何在不影响传统“post”类型的情况下实现这一点?