WordPress创建前端发布插件

时间:2014-03-16 22:48:08

标签: wordpress frontend posting

我尝试创建一个插件,允许用户从前端添加新帖子,编辑帖子和删除帖子。 现在,当用户尝试编辑帖子而不是更新同一帖子时,插件会创建新的页面!处于待定状态。 有人可以帮我吗? 这是编辑帖子类的代码:

<?php
/**
 * Edit Post form class
 *
 * @author Engr.MTH
 * @package MOSTASHAROON Frontend Form
 */
class MOSFF_Edit_Post {

    function __construct() {
        add_shortcode( 'mosff_editpost', array($this, 'shortcode') );
    }

    /**
     * Handles the edit post shortcode
     *
     * 
     */
    function shortcode() {


$query = new WP_Query(array('post_type' => 'post', 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft', 'private', 'trash') ) );

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();global $post;

    if(isset($_GET['post'])) {

        if($_GET['post'] == $post->ID)
        {
            $current_post = $post->ID;

            $title = get_the_title();
            $content = get_the_content();

            $custom_one = get_post_meta($current_post, 'vsip_custom_one', true);
            $custom_two = get_post_meta($current_post, 'vsip_custom_two', true);
        }
    }

endwhile; endif;
wp_reset_query();

global $current_post;

$postTitleError = '';

if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {

    if(trim($_POST['postTitle']) === '') {
        $postTitleError = 'Please enter a title.';
        $hasError = true;
    } else {
        $postTitle = trim($_POST['postTitle']);
    }

    $post_information = array(
        'ID' => $current_post,
        'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
        'post_content' => esc_attr(strip_tags($_POST['postContent'])),
        'post-type' => 'post',
        'post_status' => 'pending'
    );

    $post_id = wp_update_post($post_information);

    if($post_id)
    {

        // Update Custom Meta
        update_post_meta($post_id, 'vsip_custom_one', esc_attr(strip_tags($_POST['customMetaOne'])));
        update_post_meta($post_id, 'vsip_custom_two', esc_attr(strip_tags($_POST['customMetaTwo'])));

        wp_redirect( home_url() ); exit;
    }

}

?>



    <!-- #primary BEGIN -->
    <div id="primary">

        <form action="" id="primaryPostForm" method="POST">

            <fieldset>

                <label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>

                <input type="text" name="postTitle" id="postTitle" value="<?php echo $title; ?>" class="required" />

            </fieldset>

            <?php if($postTitleError != '') { ?>
                <span class="error"><?php echo $postTitleError; ?></span>
                <div class="clearfix"></div>
            <?php } ?>

            <fieldset>

                <label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>

                <textarea name="postContent" id="postContent" rows="8" cols="30"><?php echo $content; ?></textarea>

            </fieldset>

            <fieldset>

                <label for="customMetaOne"><?php _e('Custom Meta One:', 'framework') ?></label>

                <input type="text" name="customMetaOne" id="customMetaOne" value="<?php echo $custom_one; ?>" />

            </fieldset>

            <fieldset>

                <label for="customMetaTwo"><?php _e('Custom Meta Two:', 'framework') ?></label>

                <input type="text" name="customMetaTwo" id="customMetaTwo" value="<?php echo $custom_two; ?>" />

            </fieldset>

            <fieldset>

                <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>

                <input type="hidden" name="submitted" id="submitted" value="true" />
                <button type="submit"><?php _e('Update Post', 'framework') ?></button>

            </fieldset>

        </form>


    </div><!-- #primary END -->

<?php }}
$mosff_editpostform = new MOSFF_Edit_Post();

2 个答案:

答案 0 :(得分:0)

我找到了答案,只是我删除了这一行:

global $current_post;

答案 1 :(得分:0)

我认为您可以通过获取帖子ID并查询它来获取特定字段的所有字段并通过表单进行编辑并再次发布。

在此处获取更详细的文章kvcodes

相关问题