Site icon Hip-Hop Website Design and Development

Custom taxonomies registered via plugin stop existing after function finishes running

I’m writing a plugin that retrieves a information via an API and register it as a term in a custom taxonomy.

But as soon as the script finishes running, the taxonomy doesn’t exist anymore, and it gets recreated everytime the script runs, but since it doesn’t exist anymore after that, the posts aren’t affected

Here’s some relevant code:

public function ensure_place_taxonomy() {
    // Check if place is already a woocommerce taxonomy
    if (!taxonomy_exists( 'place' )) {
        // Create the "place" taxonomy
        $place_taxonomy = register_taxonomy(
            'place',
            'product',
            array(
                'label' => __( 'Place' ),
                'hierarchical' => true,
                'public' => true,
                'show_ui' => true,
                'show_in_nav_menus' => true,
                'show_tagcloud' => false,
                'show_in_quick_edit' => true,
                'show_admin_column' => true,
                'show_in_rest' => true
            )
        );
        // Register it as a woocommerce taxonomy
        register_taxonomy_for_object_type( 'place', 'product' );

        // Send an admin notice flagging that the place taxonomy was created again
        $this->send_admin_notice( 'info', 'place taxonomy was created again.' );

    } else {
        // Get the place taxonomy
        $place_taxonomy = get_taxonomy( 'place' );
    }

It creates the taxonomy, and testing for taxonomy_exists() after that, works, but it’s gone as soon as the script finishes, it doesn’t show up in any page, testing for taxonomy_exists anywhere else returns false and everytime the script runs it warns that the taxonomy was created again.

Why isn’t the taxonomy saving?

EDIT: I wasn’t sure if it really gets saved, since every example just hooks it to the ‘init’ hook without even checking if it exists or not, so I did that as well:

    // Init the place custom taxonomy
    $this->loader->add_action( 'init', $plugin_admin, 'ensure_place_taxonomy' );

It fixed the problem of it not showing up in other pages, but the terms added and the products set up with the terms aren’t being saved even then… What should I do?