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- Get All Category in Dropdown

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 447
    Comment on it

    Hello readers, In this tutorial I will guide you to create Wordpress Plugin for "Get all Category in Dropdown".

    For creating Wordpress Plugin, we need to do some basic things for creating a Plugin.

    1) Create Wordpress Plugin, just think what your Plugin will do.


    2) Choose a unique name for your Plugin. If you confuse to choose the Plugin name, go to Google and search is Plugin exists or not. In case a Plugin already exists, change your Plugin name.


    3) Create a file inside the Plugin folder and the name of your file is derived from your Plugin name.
        Example:-
            3.1) Plugin Name:- Export-post-data
            3.1) File Name:- expoet_post_data.php


    4) You can put all the code into one file or you can split your code into multiple files. Your Plugin must have one PHP file.


    5) Your Plugin also contains CSS, js, and images files.


    6) Also, your Plugin must contain One .txt file. This .txt file contains information about your plugin, like Plugin Name, Plugin URI, Description,
    Author Name, Version, Author URI.

     

    In mine code first, we can create a file and put Plugin basic information in comments, which we show below code

    1. /*
    2. Plugin Name: Get Category in Dropdown
    3. Plugin URI: evontech.com
    4. Description: Get Category in Dropdown and when we select a category fetch all relative post.
    5. Author: Nitish Rawat
    6. Version: 1.0
    7. Author URI: evontech.com
    8. */

     

    Now, Add Custom Menu  on admin panel

    1. //add new menu in the admin dashboard area
    2. add_action('admin_menu', 'getCategory');
    3. function getCategory() {
    4. add_menu_page('get_all_category', 'Get Categories', 'administrator', 'get_all_category', 'get_all_category', 'dashicons-admin-generic', '21.2');
    5. }

     

    Get all category from post in dropdown

    1. // get all category from post in dropdown
    2. function get_all_category() {
    3.  
    4. if ( !current_user_can( 'administrator' ) ) {
    5. wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    6. }
    7. ?>
    8. <div class="wrap">
    9. <h2>Get All Categories:</h2>
    10. <form method="post" name="catego">
    11. <?php
    12. $terms = get_terms( 'category', array('hide_empty' => false));
    13. echo '<select name="category" id="category">';
    14. echo '<option value="">Select</option>';
    15. $i=0;
    16. foreach ($terms as $term) {
    17. $term_id = $term->term_id;
    18. echo '<option value="'.$term_id.'" name="cate_<?php echo $i;?>">'.$term->name.'</option>';
    19. $i++;
    20. }
    21. ?>
    22. </select>
    23. <input type="hidden" name="valuecat" id="valuecat" value="">
    24. <input type="submit" name="btnsub" value="view" />
    25. </form>
    26. </div>
    27. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    28. <script type="text/javascript">
    29. $(function () {
    30. $("#category").change(function () {
    31. var Value = $(this).val();
    32. $('#valuecat').val(Value);
    33. });
    34. });
    35. </script>
    36. <?php
    37. if(isset($_POST['btnsub'])){
    38. $cat_id = $_POST['valuecat'];
    39. list_category( $cat_id);
    40. }
    41. }

     

    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.

    1. function list_post($id){
    2. $args = array(
    3. 'post_type' => 'post',
    4. 'posts_per_page'=>-1,
    5. 'tax_query' => array(
    6. array(
    7. 'taxonomy' => 'category',
    8. 'terms' => $id
    9. )
    10. )
    11. );
    12. $query = new WP_Query( $args );
    13. echo '<table border="1" width="70%" align="center">';
    14. echo '<tr>';
    15. echo '<th>S No.</th>';
    16. echo '<th>Title</th>';
    17. echo '<th>Content</th>';
    18. echo '</tr>';
    19. $j=1;
    20. if($query->have_posts()){
    21. while ($query->have_posts()) {
    22. $query->the_post();
    23. ?>
    24. <tr>
    25. <td width="10%" align="center"><?=$j;?></td>
    26. <td width="20%" align="center"><?=the_title();?></td>
    27. <td width="70%" align="center"><?=the_content();?></td>
    28. </tr>
    29. <?php $j++;
    30. }
    31. }
    32. ?>
    33. </table>
    34. <?php
    35. }
    36. ?>

     

    That's it ! your Wordpress Plugin is created.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: