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
    • 376
    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

    /*
    Plugin Name: Get Category in Dropdown
    Plugin URI: evontech.com
    Description: Get Category in Dropdown and when we select a category fetch all relative post.
    Author: Nitish Rawat
    Version: 1.0
    Author URI: evontech.com
    */

     

    Now, 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');
      
    }

     

    Get all category from post in dropdown

    // get all category from post in dropdown
    function get_all_category() {
    
        if ( !current_user_can( 'administrator' ) ) {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }    
    ?>
        <div class="wrap">
            <h2>Get All Categories:</h2>
            <form method="post" name="catego">
                <?php
                    $terms = get_terms( 'category', array('hide_empty' => false));
                    echo '<select name="category" id="category">';
                    echo '<option value="">Select</option>';
                    $i=0;
                    foreach ($terms as $term) {
                        $term_id  = $term->term_id;
                        echo '<option value="'.$term_id.'" name="cate_<?php echo $i;?>">'.$term->name.'</option>';
                        $i++;
                    }
                ?>
                </select>
                <input type="hidden" name="valuecat" id="valuecat" value="">
                <input type="submit" name="btnsub" value="view" /> 
            </form>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#category").change(function () {
                    var Value = $(this).val();
                    $('#valuecat').val(Value);
                });
            });
        </script>
        <?php
        if(isset($_POST['btnsub'])){      
            $cat_id = $_POST['valuecat'];
            list_category( $cat_id);
        }
    }

     

    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.

    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 '</tr>';
        $j=1;
        if($query->have_posts()){
            while ($query->have_posts()) {
                $query->the_post();
        ?>
            <tr>
                <td width="10%" align="center"><?=$j;?></td>
                <td width="20%" align="center"><?=the_title();?></td>
                <td width="70%" align="center"><?=the_content();?></td>
            </tr>
        <?php $j++;
                }
            }
        ?>
        </table>
    <?php
    }
    ?>

     

    That's it ! your Wordpress Plugin is created.

 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: