Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wordpress post from front end
- // class this in functions.php
- // Posting via the Front End: Inserting
- // validation
- wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
- wp_enqueue_script( 'validation' );
- function my_post_from_front_end(){
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function() {
- jQuery("#primaryPostForm").validate();
- });
- </script>
- <?php
- }
- add_action('wp_footer','my_post_from_front_end');
- /*******************************************************************************************************************************/
- //call the below code in your theme directory name as template-insert-posts.php
- <?php /* Template Name: Suggestions */
- $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(
- 'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
- 'post_content' => esc_attr(strip_tags($_POST['postContent'])),
- 'meta_input' => array(
- 'country' => $_POST['country']
- 'city' => $_POST['city'],
- ),
- 'post_type' => 'post',
- 'post_status' => 'publish' // pending
- );
- $post_id = wp_insert_post($post_information);
- if($post_id)
- {
- wp_redirect(home_url());
- exit;
- }
- }
- ?>
- <?php get_header(); ?>
- <!-- #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 if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" 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 if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>
- </fieldset>
- <fieldset>
- <label for="country"><?php _e('Country:', 'framework') ?></label>
- <input type="text" name="country" id="country" value="<?php if(isset($_POST['country'])) echo $_POST['country'];?>" class="required" />
- </fieldset>
- <fieldset>
- <label for="city"><?php _e('City:', 'framework') ?></label>
- <input type="text" name="city" id="city" value="<?php if(isset($_POST['city'])) echo $_POST['city'];?>" class="required" />
- </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('Add Post', 'framework') ?></button>
- </fieldset>
- </form>
- </div><!-- #primary END -->
- <?php get_footer(); ?>
- /***********************************************************************************************/
- // Displaying Custom Post Meta (meta_input) in WordPress
- <?php echo get_post_meta( get_the_ID(), 'country', true ); ?> // here country is custom field name
- <?php echo get_post_meta( get_the_ID(), 'country', true ); ?> // here cityis custom field name
- // usages guide
- // http://wpninjas.com/displaying-custom-post-meta/
- // https://developer.wordpress.org/reference/functions/get_post_meta/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement