Support

Account

Home Forums Add-ons Options Page Register a Taxonomy if Option Field set to True

Solved

Register a Taxonomy if Option Field set to True

  • Hello! I have a true/false option field. If the option is set to 1, I’d like to have my custom plugin register a taxonomy. Here’s my current code, however, it’s not registering the tax. What might I be doing wrong here? Thanks in advance for any assistance!

    function wed_get_vendor_enable() {
    	$enablevendortax = get_field('enable_vendor_tax', 'option');
    	
    	if($enablevendortax='1') {	
    	/**
    	 * Taxonomy: Vendors.
    	 */
    
    		function register_my_cpts_wed_vendors() {
    			$labels = array(
    				"name" => __( "Vendors", "wed" ),
    				"singular_name" => __( "Vendors", "wed" ),
    			);
    
    			$args = array(
    				"label" => __( "Vendors", "wed" ),
    				"labels" => $labels,
    				"public" => true,
    				"publicly_queryable" => true,
    				"hierarchical" => false,
    				"show_ui" => true,
    				"show_in_menu" => true,
    				"show_in_nav_menus" => true,
    				"query_var" => true,
    				"rewrite" => array( 'slug' => 'vendors', 'with_front' => true, ),
    				"show_admin_column" => true,
    				"show_in_rest" => true,
    				"rest_base" => "wed_vendors",
    				"rest_controller_class" => "WP_REST_Terms_Controller",
    				"show_in_quick_edit" => false,
    			);
    			register_taxonomy( "wed_vendors", array( "post", "wed_vendor"), $args );
    		}
    	}
    }
    
    add_action( 'init', 'wed_get_vendor_enable' );
  • UPDATE: This is solved. In case anyone wants to register a taxonomy based upon an option field, here’s how to do it:

    function register_my_cpts_wed_vendors() {
    	$labels = array(
    		"name" => __( "Vendors", "wed" ),
    		"singular_name" => __( "Vendors", "wed" ),
    	);
    
    	$args = array(
    		"label" => __( "Vendors", "wed" ),
    		"labels" => $labels,
    		"public" => true,
    		"publicly_queryable" => true,
    		"hierarchical" => false,
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'vendors', 'with_front' => true, ),
    		"show_admin_column" => true,
    		"show_in_rest" => true,
    		"rest_base" => "wed_vendors",
    		"rest_controller_class" => "WP_REST_Terms_Controller",
    		"show_in_quick_edit" => false,
    	);
    	register_taxonomy( "wed_vendors", array( "post", "wed_vendor"), $args );
    }
    
    add_action( 'register_wed_vendors','register_my_cpts_wed_vendors', 10, 2 );
    
    function wed_get_vendor_enable() {
    	$enablevendortax = get_field('enable_vendor_tax', 'option');
    	
    	if($enablevendortax=='1') {	
    		do_action('register_wed_vendors' );
    	}
    }
    
    add_action( 'init', 'wed_get_vendor_enable' );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.