• Resolved khit

    (@khittner)


    Website currently unavailable. I am trying to create a custom taxonomy (actually multiple and would also like insight for multiple, as well, please). I used a plugin to generate the code and copied and pasted it into my plugin I am attempting to write. What am I doing wrong? A little preview of code:

    defined( 'ABSPATH' ) or die( 'Oops How did you get here? You Are Not Authorized Access.' );
    
    class odsmbr
    
    { 	
    	//adding the custom taxonomy for ods product databases
    	function register_ods_taxonomy_product() {
    
    	/**
    	 * Taxonomy: product.
    	 */
    
    	$labels = array(
    		"name" => __( "product", "" ),
    		"singular_name" => __( "product", "" ),
    	);
    
    	$args = array(
    		"label" => __( "product", "" ),
    		"labels" => $labels,
    		"public" => true,
    		"hierarchical" => false,
    		"label" => "product",
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'product', 'with_front' => true, ),
    		"show_admin_column" => false,
    		"show_in_rest" => false,
    		"rest_base" => "product",
    		"show_in_quick_edit" => false,
    	);
    	register_taxonomy( "product", array( "odsdatabase" ), $args );
    }
    
    add_action( 'init', 'register_ods_taxonomy_product' );
    
    	//adding the ondash custom post type for member contribution to databases
    	function __construct(){
    		add_action( 'init', array($this, 'ods_custom_post'));
    	}
    	function activate() {
    		// generated a CPT
    		// flush rewrite rules
    		flush_rewrite_rules();
    	}
