Wednesday, 5 February 2025

wp table list in wordpress

 List.php code 

<?php

if(!class_exists('WP_List_Table')){

    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

}


class Custom_Listings_Table extends WP_List_Table {

    function __construct(){

        global $status, $page;

                

        //Set parent defaults

        parent::__construct( array(

                'singular' => 'listing',

                'plural'   => 'listings',

                'ajax'     => false

        ) );

        

    }

function search_box( $text, $input_id ) {

    if ( empty( $_REQUEST['s'] ) && !$this->has_items() ) {

        return;

    }


    $input_id = $input_id . '-search-input';


    ?>

    <p class="search-box">

        <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>

        <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php echo esc_attr( $_REQUEST['s'] ?? '' ); ?>" />

        <?php submit_button( $text, 'button', '', false, array('id' => 'search-submit') ); ?>

    </p>

    <?php

}

    function column_default($item, $column_name){

        switch($column_name){

               case 'ID':

                return $item[$column_name];  

case 'first_name':   

return $item[$column_name]; 

case 'surname':  

return $item[$column_name];

case 'email':  

return $item[$column_name];

case 'phone':  

return $item[$column_name];

case 'action':  

return $item[$column_name];

            default:

                return print_r($item,true); //Show the whole array for troubleshooting purposes

        }

    }

    function column_title($item){

        

        //Build row actions

        $actions = array(

            'Delete'      => sprintf('<a href="?page=%s&action=%s&movie=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']),

        );

/* array(

            'pending'      => sprintf('<a href="?page=%s&action=%s&movie=%s">Pending</a>',$_REQUEST['page'],'pending',$item['ID']),

            'completed'    => sprintf('<a href="?page=%s&action=%s&movie=%s">Completed</a>',$_REQUEST['page'],'completed',$item['ID']),

        ); */

        

        //Return the title contents

        return sprintf('%1$s%3$s',

            /*$1%s*/ $item['domain'],

            /*$2%s*/ $item['ID'],

            /*$3%s*/ $this->row_actions($actions)

        );

    }

    function column_cb($item){

        return sprintf(

            '<input type="checkbox" name="%1$s[]" value="%2$s" />',

            /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("movie")

            /*$2%s*/ $item['ID']                //The value of the checkbox should be the record's id

        );

    }

    function get_columns(){

        $columns = array(

               'cb'=> '<input type="checkbox" />',

                'first_name'  => 'First Name',

                'surname'     => 'Surname',

                'email'       => 'Email',

                'phone'       => 'Phone',

                'action'   => 'Action'

        );

        return $columns;

    }


    function get_sortable_columns() {

        $sortable_columns = array(

            'doamin' => array('domain',false),

);

        return $sortable_columns;

    }


    function get_bulk_actions() {

        $actions = array(

            'delete'    => 'Delete'

        );

        return $actions;

    }


    function process_bulk_action() {

        

        //Detect when a bulk action is being triggered...

        if( 'delete'===$this->current_action() ) {

           foreach($_GET['listing'] as $fID){

global $wpdb;

$table = $wpdb->prefix."custom_form_data_new";

//$wpdb->delete($table, array( 'id' => $fID ));

}

echo '<script>window.location.href = "../wp-admin/admin.php?page=custom-form-listing";</script>';

            //wp_die('Items deleted (or they would be if we had items to delete)!');

        }   


if( 'pending'===$this->current_action() OR 'completed'===$this->current_action()) {

global $wpdb;

$table_name = $wpdb->prefix."custom_form_data_new";

//$ereminders = $wpdb->query($wpdb->prepare("UPDATE ".$table_name." SET status='".$_GET['action']."' WHERE id='".$_GET['movie']."'"));

echo '<script>window.location.href = "../wp-admin/admin.php?page=custom-form-listing";</script>';

        }

        

    }

