Advertisement
firoze

Posting via the Front End in WORDPRESS

Apr 23rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. // Wordpress post from front end
  2.  
  3. // class this in functions.php
  4. // Posting via the Front End: Inserting
  5.  
  6. // validation
  7. wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
  8. wp_enqueue_script( 'validation' );
  9.  
  10.  
  11. function my_post_from_front_end(){
  12.    
  13. ?>
  14. <script type="text/javascript">
  15. jQuery(document).ready(function() {
  16.  
  17.     jQuery("#primaryPostForm").validate();
  18.  
  19. });
  20. </script>  
  21. <?php
  22. }
  23. add_action('wp_footer','my_post_from_front_end');
  24.  
  25. /*******************************************************************************************************************************/
  26.  
  27. //call the below code in your theme directory name as  template-insert-posts.php
  28.  
  29. <?php /* Template Name: Suggestions */
  30.  
  31. $postTitleError = '';
  32.  
  33. if(isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
  34.  
  35.     if(trim($_POST['postTitle']) === '') {
  36.         $postTitleError = 'Please enter a title.';
  37.         $hasError = true;
  38.     } else {
  39.         $postTitle = trim($_POST['postTitle']);
  40.     }
  41.  
  42.  
  43.     $post_information = array(
  44.         'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
  45.         'post_content' => esc_attr(strip_tags($_POST['postContent'])),
  46.         'meta_input' => array(
  47.             'country' => $_POST['country']
  48.             'city' => $_POST['city'],
  49.         ),
  50.         'post_type' => 'post',
  51.         'post_status' => 'publish' // pending
  52.     );
  53.  
  54.     $post_id = wp_insert_post($post_information);
  55.  
  56.     if($post_id)
  57.     {
  58.         wp_redirect(home_url());
  59.         exit;
  60.     }
  61.  
  62. }
  63.  
  64. ?>
  65.  
  66. <?php get_header(); ?>
  67.  
  68.  
  69.     <!-- #primary BEGIN -->
  70.     <div id="primary">
  71.  
  72.         <form action="" id="primaryPostForm" method="POST">
  73.  
  74.             <fieldset>
  75.  
  76.                 <label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>
  77.  
  78.                 <input type="text" name="postTitle" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />
  79.  
  80.             </fieldset>
  81.  
  82.             <?php if($postTitleError != '') { ?>
  83.                 <span class="error"><?php echo $postTitleError; ?></span>
  84.                 <div class="clearfix"></div>
  85.             <?php } ?>
  86.  
  87.             <fieldset>
  88.                        
  89.                 <label for="postContent"><?php _e('Post\'s Content:', 'framework') ?></label>
  90.  
  91.                 <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>
  92.  
  93.             </fieldset>
  94.  
  95.             <fieldset>
  96.  
  97.                 <label for="country"><?php _e('Country:', 'framework') ?></label>
  98.  
  99.                 <input type="text" name="country" id="country" value="<?php if(isset($_POST['country'])) echo $_POST['country'];?>" class="required" />
  100.  
  101.             </fieldset>
  102.  
  103.  
  104.             <fieldset>
  105.  
  106.                 <label for="city"><?php _e('City:', 'framework') ?></label>
  107.  
  108.                 <input type="text" name="city" id="city" value="<?php if(isset($_POST['city'])) echo $_POST['city'];?>" class="required" />
  109.  
  110.             </fieldset>
  111.  
  112.  
  113.             <fieldset>
  114.                
  115.                 <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
  116.  
  117.                 <input type="hidden" name="submitted" id="submitted" value="true" />
  118.                 <button type="submit"><?php _e('Add Post', 'framework') ?></button>
  119.  
  120.             </fieldset>
  121.  
  122.         </form>
  123.  
  124.     </div><!-- #primary END -->
  125.  
  126.  
  127. <?php get_footer(); ?>
  128.  
  129. /***********************************************************************************************/
  130. // Displaying Custom Post Meta (meta_input) in WordPress
  131. <?php echo get_post_meta( get_the_ID(), 'country', true ); ?> // here country is custom field name
  132. <?php echo get_post_meta( get_the_ID(), 'country', true ); ?> // here cityis custom field name
  133.  
  134.  
  135. // usages guide
  136. // http://wpninjas.com/displaying-custom-post-meta/
  137. // https://developer.wordpress.org/reference/functions/get_post_meta/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement