0

So I'm still rather new to WordPress plugins. And there's one thing that I'm not sure of. I'm building a front-end form that allows users to post posts. In this guide down below there's a plugin made that allows users to do this. However, the "form" he builds doesn't have any 'action' like the form doesn't redirect the user anywhere.

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

I would have a form action redirect to a php page that launches the createPost(); function. Why doesn't this form have an action?

This form works without any action so...
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();?

And it doesn't write how to include the plugin file on the form-page. Or does it get included because of the WP-short code?

Here's the plugin's full code in github:
https://github.com/abbassoftware/SubmitFromFront/blob/master/submitfromfront.php

And here's the raw full code:

<?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();  
  • please edit you question and include the code in it, not many are likely to waste time opening external links to look for some information. All information that is relevant to the question should be included. Well, at least if you want a better chance in getting an answer ;) – Mark Kaplun Apr 17 '16 at 5:23
  • Cheers! I added it! Do you think you can know why the form has no action ? @MarkKaplun – mesqueeb Apr 17 '16 at 5:25
0

By default no action url will mean that the form will be sent to the URL the page was loaded from. Once it is done, the code evaluating the shortcode is checking in the form was submitted and process it.

This is not not a great way to write a from processing code, as the evaluation of shortcodes happens too late for doing something like redirect on success.

Leaving the action empty is not horrible by itself but you should try to be as explicit as possible whenever you can.

  • I'm sorry I'm still confused. If the form redirects to the same page, how can this plugin know that it should fire this function: createPost();? – mesqueeb Apr 17 '16 at 5:38
  • The createPost() is called because the page that contains the form has the POST_FROM_FRONT shortcode. This shortcode handles the displaying of the form as well as the createPost. – czerspalace Apr 17 '16 at 6:12
  • Dear @czerspalace I have a custom PHP page in my wordpress theme with a form already written. I'm just seeing how I should add the actual sending of the post. Would it be best to add it as a PHP file and make the action go there, or add it as a plugin and add it as a shortcode? – mesqueeb Apr 17 '16 at 6:27
  • One option is to use a shortcode to display the form, and then use one of the WordPress hooks, such as template_redirect to handle the form processing. Within the template_redirect callback function I would handle any form processing you need to take care of. – czerspalace Apr 17 '16 at 6:54
  • The problem is, I already have my form on my php page directly. I just want to create a plugin that add's the form sending functionality, but I don't know how. I can't use shortcodes because I don't have any HTML I want to paste. – mesqueeb Apr 17 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.