为什么表单不需要带有使用POST数据的插件的“操作”?

时间:2016-04-17 作者:mesqueeb

所以我对WordPress插件还是比较陌生的。有一件事我不确定。我正在构建一个前端表单,允许用户发布帖子。在下面的这本指南中,有一个插件允许用户这样做。然而,他构建的“表单”没有任何“操作”,就像表单没有将用户重定向到任何地方一样。

https://www.wpkb.com/add-front-end-posting-functionality-wordpress-building-plugin/

我将有一个表单操作重定向到一个php页面,该页面将启动createPost(); 作用Why doesn\'t this form have an action?

此表单无需任何操作即可工作,因此
Since the action of the form doesn\'t redirect to a PHP page, how can this plugin know that it should fire this function: createPost();?

它没有写如何在表单页面上包含插件文件。还是因为WP短代码而被包括在内?

以下是github中插件的完整代码:
https://github.com/abbassoftware/SubmitFromFront/blob/master/submitfromfront.php

下面是原始完整代码:

<?php
/*
Plugin Name: SubmitFromFront
Plugin URI: 
Description: This creates a form so that posts can be submitted from the front end
Version: 1.0
Author: a
Author URI: 
*/
class WPSubmitFromFront {
    protected $pluginPath;  
    protected $pluginUrl;  
    public function __construct() {  
        // Set Plugin Path  
        $this->pluginPath = dirname(__FILE__);  
        // Set Plugin URL  
        $this->pluginUrl = WP_PLUGIN_URL . \'/submitfromfront\';
         //Add CSS for the form.
        add_action(\'wp_enqueue_scripts\', array($this, \'addStyles\'));
        //Add the short code
        add_shortcode(\'POST_FROM_FRONT\', array($this, \'handleFrontEndForm\'));  

    }
    function handleFrontEndForm() {
        //Check if the user has permission to publish the post.
        if ( !current_user_can(\'publish_posts\') ) {
            echo "<h2>Please Login to post links.</h2>";
            return;
        }
        if($this->isFormSubmitted() && $this->isNonceSet()) {
            if($this->isFormValid()) {
                $this->createPost();
            } else {
                $this->displayForm();
            }
        } else {
            $this->displayForm();
        }
    }
    //This function displays the HTML form.
    public function displayForm() {
        ?>
        <div id ="frontpostform">
            <form action="" id="formpost" method="POST" enctype="multipart/form-data">

                <fieldset>
                    <label for="postTitle">Post Title</label>

                    <input type="text" name="postTitle" id="postTitle" />
                </fieldset>

                <fieldset>
                    <label for="postContent">Content</label>

                    <textarea name="postContent" id="postContent" rows="10" cols="35" ></textarea>
                </fieldset>

                <fieldset>
                    <button type="submit" name="submitForm" >Create Post</button>
                </fieldset>

                <?php wp_nonce_field( \'front_end_new_post\' , \'nonce_field_for_front_end_new_post\'); ?>

            </form>
        </div>
        <?php
    }
    function addStyles() {
        // Register the style for the form
        wp_register_style( \'submitform-style\', plugins_url( \'submitfromfront/submitfromfront.css\'));
        wp_enqueue_style( \'submitform-style\' );
    }
    function isFormSubmitted() {
        if( isset( $_POST[\'submitForm\'] ) ) return true;
        else return false;
    }
    function isNonceSet() {
        if( isset( $_POST[\'nonce_field_for_front_end_new_post\'] )  &&
          wp_verify_nonce( $_POST[\'nonce_field_for_front_end_new_post\'], \'front_end_new_post\' ) ) return true;
        else return false;
    }
    function isFormValid() {
        //Check all mandatory fields are present.
        if ( trim( $_POST[\'postTitle\'] ) === \'\' ) {
            $error = \'Please enter a title.\';
            $hasError = true;
        } else if ( trim( $_POST[\'postContent\'] ) === \'\' ) {
            $error = \'Please enter the content.\';
            $hasError = true;
        } 
        //Check if any error was detected in validation.
        if($hasError == true) {
            echo $error;
            return false;
        }
        return true;
    }
   function createPost() {

        //Get the ID of currently logged in user to set as post author
        $current_user = wp_get_current_user();
        $currentuserid = $current_user->ID;
        //Get the details from the form which was posted
        $postTitle = $_POST[\'postTitle\'];
        $contentOfPost = $_POST[\'postContent\'] ;
        $postSatus = \'publish\'; // \'pending\' - in case you want to manually aprove all posts;
        //Create the post in WordPress
        $post_id = wp_insert_post( array(
                        \'post_title\'        => $postTitle,
                        \'post_content\'      => $contentOfPost,
                        \'post_status\'       => $postSatus , 
                        \'post_author\'       => $currentuserid

                    ));
    }
}
$wpSubmitFromFEObj = new WPSubmitFromFront();  

1 个回复
SO网友:Mark Kaplun

默认情况下,“无操作url”表示表单将发送到加载页面的url。完成后,评估短代码的代码将检查提交的表单并对其进行处理。

这不是编写自处理代码的好方法,因为对短代码的评估发生得太晚,无法执行成功重定向之类的操作。

将动作留空本身并不可怕,但您应该尽可能地明确。

相关推荐

即使以管理员身份也无法使用/wp-json/wp/v2/plugins API终结点

以管理员身份使用基本身份验证时,我得到一个错误代码401 Unauthorized : [rest_cannot_view_plugins] Sorry, you are not allowed to manage plugins for this site. 尝试访问GET时出错/wp-json/wp/v2/plugins 我的服务器的终结点。我可以毫无问题地获取帖子和页面信息,但当我查询插件时,我得到了401错误。我已经确认,API调用中使用的用户ID应该能够使用CLI工具管理插件:# wp use