Viewing 13 replies - 1 through 13 (of 13 total)
  • You don’t say what you are seeing happen, so it is difficult to debug your code.

    You have a class, and a function in a class that is not referred to with a class pointer.
    You are registering a taxonomy with a noun that would typically be a post type. Typically it would be a noun that is a kind of descriptor, like rating or color or feature.
    The register_taxonomy call is for a post type of odsdatabase, but the constructor function refers to some other class function for ods_custom_post.

    Thread Starter khit

    (@khittner)

    Thank you, Joy, for your assistance.

    later on in the code is (showing the odsdatabase):

    
    	//registering the ondash custom post type for member contribution to databases
    	function ods_custom_post(){
    	register_post_type( 'odsdatabase', ['public' => true, 'label' => 'OnDaSh'] );
    	
    	}
    
    The custom post type shows in the admin bar. Just whenever I try the new custom taxonomy it crashes the site. Once I delete the custom taxonomy code and reload the site is fine.  I am copying all of the code, thus far, below:
    
    defined( 'ABSPATH' ) or die( 'Oops How did you get here? You Are Not Authorized Access.' );
    
    class odsmbr
    
    { 	
    	//adding the custom taxonomy for ods product databases
    	function register_ods_taxonomy_product() {
    
    	/**
    	 * Taxonomy: product.
    	 */
    
    	$labels = array(
    		"name" => __( "product", "" ),
    		"singular_name" => __( "product", "" ),
    	);
    
    	$args = array(
    		"label" => __( "product", "" ),
    		"labels" => $labels,
    		"public" => true,
    		"hierarchical" => false,
    		"label" => "product",
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'product', 'with_front' => true, ),
    		"show_admin_column" => false,
    		"show_in_rest" => false,
    		"rest_base" => "product",
    		"show_in_quick_edit" => false,
    	);
    	register_taxonomy( "product", array( "odsdatabase" ), $args );
    }
    
    add_action( 'init', 'register_ods_taxonomy_product' );
    
    	//adding the ondash custom post type for member contribution to databases
    	function __construct(){
    		add_action( 'init', array($this, 'ods_custom_post'));
    	}
    	function activate() {
    		// generated a CPT
    		// flush rewrite rules
    		flush_rewrite_rules();
    	}
    
    	function deactivate() {
    		// flush rewrite rules
    	
    	}
    
    	function uninstall() {
    		// delete CPT
    		// delete all the plugin data from the DB
    		
    	}
    	
    	//registering the ondash custom post type for member contribution to databases
    	function ods_custom_post(){
    	register_post_type( 'odsdatabase', ['public' => true, 'label' => 'OnDaSh'] );
    	
    	}
    	
    }
    
    if( class_exists( 'odsmbr'))
    {
    	$odsmbr = new odsmbr();
    }
    
    // activation
    register_activation_hook( __FILE__, array( $megambr, 'activate'));
    
    // deactivation
    register_deactivation_hook( __FILE__, array( $megambr, 'deactivate'));
    
    // uninstall
    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    @khittner – please use code tags when you post codeblocks (they’re backticks, or use the CODE button in the editor bar)

    Again, you haven’t said what it tells you. You are writing the code, so you need to read the error messages and fix what causes them. If you haven’t already, enable debug mode and install the Debug Bar plugin.

    You have a class, and a function in a class that is not referred to with a class pointer.

    I imagine that this would cause a fatal error.

    Thread Starter khit

    (@khittner)

    I am not sure if I am doing the code thing right. I clicked code in toolbar, typed code and then clicked code in toolbar again. I apologize if I am not doing this right.

    code:
    add_action( 'init', 'register_ods_taxonomy_product' );

    wp response is:

    Your PHP code changes were rolled back due to an error on line 70 of file wp-content/plugins/odsmbr/odsmbr.php. Please fix and try saving again.

    syntax error, unexpected ‘add_action’ (T_STRING), expecting function (T_FUNCTION)

    Thread Starter khit

    (@khittner)

    So if the issue is a function within a class that is not referred to with a class pointer… what is the fix?

    I am very new to this. Any step by step help with proper explanation would be greatly appreciated.

    Sincerely,
    WordPress Novice

    Moderator bcworkz

    (@bcworkz)

    Thanks for using the code button to demarcate code. It makes it much easier to test your code.

    Move your add_action() call to outside of your class declaration. This call is to execute when the file loads, not when the class is instantiated. Even if at instantiation was appropriate, the call must be with in a proper constructor function. You can’t just call it within a class declaration.

    You are also referencing a class method incorrectly when passed to the add_action() call. Class method callbacks to action or filter hooks must be passed as an array, like so:
    add_action( 'init', array('odsmbr', 'register_ods_taxonomy_product') );

    Thread Starter khit

    (@khittner)

    Cleaning up a previous post that bunched in my post comments into the code box. Below, is the complete code:

    defined( 'ABSPATH' ) or die( 'Oops How did you get here? You Are Not Authorized Access.' );
    
    class odsmbr
    
    { 	
    	//adding the custom taxonomy for ods product databases
    	function register_ods_taxonomy_product() {
    
    	/**
    	 * Taxonomy: product.
    	 */
    
    	$labels = array(
    		"name" => __( "product", "" ),
    		"singular_name" => __( "product", "" ),
    	);
    
    	$args = array(
    		"label" => __( "product", "" ),
    		"labels" => $labels,
    		"public" => true,
    		"hierarchical" => false,
    		"label" => "product",
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'product', 'with_front' => true, ),
    		"show_admin_column" => false,
    		"show_in_rest" => false,
    		"rest_base" => "product",
    		"show_in_quick_edit" => false,
    	);
    	register_taxonomy( "product", array( "odsdatabase" ), $args );
    }
    
    add_action( 'init', 'register_ods_taxonomy_product' );
    
    	//adding the ondash custom post type for member contribution to databases
    	function __construct(){
    		add_action( 'init', array($this, 'ods_custom_post'));
    	}
    	function activate() {
    		// generated a CPT
    		// flush rewrite rules
    		flush_rewrite_rules();
    	}
    
    	function deactivate() {
    		// flush rewrite rules
    	
    	}
    
    	function uninstall() {
    		// delete CPT
    		// delete all the plugin data from the DB
    		
    	}
    	
    	//registering the ondash custom post type for member contribution to databases
    	function ods_custom_post(){
    	register_post_type( 'odsdatabase', ['public' => true, 'label' => 'OnDaSh'] );
    	
    	}
    	
    }
    
    if( class_exists( 'odsmbr'))
    {
    	$odsmbr = new odsmbr();
    }
    
    // activation
    register_activation_hook( __FILE__, array( $odsmbr, 'activate'));
    
    // deactivation
    register_deactivation_hook( __FILE__, array( $odsmbr, 'deactivate'));
    
    // uninstall
    • This reply was modified 6 years, 5 months ago by khit.
    Thread Starter khit

    (@khittner)

    @bcworkz… Thank you! I had tried with array and it didn’t work but I had it inside of the class declaration. Up and running again!

    I truly appreciate your patience and explanation!

    Could you take a moment to explain adding multiple taxonomies? I have four more to go. Do I have to do all of the code again one by one?

    hi

    khit

    i have a simple solution for you . are you willing to use the plugin .
    wck search the plugin . it will make multiple taxonomies in your page or posts
    and get the values by using post type =”you name”
    display the post meta and post terms.
    i hope it will help you
    that plugin is also have acf and more features

    Thankls

    • This reply was modified 6 years, 5 months ago by ramvijay.
    Moderator bcworkz

    (@bcworkz)

    Hi khit,
    I’m glad your code is working now. I know how confusing and frustrating it can be at first when learning a new coding language. It’ll get easier, I promise ??

    Essentially, you do need to declare each taxonomy individually. Of course you can simply redo the same code several times over, changing only the taxonomy name. You can also significantly shorten the lines of code by organizing the redundant code into an object or function and feeding the same code different data as required. As a crude example, something like this:

    $taxes = array('product', 'color', 'size', 'fitment',);
    foreach ( $taxes as $tax ) {
      $my_object->register_taxonomy( $tax );
    }

    Where the $my_object->register_taxonomy() method contains all the data, arguments, and code that is common to all of your taxonomy declarations. It would also contain the call to the WP function register_taxonomy(). My example may be rather simplistic, but the same concept can be expanded to accommodate more complexity. For example, if several parameters vary for each taxonomy, instead of declaring an array of names, declare an array of parameter arrays which can all be applied within the standard object method as is required.

    I hope that makes sense. If not, tell us what you are not understanding and we’ll try to better explain.

    Thread Starter khit

    (@khittner)

    Thank you! I appreciate your time, attention to detail and thoroughness! You have been very helpful.

    Thread Starter khit

    (@khittner)

    Thank you, ramvijay. I will look at it

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘creating custom taxonomy for custom post type’ is closed to new replies.