mardi 6 octobre 2020

Which Wordpress hook can I use that fires when a form its saved?

I ask this question because I'm a newbie at creating a WP plugin and I have a problem finding a clean way to execute Backend code. I have a view mixed with HTML with a PHP function that looks terrible to the keen eye. You can see it at the end of this text.

I want to put the function on a class in a different file that could be triggered by WP Hook and I don't know which hook to use. I have looked in other plugins that, apparently, use the 'save_post' hook. , But, reading the plugin, it seems to me that this hook applies to every saved post, like posts of a blog.

Thanks for reading this. Please let me know if you need more info.

<?php

function sinetiks_schools_create() {
    $id = $_POST["id"];
    $name = $_POST["name"];
    //insert
    if (isset($_POST['insert'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . "school";

        $wpdb->insert(
                $table_name, //table
                array('id' => $id, 'name' => $name), //data
                array('%s', '%s') //data format         
        );
        $message.="School inserted";
    }
    ?>
    <link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/sinetiks-schools/style-admin.css" rel="stylesheet" />
    <div class="wrap">
        <h2>Add New School</h2>
        <?php if (isset($message)): ?><div class="updated"><p><?php echo $message; ?></p></div><?php endif; ?>
        <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <p>Three capital letters for the ID</p>
            <table class='wp-list-table widefat fixed'>
                <tr>
                    <th class="ss-th-width">ID</th>
                    <td><input type="text" name="id" value="<?php echo $id; ?>" class="ss-field-width" /></td>
                </tr>
                <tr>
                    <th class="ss-th-width">School</th>
                    <td><input type="text" name="name" value="<?php echo $name; ?>" class="ss-field-width" /></td>
                </tr>
            </table>
            <input type='submit' name="insert" value='Save' class='button'>
        </form>
    </div>
    <?php
}

Aucun commentaire:

Enregistrer un commentaire