Magento 2, We can create product attribute programmatically using InstallData.php file. Using dynamically create product attribute save the time for creating attribute manually using admin panel. If you are creating an extension package and you need product attribute at that time below code snippet will be helpful for creating Product attribute.
We can create custom category attribute programmatically in Magento 2 using simple module. In our demo we have created a simple text field attribute for a category.
You can see a category attribute in the backend using Catalog -> Categories Page.
Click on Any category and you can see our custom attribute in General Section after the category name.