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 );
Related
I have tried this method creating a separate plugin, but it is not working, can anyone help me out with this? Please. I have a restricted site. without filling out proper legal documents the user will not be approved from the WordPress dashboard. But when submitting for registration everything is working well, but the file is not uploading or not showing on the dashboard use profile page.
`
add_action( 'register_form', 'crf_registration_form' );
function crf_registration_form() {
$year = ! empty( $_POST['legal_documents'] ) ? intval( $_POST['legal_documents'] ) : '';
?>
<p>
<label for="legal_documents"><?php esc_html_e( 'National ID/Passport/Driving License', 'crf' ) ?><br/>
<input type="file"
id="legal_documents"
name="legal_documents"
value="<?php echo esc_attr( $documents ); ?>"
class="input"
/>
</label>
</p>
<?php
}
add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['legal_documents'] ) ) {
$errors->add( 'legal_documents_error', __( '<strong>ERROR</strong>: Please upload your legal documents to have active account.', 'crf' ) );
}
return $errors;
}
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
if ( ! empty( $_POST['legal_documents'] ) ) {
update_user_meta( $user_id, 'legal_documents', intval( $_POST['legal_documents'] ) );
}
}
/**
* Back end registration
*/
add_action( 'user_new_form', 'crf_admin_registration_form' );
function crf_admin_registration_form( $operation ) {
if ( 'add-new-user' !== $operation ) {
// $operation may also be 'add-existing-user'
return;
}
$year = ! empty( $_POST['legal_documents'] ) ? intval( $_POST['legal_documents'] ) : '';
?>
<h3><?php esc_html_e( 'Verification documents', 'crf' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="legal_documents"><?php esc_html_e( 'legal_documents', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
<td>
<input type="file"
id="legal_documents"
name="legal_documents"
value="<?php echo esc_attr( $documents ); ?>"
class="regular-text"
/>
</td>
</tr>
</table>
<?php
}
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors( $errors, $update, $user ) {
if ( $update ) {
return;
}
if ( empty( $_POST['legal_documents'] ) ) {
$errors->add( 'legal_documents_error', __( '<strong>ERROR</strong>: Please upload your valid documents', 'crf' ) );
}
}
add_action( 'edit_user_created_user', 'crf_user_register' );
/**
* Profile Display
*/
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) {
?>
<h2><?php esc_html_e( 'Verification documents', 'crf' ); ?></h2>
<table class="form-table">
<tr>
<th><label for="legal_documents"><?php esc_html_e( 'Legal Documents', 'crf' ); ?></label></th>
<td><?php echo esc_html( get_the_author_meta( 'legal_documents', $user->ID ) ); ?></td>
</tr>
</table>
<?php
}
`
I have tried this code snippets but the file is not receiving on the WordPress user page, it is showing 0.
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! :)
I have researched and followed many tutorials on how to create meta-boxes from scratch, and I have created a working repeating meta-box using the helpful code from this post: Creating a "repeater meta-box" without a Plugin in WordPress
However, I am getting stuck when I try to add more inputs and/or textareas. I can't get the values to save in Wordpress and I also am stuck on how to display the values on my site. It seems that there are very many tutorials on how to create the meta boxes but not how to display the content that the user saves in the meta boxes.
Here is my current code for the custom post type and repeater meta box:
<?php
function llevel_create_post_expertise() {
register_post_type( 'expertise',
array(
'labels' => array(
'name' => __( 'Expertise' ),
),
'public' => true,
'hierarchical' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
),
'taxonomies' => array(
'post_tag',
'category',
)
)
);
register_taxonomy_for_object_type( 'category', 'expertise' );
register_taxonomy_for_object_type( 'post_tag', 'expertise' );
}
add_action( 'init', 'llevel_create_post_expertise' );
add_action('admin_init', 'gpm_add_meta_boxes', 2);
function gpm_add_meta_boxes() {
add_meta_box( 'gpminvoice-group', 'Custom Repeatable', 'Repeatable_meta_box_display', 'page', 'normal', 'default');
}
/* Repeatable Meta Box */
function Repeatable_meta_box_display() {
global $post;
$gpminvoice_group = get_post_meta($post->ID, 'customdata_group', true);
wp_nonce_field( 'gpm_repeatable_meta_box_nonce', 'gpm_repeatable_meta_box_nonce' );
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
$( '#add-row' ).on('click', function() {
var row = $( '.empty-row.screen-reader-text' ).clone(true);
row.removeClass( 'empty-row screen-reader-text' );
row.insertBefore( '#repeatable-fieldset-one tbody>tr:last' );
return false;
});
$( '.remove-row' ).on('click', function() {
$(this).parents('tr').remove();
return false;
});
});
</script>
<table id="repeatable-fieldset-one" width="100%">
<tbody>
<?php
if ( $gpminvoice_group ) :
foreach ( $gpminvoice_group as $field ) {
?>
<tr>
<td width="15%">
<input type="text" placeholder="Title" name="TitleItem[]" value="<?php if($field['TitleItem'] != '') echo esc_attr( $field['TitleItem'] ); ?>" /></td>
<td width="70%">
<textarea placeholder="Description" cols="55" rows="5" name="TitleDescription[]"> <?php if ($field['TitleDescription'] != '') echo esc_attr( $field['TitleDescription'] ); ?> </textarea></td>
<td width="15%"><a class="button remove-row" href="#1">Remove</a></td>
</tr>
<?php
}
else :
// show a blank one
?>
<tr>
<td>
<input type="text" placeholder="Title" title="Title" name="TitleItem[]" /></td>
<td>
<textarea placeholder="Description" name="TitleDescription[]" cols="55" rows="5"> </textarea>
</td>
<td><a class="button cmb-remove-row-button button-disabled" href="#">Remove</a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td>
<input type="text" placeholder="Title" name="TitleItem[]"/></td>
<td>
<textarea placeholder="Description" cols="55" rows="5" name="TitleDescription[]"></textarea>
</td>
<td><a class="button remove-row" href="#">Remove</a></td>
</tr>
</tbody>
</table>
<p><a id="add-row" class="button" href="#">Add another</a></p>
<?php
}
add_action('save_post', 'custom_repeatable_meta_box_save');
function custom_repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST['gpm_repeatable_meta_box_nonce'] ) ||
! wp_verify_nonce( $_POST['gpm_repeatable_meta_box_nonce'], 'gpm_repeatable_meta_box_nonce' ) )
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'customdata_group', true);
$new = array();
$invoiceItems = $_POST['TitleItem'];
$prices = $_POST['TitleDescription'];
$count = count( $invoiceItems );
for ( $i = 0; $i < $count; $i++ ) {
if ( $invoiceItems[$i] != '' ) :
$new[$i]['TitleItem'] = stripslashes( strip_tags( $invoiceItems[$i] ) );
$new[$i]['TitleDescription'] = stripslashes( $prices[$i] ); // and however you want to sanitize
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'customdata_group', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'customdata_group', $old );
}
Any help is greatly appreciated!
I am trying to add affiliate links to the Woocommerce variations. The idea is to have a unique http link/URL(affiliate link) for each product variation. A link/URL that I can enter in the Woocommerce backend and when a customer clicks on the 'Add to cart' button, a new web-page is loaded (based on the corresponding URL of that product)
This can be easily achieved for the products with no variations. But there is no such out of the box functionality for achieving the same thing for the products with variations.
I came across a solution over here
I implemented the code but whenever I try to enter the URL in the backend and try to save my changes, the link disappears, don't know what's going wrong with my code.
// Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
// Save Fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'Affiliate URL', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_affiliate_url[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_affiliate_url'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_affiliate_url[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_affiliate_url'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_affiliate_url', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
//front-end variations
function woocommerce_variable_add_to_cart() {
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>"method="post" enctype='multipart/form-data'>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<?php
if(!empty($value['attributes'])){
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
<?php
}
}
?>
<table>
<tbody>
<tr>
<td>
<b><?php echo implode('/', $value['attributes']);?></b>
</td>
<td>
<?php echo $value['price_html'];?>
</td>
<td>
<a class="single_add_to_cart_button button alt" target="_blank" href="<?php echo get_post_meta($value['variation_id'], '_my_affiliate_url', true); ?>" ><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product->product_type); ?></a>
</td>
</tr>
</tbody>
</table>
</form>
<?php
}
}
It's been a while, so you may have resolved this. But anyone in the future who comes here looking for an answer like I did.
Replace:
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
with:
add_action( 'woocommerce_save_product_variation', 'variable_fields_process', 10, 2 );
Saving variants changed as of WooCommerce 2.4.4
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>
';
}
}
?>