function prepare_items() {

    global $wpdb; //This is used only if making any database queries

    $per_page = 15;

    $columns = $this->get_columns();

    $hidden = array();

    $sortable = $this->get_sortable_columns();

    $this->_column_headers = array($columns, $hidden, $sortable);

    $this->process_bulk_action();


     $table = $wpdb->prefix."custom_form_data_new";

     $current_page = $this->get_pagenum();

     $offset = ($current_page - 1) * $per_page;

$search = isset($_REQUEST['s']) ? sanitize_text_field($_REQUEST['s']) : '';


        $example_datas = [];

        //$total_items = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");

  $where_sql = "";

            if (!empty($search)) {

                $where_sql = "WHERE first_name LIKE '%$search%' OR surname LIKE '%$search%' OR email LIKE '%$search%'";

            }

//$getalls = $wpdb->get_results("SELECT * FROM $table LIMIT $per_page OFFSET $offset", ARRAY_A);

          // Fetch filtered data

            $query = "SELECT * FROM $table $where_sql LIMIT $per_page OFFSET $offset";

            $getalls = $wpdb->get_results($query, ARRAY_A);


foreach($getalls as $getall){

$example_datas[] = [

                'id'    => '1',

                'first_name'  => $getall['first_name'],

                'surname'     => $getall['surname'],

                'email'       => $getall['email'],

                'phone'       => $getall['phone'],

                'action'   => 'See All Data'

                ];

}

    $data = $example_datas;


    function usort_reorder($a, $b) {

        $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title';

        $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';

        $result = strcmp($a[$orderby], $b[$orderby]);

        return ($order === 'asc') ? $result : -$result;

    }

    @usort($data, 'usort_reorder');

    $current_page = $this->get_pagenum();

    @$total_items = count($data);


    @$data = array_slice($data, (($current_page - 1) * $per_page), $per_page);

    $this->items = $data;

    $this->set_pagination_args( array(

        'total_items' => $total_items,

        'per_page'    => $per_page,

        'total_pages' => ceil($total_items / $per_page)

    ));

}


}

?>

Main file 

function custom_form_listing_menu() {
    add_menu_page(
        'Custom Form Data',     // Page title
        'Form Submissions',     // Menu title
        'manage_options',       // Capability
        'custom-form-listing',  // Menu slug
        'render_custom_form_list_page', // Callback function
        'dashicons-list-view',  // Icon
        20                      // Position
    );
}
add_action('admin_menu', 'custom_form_listing_menu');

function render_custom_form_list_page() {
    require_once plugin_dir_path(__FILE__) . 'list.php';
//include 'list.php';

    $list_table = new Custom_Listings_Table();
    $list_table->prepare_items();

    echo '<div class="wrap"><h1>Custom Form Submissions</h1>';
        // Add the search box
    echo '<form method="get">';
    echo '<input type="hidden" name="page" value="' . esc_attr($_REQUEST['page']) . '" />';
    $list_table->search_box('Search Listings', 'search_id');
    echo '</form>';
    echo '<form method="post">';
    $list_table->display();
    echo '</form></div>';
}

Monday, 3 February 2025

nocache headers in the template issue

 <?php

header('Cache-Control: no-cache, no-store, must-revalidate');

header('Pragma: no-cache');

header('Expires: 0');

?>


Friday, 31 January 2025

Add meta boxes for Video url and text multiple in wordpress using code

 
function business_add_custom_metaboxes() {

    add_meta_box(

        'business_video_url',

        'Video',

        'business_video_url_callback',

        'business',

        'normal',

        'high'

    );
}

add_action('add_meta_boxes', 'business_add_custom_metaboxes');


function business_video_url_callback($post) {

    $videos = get_post_meta($post->ID, 'business_video_urls', true);

    if (!is_array($videos)) {

        $videos = []; // Ensure $videos is an array

    }

  echo '<div id="business-video-container">';

    foreach ($videos as $video) {

        echo '<input type="text" name="business_video_urls[]" value="' . esc_attr($video) . '" style="width:100%; margin-bottom:10px;" />';

    }

    echo '</div>';

    

    echo '<button type="button" id="add-video-url">Add Video URL</button>';

    

    echo '<script>

        document.getElementById("add-video-url").addEventListener("click", function() {

            var container = document.getElementById("business-video-container");

            var input = document.createElement("input");

            input.type = "text";

            input.name = "business_video_urls[]";

            input.style = "width:100%; margin-bottom:10px;";

            container.appendChild(input);

        });

    </script>';

}


function business_save_metaboxes($post_id) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    if (!current_user_can('edit_post', $post_id)) return;


    if (isset($_POST['business_video_urls'])) {

        $video_urls = array_map('esc_url_raw', array_filter($_POST['business_video_urls']));

        update_post_meta($post_id, 'business_video_urls', $video_urls);

    }

}
add_action('save_post', 'business_save_metaboxes');