Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

This blog is part of 1 Tute Sets.

Wordpress Plugin to Get all Category in Dropdown
  • Create wordpress Plugin to Get all Category in Dropdown: Part 2

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 207
    Comment on it

    Hello readers,

    In my previous blog, we talked about "How to Get All Category in Dropdown". Means that it will help you to get all category in dropdown and if you select any category from dropdown you get all related post

     

    Now, I have made some modification on this blog and added some new feature like you have the authority to edit any post and delete a post which is shown in a tabular form. There are some changes in the code like create a new sub-menu page and added some Javascript to my previous code. Below is the code.

     

    Add Custom Menu on admin panel

     

    //add new menu in the admin dashboard area
    add_action('admin_menu', 'getCategory');
    function getCategory() {   
        add_menu_page('get_all_category', 'Get Categories', 'administrator', 'get_all_category', 'get_all_category', 'dashicons-admin-generic', '21.2');
    
        add_submenu_page(null, 'Edit Post', 'Edit Post', 'administrator', 'edit-post', 'editPost');
      
    }

     

    I have also created a function to call sub menu.

     

    // call sub menu page
    function editPost() {
        require_once('edit-post.php');
    }

     

    Now, if you want to get all posts of a particular category then use below code, it will fetch all the related post of the category you select. In the below code there are some new change like edit and delete functionality and add delete script, with the help of this script it will ask you "Do you want to delete this." in a Popup Box.

     

    // this function return all the post related to a particular category.
    function list_post($id){
        $args = array(
            'post_type' => 'post',
            'posts_per_page'=>-1,
            'tax_query' => array(
                array(
                'taxonomy' => 'category',
                'terms' => $id
                 )
              )
            );
        $query = new WP_Query( $args );
    
        echo '<table border="1" width="70%" align="center">';
        echo '<tr>';
        echo '<th>S No.</th>';
        echo '<th>Title</th>';
        echo '<th>Content</th>';
        echo '<th>Action</th>';
        echo '</tr>';
        $j=1;
        if($query->have_posts()){
            while ($query->have_posts()) {
                $query->the_post();
                $post_id = get_the_ID();
                $edit_link = admin_url("admin.php?page=edit-post&amp;&amp;post_id=$post_id");
                $delete_link = admin_url("admin.php?page=get_all_category&amp;action=delete&amp;post_id={$post_id}")
        ?>
            <tr>
                <td width="10%" align="center"><?=$j;?></td>
                <td width="20%" align="center"><?=the_title();?></td>
                <td width="60%" align="center"><?=the_content();?></td>
                <td width="10%" align="center">
                    <a href="<?=$edit_link;?>">Edit</a> | 
                    <a href="javascript:void(0)" onclick="deletePost('<?=$delete_link; ?>')">Delete</a>
                </td>
            </tr>
        <?php $j++;
                }
            }
        ?>
        </table>
    <?php
    }
    $action = $_GET['action'];
    $delete_id = $_GET['post_id'];
    if(isset($action)){
        $wpdb->query("Delete from {$wpdb->prefix}posts where ID =".$delete_id);
    }
    
    ?>
    
    <script type="text/javascript">
    /**
     * Confirm popup box to delete.
     */
    function deletePost(url,type) {
        var cmt = confirm('Do you want to delete this.');
        if (cmt == true)    
            parent.location.href = url;
        else
            return false;
    }
    </script>

     

    Now, the last step is to create a new file into your Plugin folder 'edit-post.php' and write the edit functionality. You can use the below code for edit a particular post.

     

    <?php
    	$id = $_GET['post_id'];
    	edit_post_data($id);
    
    	function edit_post_data($post_id){
    		require_once( ABSPATH . '/wp-config.php' );
    		global $wpdb;
    		$data = get_post($post_id);
    
    		if (isset($_POST['btnupdate'])) {
    			$modified	=	date('Y-m-d H:i:s');
    
    			$newtitle = $_POST['post_title'];
    			$newcontent = $_POST['post_content'];
    			$newstatus = $_POST['post_status'];
    
    			$wpdb->update( $wpdb->posts, array( 'post_title'=>$newtitle, 'post_content'=>$newcontent, 'post_status'=>$newstatus ), array( 'ID' => $post_id ) );
    		}
    ?>
    	<form method="post" action="<?php echo admin_url("admin.php?page=edit-post&post_id=$post_id"); ?>">
    		<table width="50%" align="center">
    			<h1>Post Detail</h1>
    			<tr>
    				<td width="30%">Post Title</td>
    				<td width="70%"><input size="70" type="text" name="post_title" value="<?=$data->post_title;?>"/></td>
    			</tr>
    			<tr>
    				<td>Post Content</td>
    				<td><input size="70" type="text" name="post_content" value="<?=$data->post_content;?>"></td>
    			</tr>
    			<tr>
    				<td>Post Status</td>
    				<td><input size="70" type="text" name="post_status" value="<?=$data->post_status;?>"></td>
    			</tr>
    			<tr>
    				<td colspan="2" align="center"><input type="submit" name="btnupdate" id="btnupdate" value="Update" /></td>
    			</tr>
    		</table>
    	</form>
    <?php
    	}
    
    

     

    Output:-

    Post List:-

     

     

    Edit Post:-

     

     

    Delete Post:-

     

     

    That's it ! your Wordpress Plugin is created with edit and delete functionality.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: