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!
Related
this codes diplay prodcuts checkbox and load them in template
but i want to display the value of the checked items only in backend admin new column
so i can know exactly which items the customer has selected
product displying with quantity and name i just want to display those values in my admin dashbord
new column
codes:
<tr>
<td class="row_head" valign="top">
<?php echo esc_html__( 'Produits:', 'woocommerce-smart-refunder' ); ?>
</td>
<td class="row_headxx">
<label for="refund_type_prd">
<?php
if ( sizeof( $products ) > 0 ) {
foreach( $products as $item ) { ?>
<input type="checkbox" name="wc_products" class="wc_products" id="wc_products<?php echo ($item['name']); ?>" value="<?php echo $product_id;?>"<?php if(in_array($product_id,$data) ){echo "checked";} ?>><span class="gdjhd57djk3"><?php echo __(esc_html($item['name']), 'woocommerce-smart-refunder'); ?><?php if ( isset ( $checkboxMeta['wc_products'] ) ) checked( $checkboxMeta['wc_products'][0], 'yes' ); ?></span>
<?php echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );?>
<?php echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<div class="hhdjdk654b">'.'<span>Réf:<span>'. '<a class=hhsjdh533H" href="%s">%s</a>'.'</div>', $product_permalink, $item->get_id() ) : $item->get_id(), $item, $is_visible ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped;?>
<div class="quantity">
<input class="minus" type="button" value="-">
<input type="number" step="<?php echo esc_attr( $step ); ?>" <?php if ( is_numeric( $min_value ) ) : ?>min="<?php echo esc_attr( $min_value ); ?>"<?php endif; ?> <?php if ( is_numeric( $max_value ) ) : ?>max="<?php echo esc_attr( $max_value ); ?>"<?php endif; ?> name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $item['qty']); ?>" title="<?php _ex( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" />
<input class="plus" type="button" value="+">
</div> <?php
//echo $product->get_id();
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('form.cart').on( 'click', 'button.plus, button.minus', function() {
// Get current quantity values
var qty = $( this ).closest( 'form.cart' ).find( '.qty' );
var val = parseFloat(qty.val());
var max = parseFloat(qty.attr( 'max' ));
var min = parseFloat(qty.attr( 'min' ));
var step = parseFloat(qty.attr( 'step' ));
// Change the value if plus or minus
if ( $( this ).is( '.plus' ) ) {
if ( max && ( max <= val ) ) {
qty.val( max );
} else {
qty.val( val + step );
}
} else {
if ( min && ( min >= val ) ) {
qty.val( min );
} else if ( val > 1 ) {
qty.val( val - step );
}
}
});
});
</script>
<?php
}
}
?>
</label>
<br>
</td>
</tr>
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 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>
';
}
}
?>
I seem to have a problem with my meta box saving, not totally sure what I've done wrong. Any help would be greatly appreciated!
Heres my functions file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'golf.php')
{
add_meta_box (
'golf-times',
'Golf Opening Times & Prices',
'cd_meta_box_cb',
'page',
'normal',
'high'
);
}
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$times = isset( $values['golf_meta_box_times'] ) ? esc_attr( $values['golf_meta_box_times'][0] ) : '';
$prices = isset( $values['golf_meta_box_prices'] ) ? esc_attr( $values['golf_meta_box_prices'][0] ) : '';
wp_nonce_field( 'golf_meta_box_nonce', 'meta_box_nonce' );
?>
<div style="overflow: hidden;">
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_times">Opening Times</label></p>
<p><textarea type="text" name="golf_meta_box_times" id="golf_meta_box_times" rows="5" style="width: 90%;" value="<?php echo $times; ?>"> </textarea></p>
</div>
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_prices">Prices</label></p>
<p><textarea type="text" name="golf_meta_box_prices" id="golf_meta_box_prices" rows="5" style="width: 90%;" value="<?php echo $prices; ?>"> </textarea></p>
</div>
</div>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'golf_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['golf_meta_box_times'] ) )
update_post_meta( $post_id, 'golf_meta_box_times', wp_kses( $_POST['golf_meta_box_times'], $allowed ) );
if( isset( $_POST['golf_meta_box_prices'] ) )
update_post_meta( $post_id, 'golf_meta_box_prices', wp_kses( $_POST['golf_meta_box_prices'], $allowed ) );
}
?>
If anyone has a clue how to make this save it would be a great help!
I am also having issues making the data display - but I can only assume thats where it isnt actually saving haha!
Cheers
functions.php cannot output HTML as far as i know.
wrap your html in a string and return it, your file might be returning errors.
Your add_meta_box code seems to be correct, the condition might not be met, are you sure you are sending data over get?