Mobile-Menu iFuturz Infoweb Inc. Contact Portfolio

24

Aug

Add Edit and Delete Posts from Front-End in WordPress

Add Edit and Delete Posts from Front-End in WordPress

Posted On : August 24, 2013

| No Comment

Create these three template files in your directory

1. template-view-posts.php
2. template-insert-posts.php
3. template-edit-posts.php

Now create three different pages from admin panel, Insert page, View page and Edit Page. Assign template-insert-posts.php to Insert page, template-view-posts.php to View page and template-edit-posts.php to Edit Page and Set status pending to Edit page.

Put this code in respective files :

template-view-posts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php /* Template Name: View Posts */ ?>
<?php get_header(); ?>
	<!-- #primary BEGIN -->
	<div id="primary">
		<table>
			<tr>
				<th>Post Title</th>
				<th>Post Excerpt</th>
				<th>Post Status</th>
				<th>Actions</th>
			</tr>
			<?php $query = new WP_Query(array('post_type' => 'post', 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft', 'private', 'trash') ) ); ?>
			<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
			<tr>
				<td><?php echo get_the_title(); ?></td>
				<td><?php the_excerpt(); ?></td>
				<td><?php echo get_post_status( get_the_ID() ) ?></td>
				<?php $edit_post = add_query_arg('post', get_the_ID(), get_permalink(1129 + $_POST['_wp_http_referer'])); ?>
//replace 1129 by your edit page id
				<td>
					<a href="<?php echo $edit_post; ?>">Edit</a>
					<?php if( !(get_post_status() == 'trash') ) : ?>
						<a onclick="return confirm('Are you sure you wish to delete post: <?php echo get_the_title() ?>?')"href="<?php echo get_delete_post_link( get_the_ID() ); ?>">Delete</a>
					<?php endif; ?>
				</td>
			</tr>
		<?php endwhile; endif; ?>
		</table>
	</div><!-- #primary END -->
<?php get_footer(); ?>

template-insert-posts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php /* Template Name: Insert Posts */
$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'])),
		'post-type' => 'post',
		'post_status' => '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>		
				<?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(); ?>

template-edit-posts.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php /* Template Name: Edit Posts */ 
$query = new WP_Query(array('post_type' => 'post', 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft', 'private', 'trash') ) );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();	
	if(isset($_GET['post'])) {		
		if($_GET['post'] == $post->ID)
		{
			$current_post = $post->ID;
 
			$title = get_the_title();
			$content = get_the_content();
		}
	}
endwhile; endif;
wp_reset_query();
global $current_post;
$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(
		'ID' => $current_post,
		'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
		'post_content' => esc_attr(strip_tags($_POST['postContent'])),
		'post-type' => 'post',
		'post_status' => 'pending'
	);
	$post_id = wp_update_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 echo $title; ?>" 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 echo $content; ?></textarea>
			</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('Update Post', 'framework') ?></button>
			</fieldset>
		</form>
	</div><!-- #primary END -->
<?php get_footer(); ?>

After following these, put this code in your function.php file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function register_menu() {
		register_nav_menu('navigation', __('Primary Menu'));
	}
	add_action('init', 'register_menu');
 
	function vsip_excerpt_length($length) {
	return 25; }
 
	add_filter('excerpt_length', 'vsip_excerpt_length');
 
	function vsip_excerpt_more($excerpt) {
		return str_replace('[...]', '...', $excerpt); }
	add_filter('wp_trim_excerpt', 'vsip_excerpt_more');
 
	function vsip_enqeue_scripts() 
	{
		// Register our scripts
 
		// comment out the next two lines to load the local copy of jQuery
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
 
		// validation
		wp_register_script('validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', 'jquery');	
 
		// Register our Custom JS Script
		wp_register_script('custom_js', get_template_directory_uri() . '/js/jquery.custom.js', 'jquery', '1.0', TRUE);
 
 
		// Enqueue our scripts
		wp_enqueue_script('jquery');
		wp_enqueue_script('validation');
		wp_enqueue_script('custom_js');
 
	}
	add_action('wp_enqueue_scripts', 'vsip_enqeue_scripts');
  • Tags:

Comment