WordPress.org

Ready to get started?Download WordPress

Forums

Custom types and related actions (2 posts)

  1. ramigb
    Member
    Posted 1 year ago #

    Hello,

    i am capable of creating my own site in "bigger" CMSs or Frameworks or even from scratch, but i truly respect what the folks at wordpress have done and i want to use it so I was wondering, with having global* actions like "save_details", isn't there anyway to make sure variables won't collide? i mean, yeah i will name my variables with meaningful names, i might even do a switch case to check what's the custom type i am working with and execute a callback function, but why not add another argument to the add_action function that allows it to work only on a specific type or custom type! what if i just want to save my "Cars" custom field without having to worry about the variables scope!

    tl;dr what techniques do you use/suggest for dealing with numerous custom types with numerous custom fields?

    Rami.

    * I am not sure about the scope of add_post function though.

  2. bcworkz
    Member
    Posted 1 year ago #

    For actions, it would be difficult to anticipate all the possible constraints a developer may wish to apply. I think it's better left to the developer to define the scope they desire and implement it.

    The only collision avoidance suggestion I'm aware of is to preface names with an unlikely string such as the initials of your plugin or theme name. This is fine for internal names, but is messy for names that are part of the user experience. At least for fields, you can be highly descriptive, but for post types, this is challenging since we are limited to 20 characters.

    About all you can really do is be aware of the collision issue and avoid highly generic names. Also perhaps organize your code so it's easy to change names in case of conflict. Perhaps by using defined constants, filter hooks, pluggable code, etc. In particular a way to change names that will not get overwritten during an update, similar to how child themes are implemented.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.