0

I am trying to call a function inside a class, but somehow the function is not getting fired. When hooking into the same hook and just logging a string, the string gets logged. So the hook should work.

HTML calling the Function

                     <div class="wrap">
                        <h2>Update Category</h2>

                        <form method="post" action="<?php echo plugins_url('accounting.php', __FILE__); ?>">
                            <input type="hidden" name="action" value="update_category">
                            <input type="hidden" name="action" value="<?php echo htmlspecialchars($_GET["ID"]); ?>">

                            <label>Name</label>
                            <input type="text" name="category" placeholder="Category Name" value="<?php echo $category['category']; ?>">

                            <label>Description</label>
                            <input type="text" name="description" placeholder="Category Description" value="<?php echo $category['description']; ?>">

                            <input type="submit" value="Update Category" class="button button-primary">
                        </form>
                    </div>

Create Instance of Class and initializing Hooks

if (class_exists('AccountingTool')) {
    $accountingTool = new AccountingTool();
} 

add_action( 'admin_post_create_category', array($accountingTool, 'createCategory'));
add_action( 'admin_post_create_category', error_log("called"));

Class

class AccountingTool {

    function __construct() {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->categoriesTableName = $this->wpdb->prefix . 'categories';
    }


    function createCategory() {
        error_log("called 2");
        $category = $_POST['category'];
        $description = $_POST['description'];
        $res = $this->wpdb->insert($this->categoriesTableName, array(
            'ID' => NULL,
            'category' => $category,
            'description' => $description
        ));
        wp_redirect('/wp-admin/admin.php?page=category&ID=' . $this->wpdb->insert_id);
        error_log(var_export($_POST) . ", " . var_export($res));
    }

Logs As seen below the string gets logged, but nothing afterwards.

[27-Aug-2021 14:49:43 UTC] called
[27-Aug-2021 14:49:43 UTC] called
[27-Aug-2021 14:49:46 UTC] called
2
  • Where is your second block of code? Aug 27 at 15:58
  • All in one file /wp-content/plugins/accounting/accounting.php
    – alexesh
    Aug 28 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.