When to use action hooks and plugins

I have a page which I want user to be able to post info which I then upload into a database. I am doing my requests via ajax and I am trying to establish my database connection and sql querying via an action hook. First off is this a good pratice?

My code (the relative part):


<div id="user_info">
    <input class="form-control" type="text" id="name" placeholder="Fullständigt namn" required autocomplete="off"/>
    <input class="form-control" type="text" id="email" placeholder="Email" required autocomplete="off"/>
    <input class="form-control" type="text" id="address" placeholder="Adress" required autocomplete="off"/>
    <input class="form-control" type="text" id="co_address" placeholder="C/O Adress"  autocomplete="off"/>
    <input class="form-control" type="text" id="city" placeholder="Namn" required autocomplete="off"/>


function updateDB(name, email, address, co_address, city){
        url: '/wp-admin/admin-ajax.php',
        type: 'post',
        dataType: 'json',
        data: {
            action: 'det_lilla_extra',
            name: name,
            email: email,
            address: address,
            co_address: co_address,
            city: city

PHP (action hook):


add_action('wp_ajax_det_lilla_extra', 'det_lilla_extra');
add_action( 'wp_ajax_nopriv_det_lilla_extra', 'det_lilla_extra' );
function det_lilla_extra(){
    global $wpdb;
    $wpdb->insert($wpdb->prepare("INSERT INTO orders (name, email, address, co_address, city) VALUES ('%s','%s','%s','%s','%s')",
    $_POST['name'], $_POST['email'], $_POST['address'], $_POST['co_address'], $_POST['city']));

I’ve been able to get this far by following and looking at others. But my problem now is that for my action hook to work I need to create a plugin. I dont know how to do this, I’ve been googling to find turorials but I dont really know what to look for, so its kinda hard to pinpoint what to do.

So my questions:

  1. Is this “good” pratice what i have done? Could I do it another way (regarding having an action hook for database connection)

  2. Is a plugin what i need here? And if so, how do i go about creating it?

ALSO: Currently my action hook does not work since I have not enabled the plugin (i think this is the reason). But everything else should work though.

Read more here: When to use action hooks and plugins

Leave a Reply

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