WordPress custom post type add form in frontend for users

WordPress have got nice popular features of custom post type. Sometimes most of us requires to create a form inside frontend site where customers can put in post. example inside news/article site there ought to be a page in which a user can easily submit news/article by way of form.
These all data goes toward our customized post variety with customized taxonomy along with custom areas. Below i’ve got decribed coming from begening about
– making Custom Post Type my spouse and i. e ‘new’ (plural title ‘news’)
: create customized taxonomy my spouse and i. e ‘news-category’ along with ‘state’
: create customized field ‘news-image’.

We have created any post form ‘new’ (slug brand ‘new’, singular brand ‘news’) by using types extensions as viewed below screenshotcustom_post_type
Then created image fields as shown in screenshot
custom_post_type_field
I have also created 2 custom taxonomy ‘state’ and ‘news category’ as shown in screenshot
custom_taxonomy

Now let’s create a page template for ‘News Add’

I have created a php file ‘news_add.php’ and commneted inside the page

After accessing admin panel, create a page ‘News Add’ and select ‘News add’ with ‘Template’ combination box came out on proper side connected with page. Now we are able to access the actual page through ‘news-add’ after your website home url. in my own contest
http: //localhost/batch/b107wp/news-add/

Now i’ve got called header footer and a few div for that template structure. After created form on the inside this and processed seeing that shown down below code: We’ve mentioned some function ref. inbound links for details overviews.

view plaincopy to clipboardprint?

$title,
‘post_content’ => $body,
‘post_type’ =>’new’,
‘post_status’ => ‘publish’,
‘post_author’ => 1
);

// Insert the post into the database ref.https://codex.wordpress.org/Function_Reference/wp_insert_post
$post_id = wp_insert_post( $post, $wp_error=” );

if($post_id!=0){

///upload image ref. https://codex.wordpress.org/Function_Reference/add_post_meta

///ref. https://codex.wordpress.org/Function_Reference/wp_handle_upload
if ( ! function_exists( ‘wp_handle_upload’ ) ) require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
$uploadedfile = $_FILES[‘image’];
$upload_overrides = array( ‘test_form’ => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
// echo “File is valid, and was successfully uploaded.\n”;
// var_dump( $movefile);
} else {
echo “Possible file upload attack!\n”;
}

add_post_meta($post_id, ‘wpcf-news-image’, $movefile[‘url’] );

///////////////////////////////////////////////////////////

//insert taxonomy terms
//ref. https://codex.wordpress.org/Function_Reference/wp_set_object_terms

////for taxonomy ‘news-category’//////////////////////////////
$cat_ids = array($news_category);

$cat_ids = array_map( ‘intval’, $cat_ids );
$cat_ids = array_unique( $cat_ids );

$term_taxonomy_ids = wp_set_object_terms($post_id, $cat_ids, ‘news-category’ );

/////////////End for taxonomy ‘news-category’//////////////////////////////

////for taxonomy ‘st’//////////////////////////////
$cat_ids2 = array($state);

$cat_ids2 = array_map( ‘intval’, $cat_ids2 );
$cat_ids2 = array_unique( $cat_ids2 );

$term_taxonomy_ids2 = wp_set_object_terms($post_id, $cat_ids2, ‘st’ );

/////////////End for taxonomy ‘news-category’//////////////////////////////

echo “New successfully added”;
}

}

?>

Add News

Name
Description
Upload Image
State <!–?php
//ref. https://codex.wordpress.org/Function_Reference/get_terms
$terms2 = get_terms("st",'order_by=count&hide_empty=0');
if ( !emptyempty( $terms2 ) && !is_wp_error( $terms2 ) ){
echo "”;
echo “Select”;
foreach ( $terms2 as $term2 ) {
echo “term_id.””>” . $term2->name . “”;}
echo “”;
}
?>
Category <!–?php
//ref. https://codex.wordpress.org/Function_Reference/get_terms
$terms = get_terms("news-category",'order_by=count&hide_empty=0');
if ( !emptyempty( $terms ) && !is_wp_error( $terms ) ){
echo "”;
echo ”

Select”;

foreach ( $terms as $term ) {
echo ”

term_id.””>” . $term->name . “”;

}
echo “”;
}
?>


Download this wordpress theme ‘news’

I think this tutorial will guide you understanding wordpress custom post type add form .

If you looking advance training visit our site WordPress training
or
call me +91 99999 680 96

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s