Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Ty Chhoun

    (@tychhoun)

    Yes I use it in single post!
    In that page I have custom form submit, so I need input hidden field to keep current page or post title embed when I submit sent email, that I need
    <input type=”hidden” mane=”tourCode” value=”[get_post_title]” />

    Any better solution?

    Thread Starter Ty Chhoun

    (@tychhoun)

    Dear threadi

    Thanks you so much for your kindness help me, I am very happy with your help.

    Thank
    Ty

    Thread Starter Ty Chhoun

    (@tychhoun)

    Hi

    I understand that you recommend with post type, but my purpose is newest and fresh to learn WP that why I follow some some document. and I don’t want most record stored in table post.

    so I hope you can check my bellow and can help thanks.

    bellow is code for “toty_table_render.php”
    <?php
        // Creating an instance
        $empTable = new Employees_List_Table();
        echo ‘<div class=”wrap”><h2>Employees List Table</h2>’;
        // Prepare table
        $empTable->prepare_items();
    ?>
        <form method=”post”>
              <input type=”hidden” name=”page” value=”emp-list” />
              <?php 
                $empTable->search_box(‘search’, ‘search_id’); 
                // Display table
                $empTable->display();
              ?>
        </form>
    <?php
        echo ‘</div>’;

    Thread Starter Ty Chhoun

    (@tychhoun)

    Thanks you so much, here is my hold code & May video
    https://www.youtube.com/watch?v=4GoGhH3RZts
    
    
    <?php
    
    /*
    
     * Plugin Name: Custom TOTY
    
     * Description: My plugin to explain the crud functionality.
    
     * Version: 1.0
    
     * Author: ToTy Monthana
    
     * Plugin URI: https://estccomputer.com/
    
     * Author URI: https://estccomputer.com/
    
     */
    
    define("TOTY_PLUGIN_DIR_PATH",plugin_dir_path(__FILE__));
    
    define("TOTY_PLUGIN_URL", plugins_url());
    
    register_activation_hook(__FILE__, 'table_creator');
    
    function table_creator()
    
    {
    
        global $wpdb;
    
        $charset_collate = $wpdb->get_charset_collate();
    
        $table_name = $wpdb->prefix . 'ems';
    
        $sql = "DROP TABLE IF EXISTS $table_name;
    
                CREATE TABLE $table_name(
    
                id mediumint(11) NOT NULL AUTO_INCREMENT,
    
                emp_id varchar(50) NOT NULL,
    
                emp_name varchar (250) NOT NULL,
    
                emp_email varchar (250) NOT NULL,
    
                emp_dept longtext NOT NULL,
    
                PRIMARY KEY id(id)
    
                )$charset_collate;";
    
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
        dbDelta($sql);
    
    }
    
    if(!class_exists('WP_List_Table')) {
    
        require_once(ABSPATH . 'wp-admin/includes/screen.php');
    
        require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
    
    }
    
    class Employees_List_Table extends WP_List_Table{
    
        private $users_data;
    
        private function get_users_data($search = "") {
    
            global $wpdb;
    
            $table_name = $wpdb->prefix . 'ems';
    
            if (!empty($search)) {
    
                return $wpdb->get_results(
    
                        "SELECT * FROM $table_name WHERE id Like '%{$search}%' OR emp_id Like '%{$search}%' OR emp_name Like '%{$search}%' OR emp_email Like '%{$search}%'",
    
                        ARRAY_A
    
                );
    
            }else{
    
                return $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
    
            }
    
        }
    
        // Define table columns
    
        function get_columns() {
    
            $columns = array(
    
                'cb' => '<input type="checkbox" />',
    
                'id' => 'ID',
    
                'emp_id' => 'Employee ID',
    
                'emp_name' => 'Employee Name',
    
                'emp_email' => 'Email'
    
            );
    
            return $columns;
    
        }
    
        // Prepare table items
    
        function prepare_items() {
    
            // Condidtion Search
    
            if (isset($_POST['page']) && isset($_POST['s'])) {
    
                $this->users_data = $this->get_users_data($_POST['s']);
    
            } else {
    
                $this->users_data = $this->get_users_data();
    
            }
    
            $columns = $this->get_columns();
    
            $hidden = array();
    
            $sortable = $this->get_sortable_columns();
    
            $this->_column_headers = array($columns, $hidden, $sortable);
    
            // var_dump($columns);
    
            /* pagination */
    
            //$per_page = 2;
    
            $per_page = $this->get_items_per_page('employees_per_page', 20);
    
            $current_page = $this->get_pagenum();
    
            $total_items = count($this->users_data);
    
            /* pagination */
    
            $this->users_data = array_slice($this->users_data, (($current_page - 1) * $per_page), $per_page);
    
            $this->set_pagination_args(array(
    
                'total_items' => $total_items, // total number of items
    
                'per_page'    => $per_page // items to show on a page
    
            ));
    
            usort($this->users_data, array(&$this, 'usort_reorder'));
    
            $this->items = $this->users_data;
    
        } // END Prepare table items
    
        // bind data with column
    
        function column_default($item, $column_name)
    
        {
    
            switch ($column_name) {
    
                case 'id':
    
                case 'emp_id':
    
                case 'emp_name':
    
                case 'emp_email':
    
                    return $item[$column_name];
    
                default:
    
                    return print_r($item, true); //Show the whole array for troubleshooting purposes
    
            }
    
        }
    
        function column_cb($item)
    
        {
    
            return sprintf(
    
                '<input type="checkbox" name="eployee[]" value="%s" />',
    
                $item['id']
    
            );
    
        }
    
        // To show bulk action dropdown
    
        function get_bulk_actions()
    
        {
    
            $actions = array(
    
                    'delete_all'    => 'Delete',
    
                    'draft_all' => "Move to Draft"
    
            );
    
            return $actions;
    
        }
    
        // Add sorting to columns
    
        protected function get_sortable_columns()
    
        {
    
            $sortable_columns = array(
    
                'id'  => array('id', true),
    
                'emp_id' => array('emp_id', false),
    
                'emp_name'   => array('emp_name', false)
    
            );
    
            return $sortable_columns;
    
        }
    
        // Sorting function
    
        function usort_reorder( $a, $b ) {
    
            // If no sort, default to title
    
            $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id';
    
            // If no order, default to asc
    
            $order = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc';
    
            // Determine sort order
    
            $result = strcmp( $a[$orderby], $b[$orderby] );
    
            // Send final sort direction to usort
    
            return ( $order === 'asc' ) ? $result : -$result;
    
        }
    
        // Adding action buttons to column
    
        function column_id($item)
    
        {
    
            $actions = array(
    
                    'edit'  => sprintf('<a href="?page=%s&action=%s&employee_id=%s">Edit</a>', $_REQUEST['page'], 'edit', $item['id']),
    
                    'delete'  => sprintf('<a href="?page=%s&action=%s&employee_id=%s">Delete</a>', $_REQUEST['page'], 'delete', $item['id']),
    
            );
    
            return sprintf('%1$s %2$s', $item['id'], $this->row_actions($actions));
    
        }
    
    } // End class
    
    function toty_emp_display_admin_menu()
    
    {
    
        $toty_hook = add_menu_page('EMS', 'EMS', 'manage_options', 'emp-list', 'da_ems_list_callback');
    
        // screen option
    
        add_action("load-$toty_hook", 'my_emp_tbl_add_options');  
    
        function my_emp_tbl_add_options()
    
        {
    
            global $empTable;
    
            $option = 'per_page';
    
            $args = array(
    
                    'label' => 'Number of items per page:',
    
                    'default' => 20,
    
                    'option' => 'employees_per_page'
    
            );
    
            add_screen_option($option, $args);
    
            $empTable = new Employees_List_Table();
    
        };
    
        add_submenu_page('emp-list', 'Employee List', 'Employee List', 'manage_options', 'emp-list', 'da_ems_list_callback');
    
        add_submenu_page('emp-list', 'Add Employee', 'Add Employee', 'manage_options', 'add-emp', 'da_ems_add_callback');
    
        //add menu for update
    
        add_submenu_page(null, 'Update Employee', 'Update Employee', 'manage_options', 'update-emp', 'da_emp_update_call');
    
    } // End toty_emp_display_admin_menu
    
    add_action('admin_menu', 'toty_emp_display_admin_menu');
    
    // get saved screen meta value
    
    add_filter('set-screen-option', 'my_emp_table_set_option', 10, 3);
    
    function my_emp_table_set_option($status, $option, $value)
    
    {
    
       return $value;
    
    }
    
    function da_ems_add_callback()
    
    {
    
        global $wpdb;
    
        $table_name = $wpdb->prefix . 'ems';
    
        $msg = '';
    
        if (isset($_REQUEST['submit'])) {
    
            $wpdb->insert("$table_name", [
    
                "emp_id" => $_REQUEST['emp_id'],
    
                'emp_name' => $_REQUEST['emp_name'],
    
                'emp_email' => $_REQUEST['emp_email'],
    
                'emp_dept' => isset($_REQUEST['emp_dept']) ? stripslashes(html_entity_decode($_REQUEST['emp_dept'])) : ''
    
            ]);
    
            if ($wpdb->insert_id > 0) {
    
                $msg = "Saved Successfully";
    
            } else {
    
                $msg = "Failed to save data";
    
            }
    
        }
    
    ?>
    
        <h4 id="msg"><?php echo $msg; ?></h4>
    
        <form method="post">
    
            <p>
    
                <label>EMP ID</label>
    
                <input type="text" name="emp_id" placeholder="Enter ID" required>
    
            </p>
    
            <p>
    
                <label>Name</label>
    
                <input type="text" name="emp_name" placeholder="Enter Name" required>
    
            </p>
    
            <p>
    
                <label>Email</label>
    
                <input type="email" name="emp_email" placeholder="Enter Email" required>
    
            </p>
    
            <p>
    
                <label>Department</label>
    
                <!-- <input type="text" name="emp_dept" placeholder="Enter Department" required> -->
    
                <?php
    
                    //$description_field = 'emp_dept'; // Replace with your field name
    
                    $editor_id = 'emp_dept';
    
                    $description_args = array(
    
                        'media_buttons' => true,
    
                        'textarea_rows' => 12,
    
                        'textarea_name' => 'emp_dept',
    
                        'editor_class' => 'description-editor widefat',
    
                        'wpautop' => true
    
                    );
    
                    wp_editor($emp_dept, $editor_id, $description_args);
    
                ?>
    
            </p>
    
            <p>
    
                <button type="submit" name="submit">Submit</button>
    
            </p>
    
        </form>
    
    <?php
    
    }
    
    // List EMP with action
    
    function da_ems_list_callback()
    
    {
    
        if (isset($_REQUEST['action'])){
    
            if($_REQUEST['action'] == 'edit'){
    
                global $wpdb;
    
                $table_name = $wpdb->prefix . 'ems';
    
                $msg = '';
    
                $id = isset($_REQUEST['employee_id']) ? intval($_REQUEST['employee_id']) : "";
    
                if (isset($_REQUEST['update'])) {
    
                    if (!empty($id)) {
    
                        $wpdb->update(
    
                            "$table_name",
    
                                [
    
                                    "emp_id" => $_REQUEST['emp_id'],
    
                                    'emp_name' => $_REQUEST['emp_name'],
    
                                    'emp_email' => $_REQUEST['emp_email'],
    
                                    'emp_dept' => stripslashes(html_entity_decode($_REQUEST['emp_dept']))
    
                                ],
    
                            ["id" => $id]
    
                        );
    
                        $msg = 'Data updated';
    
                    }
    
                }
    
                $employee_details = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name where id = %d", $id), ARRAY_A); ?>
    
                <h4><?php echo $msg; ?></h4>
    
                <form method="post">
    
                    <p>
    
                        <label>EMP ID</label>
    
                        <input type="text" name="emp_id" placeholder="Enter ID" value="<?php echo $employee_details['emp_id']; ?>"
    
                            required>
    
                    </p>
    
                    <p>
    
                        <label>Name</label>
    
                        <input type="text" name="emp_name" placeholder="Enter Name"
    
                            value="<?php echo $employee_details['emp_name']; ?>" required>
    
                    </p>
    
                    <p>
    
                        <label>Email</label>
    
                        <input type="email" name="emp_email" placeholder="Enter Email"
    
                            value="<?php echo $employee_details['emp_email']; ?>" required>
    
                    </p>
    
                    <p>
    
                        <label>Department</label>
    
                        <?php
    
                            $content = '';
    
                            // sanitize_text_field($employee_details['emp_dept']); // Replace with your field name
    
                            $content = isset($employee_details['emp_dept']) ? stripslashes(html_entity_decode($employee_details['emp_dept'])) : '';
    
                            //var_dump($content);
    
                            $editor_id = 'edit_emp_dept';
    
                            $wp_editor_setting = array(
    
                                'textarea_name' => 'emp_dept',
    
                            );
    
                            wp_editor($content, $editor_id, $wp_editor_setting);
    
                        ?>
    
                    </p>
    
                    <p>
    
                        <button type="submit" name="update">Update</button>
    
                    </p>
    
                </form>
    
            <?php
    
            }else if($_REQUEST['action'] == 'delete'){
    
                echo "delete";
    
            }else{
    
                include_once TOTY_PLUGIN_DIR_PATH . "/view/toty_table_render.php";
    
            }
    
        }else{
    
            include_once TOTY_PLUGIN_DIR_PATH . "view/toty_table_render.php";
    
        }
    
    }
    • This reply was modified 2 years ago by Ty Chhoun.
Viewing 4 replies - 1 through 4 (of 4 total)