Add the ability to post form content to a post type/custom post type
This would be a nice addition and can be achieved using wp_insert_post http://codex.wordpress.org/Function_Reference/wp_insert_post
Hi,
I did not really understand the idea, could you provide more info?
Sure.
So the reason I added this is because I'm wanting to create a guestbook style feature, which allows users to submit a message and have it create a custom post which goes to the drafts in WP admin for the admin to approve and show on the front end of the site. I thought this would be great by using the forms extension already built into Unyson.
Using wp_insert_post you can achieve this. You can set what kind of post it will create when the user submits the form, i.e. 'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] and you can set other parameters such as whether the post goes to draft, published etc, there's more info on the link in my first comment.
Hope that makes sense, let me know if you need any more details.
Hi,
I don't know I understood this correctly, what do you want, is to be able to create posts from data submitted from frontend.
I don't think this is hard to do, and also It doesn't require any special functionality.
Just use Unyson FW_Form to create frontend form and after submit create a post.
If you need some help about using the FW_Form or if I did not understand correctly the request, please, tell me.
That is correct, I'd like to create Custom Post Type posts from user submitted forms on the front end.
Hi,
This is very simple. You just need to create a small extension for this. Let's say you create a testimonials extensions, where users will be able to submit testimonials from frontend.
In framework-customizations/extensions create the testimonials folder.
First thing you need to define is the Testimonials extension manifest file: manifest.php
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
$manifest = array();
$manifest['name'] = __( 'Testimonials', 'fw' );
$manifest['description'] = __( '', 'fw' );
$manifest['version'] = '1.0.0';
$manifest['display'] = false;
$manifest['standalone'] = true;
Then the extension class: class-fw-extension-testimonials.php
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
class FW_Extension_Testimonials extends FW_Extension{
/**
* @var FW_Form
*/
private $form;
public function _init() {
$this->form = new FW_Form(
$this->get_name() . '-frontend-form',
array(
'render' => array( $this, '_render' ),
'validate' => array( $this, '_validate' ),
'save' => array( $this, '_save' ),
)
);
$this->register_post_type();
}
public function render() {
$this->form->render();
}
/**
* Register the custom post type using the 'register_post_type': https://codex.wordpress.org/Function_Reference/register_post_type
*/
private function register_post_type() {
//TODO: Register the custom post
}
/**
* @param array $data
*
* @return array
*/
public function _render( $data ) {
//Render the form view
echo $this->render_view('form', array());
return $data;
}
/**
* @param array $errors
*
* @return array
*/
public function _validate( $errors ) {
//TODO: Validate form
return $errors;
}
/**
*
* @param array $data
*
* @return array
*/
public function _save( $data ) {
//TODO: Create post using 'wp_insert_post': https://codex.wordpress.org/Function_Reference/wp_insert_post
return $data;
}
}
And then create the views folder, where you'll place the form.php file, this will the view of the form from forntend to submit the testimonial.
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
$testimonials = fw_ext('testimonials');
?>
<label><?php _e( 'Name', 'fw' ); ?>: <input type="text" name="<?php echo $testimonials->get_name(); ?>[name]"></label>
<br>
<label><?php _e( 'Email', 'fw' ); ?>: <input type="text" name="<?php echo $testimonials->get_name(); ?>[email]"></label>
<br>
<label><?php _e( 'Message', 'fw' ); ?>: <textarea name="<?php echo $testimonials->get_name(); ?>[message]"></textarea></label>
Now, in the extension class, you have to define the _validate method functionality, to validate the submitted data. Use FW_Request::POST() method to get the submitted data.
The $errors parameter represents and array where you'll add all errors, if the $errors is returnded empty, that means that the validations has passed successfully, and the form calls the _save method, wich you also have to define. In this method you'll insert the testimonial post, here also use the FW_Request::POST() to get the requested data.