0
votes

I had created plugin named DailyCostPlugin and initiate register_post_type function to create custom post named Daily Cost.I want to add Meta box named Daily Cost Details by add_action add_action( 'admin_init',array( $this, create_meta_cost_box' )); it goes into the function create_meta_cost_box but did not display metabox named Daily Cost Details, also it did not goto function named display_cost_box called in the add_meta_box. Here is code

<?php
 /*
 Plugin Name: Daily Cost Plugin
 Plugin URI: http://biney.com/plugin
 Description: This is my First attempt
 Version: 1.0.0
 Author: Biney
 Author URI: http://biney.com
 License: GPLv2 or later
 Text Domain: daily-cost-plugin
*/

defined( 'ABSPATH' ) or die("Hey, you can\t access!");
class DailyCostPlugin{

 function __construct(){
       add_action( 'init',array( $this,'daily_cost_custom_post' ));
       add_action( 'admin_init',array( $this, 'create_meta_cost_box' ));
 }

 function daily_cost_custom_post(){
    register_post_type('daily_costs', [
        'public' => true,
        'menu_position' => 15,
        'supports'      => array( 'title'),
        'taxonomies'    => array( '' ),
        'menu_icon'     => plugins_url( 'images/image.png', __FILE__ ),
        'has_archive'   => true,
        'labels' => array(
            'name' => 'Daily Costs',
            'singular_name'         => 'Daily Cost',
            'add_new'               => 'Add New',
            'add_new_item'          => 'Add New Daily Cost',
            'edit'                  => 'Edit',
            'edit_item'             => 'Edit Daily Cost',
            'new_item'              => 'New Daily Cost',
            'view'                  => 'View',
            'view_item'             => 'View Daily Cost',
            'search_items'          => 'Search Daily Costs',
            'not_found'             => 'No Daily Costs found',
            'not_found_in_trash'    => 'No Daily Costs found in Trash',
            'parent'                => 'Parent Daily Cost'
            )
        ]
    );
 }
 function create_meta_cost_box(){
    //didn't show meta bow having title Daily Cost Details
    add_meta_box( 'daily_cost_meta_box',
        'Daily Cost Details',
        'display_daily_cosyt_meta_box',
        'daily_costs', 'normal', 'high'
    );
 }

 function display_cost_box( $daily_cost ) {
    // didn't reaching here
    die('ww');
    // Retrieve current cost and rating based on daily cost ID
    $daily_cost_value = esc_html( get_post_meta( $daily_cost->ID, 
    'daily_cost_value', true ) );
    $daily_cost_rating = intval( get_post_meta( $daily_cost->ID, 
    'daily_cost_rating', true ) );
    ?>
    <table>
        <tr>
            <td style="width: 100%">Daily Cost</td>
            <td><input type="text" size="80" name="daily_cost_value" value=" 
     <?php echo $daily_cost_value; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 150px">Daily Cost Rating</td>
                <td>
                <select style="width: 100px" name="daily_cost_rating">
                <?php
                // Generate all items of drop-down list
                for ( $rating = 5; $rating >= 1; $rating -- ) {
                ?>
                    <option value="<?php echo $rating; ?>" <?php echo 
  selected( $rating, $daily_cost_rating ); ?>>
                    <?php echo $rating; ?> stars <?php } ?>
                </select>
            </td>
        </tr>
    </table>
    <?php
   }
  function activate(){
    //generate a cpt
    //flush rewrite rules
    flush_rewrite_rules();
  }
  function deactivate(){
    //flush rewrite rules
    flush_rewrite_rules();  
  }
  function uninstall(){
    //delete cpt
    //delte all the plugin data from the DB
   }
  }
 if( class_exists( 'DailyCostPlugin' ) ){
     $dailyCostPlugin = new DailyCostPlugin();
 }
 //activation
 register_activation_hook( __FILE__, array( $dailyCostPlugin, 'activate'));
 //deactivation
 register_deactivation_hook( __FILE__, array( $dailyCostPlugin, 
 'deactivate'));
1

1 Answers

0
votes

Here you go, let's modify your code like this

<?php
    /*
    Plugin Name: Daily Cost Plugin
    Plugin URI: http://biney.com/plugin
    Description: This is my First attempt
    Version: 1.0.0
    Author: Biney
    Author URI: http://biney.com
    License: GPLv2 or later
    Text Domain: daily-cost-plugin
    */

    defined( 'ABSPATH' ) or die("Hey, you can\t access!");
    class DailyCostPlugin{

        function __construct(){
            add_action( 'init',array( $this,'daily_cost_custom_post' ));
            add_action( 'admin_init',array( $this, 'create_meta_cost_box' ));
        }

        function daily_cost_custom_post(){
            register_post_type('daily_costs', [
                    'public' => true,
                    'menu_position' => 15,
                    'supports'      => array( 'title'),
                    'taxonomies'    => array( '' ),
                    'menu_icon'     => plugins_url( 'images/image.png', __FILE__ ),
                    'has_archive'   => true,
                    'labels' => array(
                        'name' => 'Daily Costs',
                        'singular_name'         => 'Daily Cost',
                        'add_new'               => 'Add New',
                        'add_new_item'          => 'Add New Daily Cost',
                        'edit'                  => 'Edit',
                        'edit_item'             => 'Edit Daily Cost',
                        'new_item'              => 'New Daily Cost',
                        'view'                  => 'View',
                        'view_item'             => 'View Daily Cost',
                        'search_items'          => 'Search Daily Costs',
                        'not_found'             => 'No Daily Costs found',
                        'not_found_in_trash'    => 'No Daily Costs found in Trash',
                        'parent'                => 'Parent Daily Cost'
                    )
                ]
            );
        }
        function create_meta_cost_box(){
            //didn't show meta bow having title Daily Cost Details
            add_meta_box( 'daily_cost_meta_box',
                'Daily Cost Details',
                array( $this,'display_cost_box' ),
                'daily_costs', 'normal', 'high'
            );
        }

        function display_cost_box( $daily_cost ) {

            // Retrieve current cost and rating based on daily cost ID
            $daily_cost_value = esc_html( get_post_meta( $daily_cost->ID,
                'daily_cost_value', true ) );
            $daily_cost_rating = intval( get_post_meta( $daily_cost->ID,
                'daily_cost_rating', true ) );
            ?>
            <table>
                <tr>
                    <td style="width: 100%">Daily Cost</td>
                    <td><input type="text" size="80" name="daily_cost_value" value="
         <?php echo $daily_cost_value; ?>" /></td>
                </tr>
                <tr>
                    <td style="width: 150px">Daily Cost Rating</td>
                    <td>
                        <select style="width: 100px" name="daily_cost_rating">
                            <?php
                            // Generate all items of drop-down list
                            for ( $rating = 5; $rating >= 1; $rating -- ) {
                            ?>
                            <option value="<?php echo $rating; ?>" <?php echo
                            selected( $rating, $daily_cost_rating ); ?>>
                                <?php echo $rating; ?> stars <?php } ?>
                        </select>
                    </td>
                </tr>
            </table>
            <?php
        }
        function activate(){
            //generate a cpt
            //flush rewrite rules
            flush_rewrite_rules();
        }
        function deactivate(){
            //flush rewrite rules
            flush_rewrite_rules();
        }
        function uninstall(){
            //delete cpt
            //delte all the plugin data from the DB
        }
    }
    if( class_exists( 'DailyCostPlugin' ) ){
        $dailyCostPlugin = new DailyCostPlugin();
    }
    //activation
    register_activation_hook( __FILE__, array( $dailyCostPlugin, 'activate'));
    //deactivation
    register_deactivation_hook( __FILE__, array( $dailyCostPlugin,'deactivate'));