How to register an attribute to Woocommerce programmatically - woocommerce

I'm not talking about adding an attribute to a product but I want to add an attribute itself..
You can do that from the UI under Products -> Attributes but from the code how is it done (e.i: what functions to call or tables to update)?

An attribute is merely a custom taxonomy, so I think you should be able to do this the same as you'd register any custom taxonomy. WooCommerce prefaces their attributes taxonomy names with 'pa_' (probably standing for product attibute)
add_action( 'init', 'create_product_attribute' );
function create_product_attribute() {
register_taxonomy(
'pa_genre',
'product',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}

Related

rename cpt in wordpress

Hello i have one cpt which is case studies but i want to rename it as portfolio .and i have created 10 post under case studies cpt .. so can i rename it cpt ? actually i want change in url .. currently my url is http://www.praxinfo.com/case-studies-page/whatscrackin/ but i want http://www.praxinfo.com/portfolio/whatscrackin/. so how to do this in wordpress?
In your register_post_type use the rewrite option to change the url but not the actual name of the cpt. This way, you won't run into database problems and your users will still see the new name.
Example from the WordPress codex:
add_action( 'init', 'create_posttype' );
function create_posttype() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
Here, the url would be /products/ even though the name of the cpt is acme_product.

Change custom post type url

I've a custom post type by name 'Portfolio'. It's generating the url as /portfolio/xxx when I make any post. I want to change it to 'Products'.
Change custom post-type rewrite
I've tried this but it doesn't suppose to work.
I am presuming you have added the custom post type yourself, in which case it is easy.
When you register a new post type, you can set any rewrite rules for that post type to follow as part of the arguments used in the register_post_type( 'name', $args ) function.
If your post type is available on the front end and is public, then by default WordPress will use the custom post name as the slug. You can override this as follows:
$args = array(
// your other arguments
'rewrite' => array(
'slug' => 'products', // use this slug instead of post type name
'with_front' => FALSE // if you have a permalink base such as /blog/ then setting this to false ensures your custom post type permalink structure will be /products/ instead of /blog/products/
),
);
register_post_type( 'portfolio', $args );
The other arguments you can use are documented at http://codex.wordpress.org/Function_Reference/register_post_type
Please see the documentation on URLs of Namespaced Custom Post Types Identifiers. One of the options available to you is to add a 'rewrite' argument to register_post_type(). From the docs:
When you namespace a custom post type identifier and still want to use a clean URL structure, you need to set the rewrite argument of the register_post_type() function. For example:
add_action( 'init', 'create_posttype' );
function create_posttype() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
You'll then likely need to login to the admin and re-save your permalinks.
Use the rewrite property
$args= array(
// other settings
'rewrite' => array( 'slug' => 'Products' ),
);
register_post_type( 'portfolio', $args);

Wordpress Custom Post Type Template

I am working on a wordpress website. I need to add custom Post types. I have created a custom post type products which has a taxonomy product_type which is similar to category . There are various taxonomy value for product_type . Some of them are flowers, extracts etc .
Now I am trying to visit this link http://farma.mechadigital.net/products/product_type/flowers/ and it doesnt work for me.
I have added some files.
archive-products => This should be the custom post template
taxonomy-product_type.php => This should be the taxonomy Template
taxonomy-product_type-flowers.php => This should be the template for the term value flowers
Here is the code that I have included in functions.php. I dont know where I am doing it wrong.
functions.php
function farma_products() {
$labels = array(
// List of arguments
);
$args = array(
// list of arguments
'rewrite' => array( 'slug' => 'products' ),
);
register_post_type( 'products', $args );
flush_rewrite_rules(false);
}
add_action( 'init', 'farma_products_type' );
function farma_products_type() {
register_taxonomy(
'product_type',
'products',
array(
'label' => __( 'Product Type' ),
'rewrite' => array( 'slug' => 'products/product_type' ),
'hierarchical' => true,
)
);
}

How to create new form on wordpress admin panel?

I want to create new form on wordoress admin panel for store data by admin, for example i have one student detail and i want store through admin panel.
Please let me know.....
Best
Abhishek
Why dont you create a new post type? http://codex.wordpress.org/Post_Types
You can set this to not show publicly. You could some something like this:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => false,
'has_archive' => true,
)
);
}

Move WordPress posts and related taxonomy to custom post type & taxonomy

I have a clean WordPress installation using Twenty Eleven, with no plugins or other modifications. The blog contains approximately 800 posts with related tags and categories.
I'd like to move all these posts to a custom post type. By adding the following code to my functions.php my blog now has the custom post type installed and working perfectly.
//Add custom post type
add_action( 'init', 'create_en_post_type' );
function create_en_post_type() {
register_post_type( 'en_post', array(
'labels' => array(
'name' => __( 'English Posts' ),
'singular_name' => __( 'English Post' )
),
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'en'),
'supports' => array(
'title',
'editor',
'author',
'excerpt',
'comments',
'custom-fields'
)
));
}
//Add belonging custom taxonomy
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'english_categories', 'en_post', array( 'hierarchical' => true, 'label' => 'English Categories', 'query_var' => true, 'rewrite' => true ) );
register_taxonomy( 'english_tags', 'en_post', array( 'hierarchical' => false, 'label' => 'English Tags', 'query_var' => true, 'rewrite' => true ) );
}
Now all I need is to change the regular post type to my custom post type. I've managed to move the posts using the plugin Convert Post Types, but my taxonomies aren't converted.
I realize I could just create similar custom categories and assign the custom posts to them, but what about tags? How can I convert these (automatically, 1200 tags)?
I've tried manually messing around in the database, but I can't make it work.
You should change the ID's in the database, take a long look at the phpmyadmin
wp_term_taxonomy
wp_terms
wp_term_relationships
In the terms table you will find the id of the old and new taxamonies
in term_taxonomy replace all old term_id with the new.
SQL:
UPDATE `wp_term_relationships` SET `term_id` = REPLACE(`term_id`, /*old id*/, /*new id*/);
Do make a backup before you start

Resources