With help i've made custom field on WooCommerce's category, but i want to make it media field (to pick cover image and upload directly from form).
I'm using "How do I add custom fields to the categories in Woocommerce?"
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="">
<p class="description"><?php _e( 'Enter a value for this field','category-cover' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[category_cover]"><?php _e( 'Category Cover', 'category-cover' ); ?></label></th>
<td>
<input type="text" name="term_meta[category_cover]" id="term_meta[category_cover]" value="<?php echo esc_attr( $term_meta['category_cover'] ) ? esc_attr( $term_meta['category_cover'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter an URL for category cover','category-cover' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
I dont know what should I tell more, but i will be really thankful for help me! :)
Related
I’m trying to add an extra field on dokan registration for vendors to upload an identification document.. i found the code specified for adding an extra field to the form but it is for a "text" input type… i changed the input type from "text" to "file"
<p class="form-row form-group form-row-wide">
<label for="veri-file"><?php esc_html_e( 'Upload Verification ID', 'dokan-custom-codes' ); ?><span class="required">*</span></label>
<input type="file" class="verifile" name="veri_file" id="veri_file" accept="image/png, image/jpeg" value="upload"<?php if ( ! empty( $postdata['veri_file'] ) ) echo esc_attr($postdata['veri_file']); ?>" required="required" />
</p>
and that worked… but the code to save and display the field content on the user backend doesn’t show the image just the same upload box
// save id verification field
I also used below code to show in admin side but image not shown not move to folders
function dokan_custom_seller_registration_required_fields( $required_fields ) {
$required_fields['veri_file'] = __( 'Please upload a valid means of Identification', 'dokan-custom' );
return $required_fields;
};
add_filter( 'dokan_seller_registration_required_fields', 'dokan_custom_seller_registration_required_fields' );
function dokan_custom_new_seller_created( $vendor_id, $dokan_settings ) {
$post_data = wp_unslash( $_POST );
$veri_file = $post_data['veri_file'];
update_user_meta( $vendor_id, 'dokan_custom_veri_file', $veri_file );
}
add_action( 'dokan_new_seller_created', 'dokan_custom_new_seller_created', 10, 2 );
/* Add custom profile fields (call in theme : echo $curauth->fieldname;) */
add_action( 'dokan_seller_meta_fields', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<?php if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! user_can( $user, 'dokandar' ) ) {
return;
}
$gst = get_user_meta( $user->ID, 'dokan_custom_veri_file', true );
?>
<tr>
<th><?php esc_html_e( 'Upload Verification ID', 'dokan-lite' ); ?></th>
<td>
<input type="file" name="veri_file" class="verifile" value="<?php echo esc_attr($gst); ?>"/>
<img src=".$gst." height=200 width=300 />
</td>
</tr>
echo "<img src=".$gst." height=200 width=300 />";
<?php
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
update_usermeta( $user_id, 'dokan_custom_veri_file', $_POST['veri_file'] );
}
I've created a 'color' custom field for pa_color taxonomy terms, by native ways as follows:
This is my code:
add_action( 'pa_color_add_form_fields', 'themename_pa_color_add_term_fields' );
function themename_pa_color_add_term_fields( $taxonomy ) {
?>
<div class="form-field">
<label for="themename_pa_color">PA Color</label>
<input type="color" name="themename_pa_color" id="themename_pa_color" class="wpColorChoose" />
<p>Field description may go here.</p>
</div>
<?php
}
add_action( 'pa_color_edit_form_fields', 'themename_pa_color_edit_term_fields', 10, 2 );
function themename_pa_color_edit_term_fields( $term, $taxonomy ) {
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo '<tr class="form-field">
<th>
<label for="themename_pa_color">PA Color</label>
</th>
<td>
<input name="themename_pa_color" id="themename_pa_color" type="color" class="wpColorChoose" value="' . esc_attr( $value ) .'" />
<p class="description">Field description may go here.</p>
</td>
</tr>';
}
add_action( 'created_pa_color', 'themename_pa_color_save_term_fields' );
add_action( 'edited_pa_color', 'themename_pa_color_save_term_fields' );
function themename_pa_color_save_term_fields( $term_id ) {
update_term_meta(
$term_id,
'themename_pa_color',
sanitize_text_field( $_POST[ 'themename_pa_color' ] )
);
}
How can I get their values to the place I need?
When trying to use $term->themename_pa_color, it doesn't work.
It works with name and description default fields, like $term->name or $term->description, but not with my field.
That's how I created it, it correctly saves values.
Your code is adding a custom field to the terms from "pa_color" taxonomy… So this is about term meta data which is never included in the WP_Term Object, and so not accessible as a property from that WP_Term Object.
So the answer is in your code. You need to use the WordPress function get_term_meta() like in your own code (in your 2nd function):
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo $value; // Display value
(where $term->term_id is the term Id and themename_pa_color the meta key)…
I need to add a list of check-boxes to custom taxonomy add/edit form.
I have this code for adding a text field for custom taxonomy form in my plugin and it works fine:
<?php
function taxonomy_edit_meta_field($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Term:' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
How I can add a list of check-boxes in the same way?
There were a problem with the saving not checked check-box input.
I have fixed it with input type="hidden" with the same value of "name" attribute, as in check-box input.
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Color:' ); ?></label></th>
<td>
<input type="hidden" value="0" name="term_meta[pa_color_attr]">
<input type="checkbox" <?php echo (!empty($term_meta['pa_color_attr']) ? ' checked="checked" ' : 'test'); ?> value="1" name="term_meta[pa_color_attr]" />
</td>
</tr>
you need to change the name of the hook.
add_action( 'edited_custom_tax', 'save_taxonomy_custom_meta', 10, 2 );
i make a custom field in categories, it save and update data successfully, but i want to show the value of custom field data in archive page, i search a lot about this but in vain
Please help me
here is my code
Create custom field:
add_action ( 'category_add_form_fields', 'extra_field');
add_action ( 'category_edit_form_fields', 'extra_field');
function extra_field($term) { //check for existing featured ID
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id");
?>
<table width="100%" border="0" cellspacing="3" cellpadding="0" style="margin-bottom:20px;">
<tr>
<td><strong>Image</strong></td>
</tr>
<tr>
<td><input type="text" size="40" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>" /></td>
</tr>
<tr>
<td><p>A quick brown fox jumps over the lazy dog.</p></td>
</tr>
</table>
<?php
}
Save / Update data:
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = $_POST['term_meta'];
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_category', 'save_taxonomy_custom_meta' );
add_action( 'create_category', 'save_taxonomy_custom_meta' );
One more thing, can i make an extra field in db in wp_terms, because it save in wp_options
Use this
first of all you get category id on taxonomy page.I suppose one category assign to each post.
$t_id = $term_id;
Then get value using this
get_option( "taxonomy_".$t_id );
I thanked to #yatendra to help me i got an idea from his answer so its work
here is answer
$queried_object = get_queried_object();
$t_id = $queried_object->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
echo "<img src=".$term_meta['custom_term_meta']." />";
I want to customize my category section so have use below code and it is working perfectly without any issue..
In
function.php
add_action ( 'edit_category_form_fields', function( $tag ){
$cat_title = get_term_meta( $tag->term_id, '_pagetitle', true );
?>
<tr class='form-field'>
<th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
<td>
<input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
<p class='description'><?php _e('Title for the Category '); ?></p>
</td>
</tr> <?php
});
add_action ( 'edited_category', function() {
if ( isset( $_POST['cat_title'] ) )
update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});
and call in index.php page for fronted
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
foreach( $categories as $category ) {
if($category->name !="Uncategorized")
{
$cat_title = get_term_meta( $category->term_id, '_pagetitle', true );
echo '
<div class="col-md-4">' . $category->name . '</div>
<div class="col-md-4">' . $category->description . '</div>
';
}
}
?>
Hi I have a site with a Custom Post Type of 'Researcher', within that I have a taxonomy called 'Supervisors', and within that a meta field named 'Job Title'. I am trying to figure out how to display this 'Job Title' in the front end.
The code for the meta field (from functions.php) is as follows:
// Edit Supervisor Taxonomy
// Add term page
function jobtitle_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</div>
<?php
}
add_action( 'supervisor_add_form_fields', 'jobtitle_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function jobtitle_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[jobtitle]"><?php _e( 'Job Title', 'jobtitle' ); ?></label></th>
<td>
<input type="text" name="term_meta[jobtitle]" id="term_meta[jobtitle]" value="<?php echo esc_attr( $term_meta['jobtitle'] ) ? esc_attr( $term_meta['jobtitle'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','jobtitle' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'supervisor_edit_form_fields', 'jobtitle_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_supervisor', 'save_taxonomy_custom_meta', 10, 2 );
// End Edit Supervisor Taxonomy
Image showing the backend — http://d.pr/i/TS13
You will need:
The post ID of the researcher. On the single-researcher.php page (if your post type is named researcher) you should be able to use $post->ID to obtain the post ID of the researcher.
The name of the taxonomy. This can be obtained by reading the URL of the screenshot you posted previously. It should say: ...edit-tags.php?taxonomy=supervisors&.... I'll assume the taxonomy is named supervisors.
If you're on the single-researcher.php page, this might work... Note: $supervisor_tags is an array of tags for that researcher. Each researcher could have 0, 1, or more supervisors, so you might want to loop through them:
global $post;
$supervisor_tags = get_the_terms($post->ID, 'supervisors');
echo "<h2>Supervisors:</h2>";
foreach ($supervisor_tags as $tag){
$term_meta = get_option('taxonomy_' . $tag->term_id);
echo "<p>Name: " . $tag->name . " - Job Title: " . $term_meta['jobtitle'] . "</p>";
}
You can echo "<pre>"; print_r($variable); echo "</pre>"; to see what the $variable array looks like, if you're having trouble.