Advertisement
terorama

WP Front-End posting

Dec 13th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.29 KB | None | 0 0
  1. <?php
  2. //-------------------------------------------------------------------
  3. //            posting via front-end
  4. //-------------------------------------------------------------------
  5.  
  6. //-------------------------------------
  7. //       functions.php
  8. //-------------------------------------
  9.  
  10.     /*-------------------------------*/
  11.     /*  Includes
  12.     /*-------------------------------*/
  13.  
  14.     include('include/post-meta.php');
  15.  
  16.     /*-------------------------------*/
  17.     /*  WP3.0+ Menus
  18.     /*-------------------------------*/
  19.  
  20.     function register_menu() {
  21.         register_nav_menu('navigation', __('Primary Menu'));
  22.     }
  23.         //------------------------------
  24.     add_action('init', 'register_menu');
  25.  
  26.  
  27.     /*-------------------------------*/
  28.     /*  Edit Excerpt
  29.     /*-------------------------------*/
  30.  
  31.     function vsip_excerpt_length($length) {
  32.     return 25; }
  33.         //------------------------------
  34.     add_filter('excerpt_length', 'vsip_excerpt_length');
  35.  
  36.         //------------------------------
  37.     function vsip_excerpt_more($excerpt) {
  38.         return str_replace('[...]', '...', $excerpt); }
  39.  
  40.         //------------------------------
  41.     add_filter('wp_trim_excerpt', 'vsip_excerpt_more');
  42.  
  43.  
  44.     /*-------------------------------*/
  45.     /*  Register JS
  46.     /*-------------------------------*/
  47.  
  48.     function vsip_enqeue_scripts()
  49.     {
  50.         wp_deregister_script('jquery');
  51.         wp_register_script('jquery',
  52.                    'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
  53.  
  54.        
  55.         wp_register_script('validation',
  56.                  'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js',
  57.                   'jquery');   
  58.    
  59.         wp_register_script('custom_js', get_template_directory_uri() .
  60.                      '/js/jquery.custom.js', 'jquery', '1.0', TRUE);
  61.  
  62.         wp_enqueue_script('jquery');
  63.         wp_enqueue_script('validation');
  64.         wp_enqueue_script('custom_js');
  65.  
  66.     }
  67.  
  68.         //---------------------------------------
  69.     add_action('wp_enqueue_scripts', 'vsip_enqeue_scripts');
  70.  
  71.     /*-------------------------------*/
  72.     /*  Browser Detection
  73.     /*-------------------------------*/
  74.  
  75.     function browser_body_class($classes) {
  76.  
  77.         global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari,
  78.                        $is_chrome, $is_iphone;
  79.  
  80.         if($is_lynx) $classes[] = 'lynx';
  81.         elseif($is_gecko) $classes[] = 'gecko';
  82.         elseif($is_opera) $classes[] = 'opera';
  83.         elseif($is_NS4) $classes[] = 'ns4';
  84.         elseif($is_safari) $classes[] = 'safari';
  85.         elseif($is_chrome) $classes[] = 'chrome';
  86.         elseif($is_IE) $classes[] = 'ie';
  87.         else $classes[] = 'unknown';
  88.  
  89.         if($is_iphone) $classes[] = 'iphone';
  90.  
  91.         return $classes;
  92.  
  93.     }
  94.         //-------------------------------------
  95.     add_filter('body_class','browser_body_class');
  96. ?>
  97.  
  98. <?php
  99. //--------------------------------------------------
  100. //           include/post-meta.php
  101. //--------------------------------------------------
  102.  
  103.     //-----------------------
  104.     add_action( 'add_meta_boxes',
  105.                                 'vsip_project_add_meta');
  106.     add_action('save_post',
  107.                                 'vsip_post_save_data');
  108.         //-----------------------
  109.     $prefix = 'vsip_';
  110.  
  111.         //-------------------------
  112.     $vsip_post_meta_box = array(
  113.         //-------------------------
  114.         'id' => 'vsip-post-meta-box',
  115.         'title' => __('Custom Meta', 'framework'),
  116.         'page' => 'post',
  117.         'context' => 'normal',
  118.         'priority' => 'high',
  119.         'fields' => array(
  120.             array(
  121.                 'name' => __('Custom Input One: ', 'framework'),
  122.                 'desc' => __('Enter your custom meta 1', 'framework'),
  123.                 'id' => $prefix.'custom_one',
  124.                 'type' => 'text'
  125.             ),
  126.             array(
  127.                 'name' => __('Custom Input Two: ', 'framework'),
  128.                 'desc' => __('Enter your custom meta 2', 'framework'),
  129.                 'id' => $prefix.'custom_two',
  130.                 'type' => 'text'
  131.             ),
  132.         )
  133.     );
  134.  
  135.  
  136.         //------------------------------------------
  137.     //            Custom Meta Box
  138.         //------------------------------------------
  139.     add_action( 'add_meta_boxes',
  140.                                     'vsip_project_add_meta');
  141.  
  142.         //-----------------------------------
  143.     function vsip_project_add_meta()
  144.         //-----------------------------------
  145.     {
  146.         global $vsip_post_meta_box;
  147.  
  148.         add_meta_box(  $vsip_post_meta_box['id'],
  149.                                $vsip_post_meta_box['title'],
  150.                                //---------------------------
  151.                                'vsip_display_post_meta',
  152.                                //---------------------------
  153.                                $vsip_post_meta_box['page'],
  154.                                $vsip_post_meta_box['context'],
  155.                                $vsip_post_meta_box['priority']);
  156.  
  157.     }
  158.  
  159.         //-----------------------------------
  160.     function vsip_display_post_meta()
  161.         //-----------------------------------
  162.     {
  163.         global $vsip_post_meta_box, $post;
  164.    
  165.         echo '<input type="hidden" name="vsip_meta_box_nonce" value="',
  166.                           wp_create_nonce(basename(__FILE__)), '" />';
  167.  
  168.         echo '<table class="form-table">';
  169.                    //------------------------------------------------
  170.            foreach ($vsip_post_meta_box['fields'] as $field)
  171.                    //------------------------------------------------
  172.               {
  173.                  $meta = get_post_meta($post->ID, $field['id'], true);
  174.  
  175.                 switch($field['type'])
  176.                 {
  177.                 case 'text':
  178.                    
  179.                    echo '<tr style="border-top:1px solid #eeeeee;">',
  180.                        '<th style="width:25%"><label for="', $field['id'], '"><strong>',
  181.                                     $field['name'],
  182.                                     '</strong>'.
  183.                                     '<span style=" display:block; '.
  184.                                     'color:#999; line-height: 20px; margin:5px 0 0 0;">'.
  185.                                     $field['desc'].'</span></label></th><td>';
  186.  
  187.                    echo '<input type="text" name="',
  188.                                      $field['id'],
  189.                                      '" id="', $field['id'], '" value="',
  190.  
  191.                                      $meta ? $meta :
  192.                                      stripslashes(htmlspecialchars(( $field['std']),ENT_QUOTES)),
  193.                                '" size="30" style="width:75%; margin-right: 20px; float:left;" />','</td>';
  194.                    
  195.                  break;
  196.  
  197.                 }
  198.  
  199.             }
  200.  
  201.         echo '</table>';
  202.     }
  203.  
  204.    
  205.         //--------------------------------------
  206.     function vsip_post_save_data($post_id)
  207.         //--------------------------------------
  208.     {
  209.         global $vsip_post_meta_box;
  210.        
  211.        
  212.         if (!isset($_POST['vsip_meta_box_nonce']) ||
  213.                 !wp_verify_nonce($_POST['vsip_meta_box_nonce'], basename(__FILE__))) {
  214.             return $post_id;
  215.         }
  216.        
  217.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  218.             return $post_id;
  219.         }
  220.        
  221.         if ('page' == $_POST['post_type']) {
  222.             if (!current_user_can('edit_page', $post_id)) {
  223.                 return $post_id;
  224.             }
  225.         } elseif (!current_user_can('edit_post', $post_id)) {
  226.             return $post_id;
  227.         }
  228.      
  229.                 //------------------------------------------------
  230.         foreach ($vsip_post_meta_box['fields'] as $field)
  231.                 //------------------------------------------------
  232.         {
  233.  
  234.             $old = get_post_meta($post_id, $field['id'], true);
  235.             $new = $_POST[$field['id']];
  236.      
  237.             if ($new && $new != $old) {
  238.                 update_post_meta($post_id, $field['id'], $new);
  239.             } elseif ('' == $new && $old) {
  240.                 delete_post_meta($post_id, $field['id'], $old);
  241.             }
  242.         }
  243.     }
  244.  
  245.         //--------------------------------------
  246.     function vsip_project_scripts()
  247.         //--------------------------------------
  248.     {
  249.  
  250.         wp_enqueue_script('media-upload');
  251.         wp_enqueue_script('thickbox');
  252.  
  253.     }
  254.  
  255.         //--------------------------------------
  256.     function vsip_project_styles()
  257.         //--------------------------------------
  258.     {
  259.         wp_enqueue_style('thickbox');
  260.  
  261.     }
  262.  
  263.         //--------------------------------------
  264.     add_action('admin_print_scripts', 'vsip_project_scripts');
  265.     add_action('admin_print_styles', 'vsip_project_styles');
  266.         //--------------------------------------
  267.  
  268.  
  269. //--------------------------------------------------
  270. //            template-view-posts.php
  271. //--------------------------------------------------
  272. ?>
  273. <?php /* Template Name: View Posts */ ?>
  274.  
  275. <?php get_header(); ?>
  276.  
  277.     <div id="primary">
  278.         <?php if(isset($_GET['result'])) : ?>
  279.             <?php if($_GET['result'] == 'success') : ?>
  280.                 <div class="client_success">
  281.                     <span class="success">Successfully Added
  282.                                         <span class="cross"><a href="#">X</a></span></span>
  283.                 </div>
  284.             <?php endif; ?>
  285.         <?php endif; ?>
  286.  
  287.         <table>
  288.             <tr>
  289.                 <th>Post Title</th>
  290.                 <th>Post Excerpt</th>
  291.                 <th>Post Status</th>
  292.                 <th>Actions</th>
  293.             </tr>
  294.  
  295.             <?php $query = new WP_Query(array('post_type' => 'post',
  296.                                          'posts_per_page' =>'-1',
  297.                                          'post_status' =>
  298.                                           array(
  299.                                             'publish', 'pending', 'draft', 'private', 'trash') ) ); ?>
  300.  
  301.             <?php if ($query->have_posts()) :
  302.                               while ($query->have_posts()) : $query->the_post(); ?>
  303.  
  304.             <tr>
  305.                 <td><?php echo get_the_title(); ?></td>
  306.                 <td><?php the_excerpt(); ?></td>
  307.                 <td><?php echo get_post_status( get_the_ID() ) ?></td>
  308.  
  309.                 <?php $edit_post = add_query_arg('post', get_the_ID(),
  310.                                                 get_permalink(61 + $_POST['_wp_http_referer'])); ?>
  311.  
  312.                 <td>
  313.                     <a href="<?php echo $edit_post; ?>">Edit</a>
  314.  
  315.                 <?php if( !(get_post_status() == 'trash') ) : ?>
  316.                     <a onclick="return confirm('Are you sure you wish to delete post:
  317.                                     <?php echo get_the_title()
  318.                                      ?>?')"href="<?php echo get_delete_post_link( get_the_ID() );
  319.                                      ?>">Delete</a>
  320.  
  321.                 <?php endif; ?>
  322.                 </td>
  323.             </tr>
  324.         <?php endwhile; endif; ?>
  325.         </table>
  326.     </div>
  327.  
  328.  
  329. <?php get_footer(); ?>
  330.  
  331. <?php
  332. //--------------------------------------------------
  333. //          template-insert-posts.php
  334. //--------------------------------------------------
  335. ?>
  336.  
  337. <?php /* Template Name: Insert Posts */
  338.  
  339. $postTitleError = '';
  340.  
  341. if(isset($_POST['submitted']) &&
  342.       isset($_POST['post_nonce_field']) &&
  343.          wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
  344.  
  345.     if(trim($_POST['postTitle']) === '') {
  346.         $postTitleError = 'Please enter a title.';
  347.         $hasError = true;
  348.     } else {
  349.         $postTitle = trim($_POST['postTitle']);
  350.     }
  351.  
  352.     $post_information = array(
  353.         'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
  354.         'post_content' => esc_attr(strip_tags($_POST['postContent'])),
  355.         'post-type' => 'post',
  356.         'post_status' => 'pending'
  357.     );
  358.  
  359.     $post_id = wp_insert_post($post_information);
  360.  
  361.     if($post_id)
  362.     {
  363.        update_post_meta($post_id, 'vsip_custom_one',
  364.                                         esc_attr(strip_tags($_POST['customMetaOne'])));
  365.  
  366.        update_post_meta($post_id, 'vsip_custom_two',
  367.                                         esc_attr(strip_tags($_POST['customMetaTwo'])));
  368.  
  369.        wp_redirect( home_url() ); exit;
  370.     }
  371.  
  372. }
  373. //--------------------------------------
  374. ?>
  375.  
  376. <?php get_header(); ?>
  377.  
  378.     <div id="primary">
  379.         <form action="" id="primaryPostForm" method="POST">
  380.             <fieldset>
  381.                 <label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?>
  382.                                 </label>
  383.  
  384.                 <input type="text" name="postTitle" id="postTitle" value="<?php
  385.                                     if(isset($_POST['postTitle'])) echo $_POST['postTitle'];
  386.                                  ?>" class="required" />
  387.             </fieldset>
  388.  
  389.             <?php if($postTitleError != '') { ?>
  390.                 <span class="error"><?php echo $postTitleError; ?></span>
  391.                 <div class="clearfix"></div>
  392.             <?php } ?>
  393.  
  394.             <fieldset>             
  395.                 <label for="postContent"><?php _e('Post\'s Content:', 'framework') ?>
  396.                                 </label>
  397.  
  398.                 <textarea name="postContent" id="postContent" rows="8" cols="30"><?php
  399.  
  400.                                    if(isset($_POST['postContent'])) {
  401.                                        if(function_exists('stripslashes')) {
  402.                                            echo stripslashes($_POST['postContent']); }
  403.                                          else {
  404.                                             echo $_POST['postContent']; } } ?>
  405.                                  </textarea>
  406.             </fieldset>
  407.  
  408.             <fieldset>
  409.                 <label for="customMetaOne"><?php _e('Custom Meta One:', 'framework') ?>
  410.                                 </label>
  411.                 <input type="text" name="customMetaOne" id="customMetaOne" value="<?php
  412.                                    if(isset($_POST['customMetaOne'])) echo $_POST['customMetaOne'];?>" />
  413.             </fieldset>
  414.  
  415.             <fieldset>
  416.                 <label for="customMetaTwo"><?php _e('Custom Meta Two:', 'framework') ?>
  417.                                  </label>
  418.                 <input type="text" name="customMetaTwo" id="customMetaTwo" value="<?php
  419.                                   if(isset($_POST['customMetaTwo'])) echo $_POST['customMetaTwo'];?>" />
  420.             </fieldset>
  421.  
  422.             <fieldset> 
  423.                 <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
  424.                 <input type="hidden" name="submitted" id="submitted" value="true" />
  425.                 <button type="submit"><?php _e('Add Post', 'framework') ?></button>
  426.  
  427.             </fieldset>
  428.         </form>
  429.  
  430.     </div>
  431.  
  432.  
  433. <?php get_footer(); ?>
  434.  
  435. <?php
  436. //--------------------------------------------------
  437. //             template-edit-posts.php
  438. //--------------------------------------------------
  439. ?>
  440.  
  441. <?php /* Template Name: Edit Posts */
  442. //----------------------------------------
  443. $query = new WP_Query(array('post_type' => 'post',
  444.                             'posts_per_page' =>'-1',
  445.                             'post_status' =>
  446.                                 array('publish','pending', 'draft', 'private', 'trash') ) );
  447.  
  448. //----------------------------------------
  449. if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
  450.    
  451.     if(isset($_GET['post'])) {
  452.         if($_GET['post'] == $post->ID)
  453.         {
  454.             $current_post = $post->ID;
  455.  
  456.             $title = get_the_title();
  457.             $content = get_the_content();
  458.  
  459.             $custom_one = get_post_meta($current_post, 'vsip_custom_one', true);
  460.             $custom_two = get_post_meta($current_post, 'vsip_custom_two', true);
  461.         }
  462.     }
  463.  
  464. endwhile; endif;
  465. //----------------------------------------
  466. wp_reset_query();
  467. //----------------------------------------
  468.  
  469. global $current_post;
  470.  
  471. $postTitleError = '';
  472.  
  473. //----------------------------------------
  474. if (isset($_POST['submitted']) &&
  475.             isset($_POST['post_nonce_field']) &&
  476.                  wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
  477. //----------------------------------------
  478.     if(trim($_POST['postTitle']) === '') {
  479.         $postTitleError = 'Please enter a title.';
  480.         $hasError = true;
  481.     } else {
  482.         $postTitle = trim($_POST['postTitle']);
  483.     }
  484.         //----------------------------------------
  485.     $post_information = array(
  486.         'ID' => $current_post,
  487.         'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
  488.         'post_content' => esc_attr(strip_tags($_POST['postContent'])),
  489.         'post-type' => 'post',
  490.         'post_status' => 'pending'
  491.     );
  492.         //----------------------------------------
  493.     $post_id = wp_update_post($post_information);
  494.         //----------------------------------------
  495.     if($post_id)
  496.     {      
  497.         update_post_meta($post_id, 'vsip_custom_one',
  498.                                esc_attr(strip_tags($_POST['customMetaOne'])));
  499.  
  500.         update_post_meta($post_id, 'vsip_custom_two',
  501.                                esc_attr(strip_tags($_POST['customMetaTwo'])));
  502.  
  503.         wp_redirect( home_url() ); exit;
  504.     }
  505.  
  506. }
  507. //----------------------------------------
  508.  get_header(); ?>
  509.  
  510.     <div id="primary">
  511.         <form action="" id="primaryPostForm" method="POST">
  512.  
  513.             <fieldset>
  514.                 <label for="postTitle"><?php _e('Post\'s Title:', 'framework') ?></label>
  515.                 <input type="text" name="postTitle" id="postTitle" value="<?php
  516.                                 echo $title; ?>" class="required" />
  517.             </fieldset>
  518.  
  519.             <?php if($postTitleError != '') { ?>
  520.                 <span class="error"><?php echo $postTitleError; ?></span>
  521.                 <div class="clearfix"></div>
  522.             <?php } ?>
  523.  
  524.             <fieldset>         
  525.                 <label for="postContent"><?php _e('Post\'s Content:', 'framework') ?>
  526.                                 </label>
  527.                 <textarea name="postContent" id="postContent" rows="8" cols="30"><?php
  528.                                      echo $content; ?></textarea>
  529.             </fieldset>
  530.  
  531.             <fieldset>
  532.                 <label for="customMetaOne"><?php _e('Custom Meta One:', 'framework')
  533.                                 ?></label>
  534.                 <input type="text" name="customMetaOne" id="customMetaOne" value="<?php
  535.                                  echo $custom_one; ?>" />
  536.             </fieldset>
  537.  
  538.             <fieldset>
  539.                 <label for="customMetaTwo"><?php _e('Custom Meta Two:', 'framework')
  540.                                  ?></label>
  541.                 <input type="text" name="customMetaTwo" id="customMetaTwo" value="<?php
  542.                                 echo $custom_two; ?>" />
  543.             </fieldset>
  544.  
  545.             <fieldset>
  546.                 <?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
  547.                 <input type="hidden" name="submitted" id="submitted" value="true" />
  548.                 <button type="submit"><?php _e('Update Post', 'framework') ?></button>
  549.             </fieldset>
  550.         </form>
  551.     </div>
  552. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement