insert data in wordpress

Sometimes you need to make a custom table in wordpress database table. It is not so very difficult to insert data in wordpress database table. Just follow the following steps.

step 01 : First make table in your wordpress database tables name ctables with your required fields.

step 02 : Next you need to make a shortcode with a form to take input from input fields. I have made a shortcode named ctable_shortcode.php

<?php
function get_ctable_form($atts, $content = null)
{
    $form = '<form action="#" method="post">
                <label for="name">Name</label>
                <input type="text" name="name" id="name" />
                <label for="category">Category</label>
                <input type="text" name="category" id="category" />
                <label for="description">Description</label>
                <textarea name="description" id="description"></textarea>   
                <input type="submit" name="ctable_insert" >
            </form>';
    return $form;
}
add_shortcode( 'ctable-form', 'get_ctable_form' );

step 03 : Next you need to make a functions file to insert input data from input fields to database table. I have made a function file named ctable_function.php

 <?php 
if(isset($_POST['ctable_insert'])){
    global $wpdb;
 $table_name = 'uploaded_srts';
foreach ($subs as $key=>$subtitle) {
    $data_array = [
        'name'=> $_POST['name'],
        'category'=> $_POST['category'], 
        'description'=> $_POST['description'], 
    ];
    $result = $wpdb->insert($table_name, $data_array, $format=null);
 }
}

step 04 : Next you need to include the files in the wordpress functions.

require get_template_directory() . '/inc/ctable_shortcodes.php';
require get_template_directory() . '/inc/ctable_function.php';

ok all sets now add the shortcode in a page and insert data according to your choice. So insert data in wordpress database and enjoy!

How to parse srt file and insert it in wordpress database

Leave a Reply

Your email address will not be published. Required fields are marked *