Saving metabox value not working [WP-PHP] - php

I'm trying to save a metabox input but it doesn't seem to work. I'm working with arrays (as I need my metabox to have 60 rows), so I assume the problem lies within them.
This is my metabox function for the admin (which works correctly showing the info I wanna show):
function mock_metabox() {
global $post;
// Nonce field
wp_nonce_field( basename( __FILE__ ), 'mock_fields' );
// init counter for meta array
$contadorglobal = 1;
$selecciones = array();
$equipos = array();
$equiposog = array();
while ( $contadorglobal <= 60 ){
$selecciones[$contadorglobal-1] = get_post_meta( $post->ID, '_seleccion_' . $contadorglobal, true );
$equipos[$contadorglobal-1] = get_post_meta( $post->ID, '_equipo_' . $contadorglobal, true );
$equiposog[$contadorglobal-1] = get_post_meta( $post->ID, '_equipoog_' . $contadorglobal, true );
$contadorglobal++;
}
// Output the fields
?>
<h3> Informacion del Mock </h3>
<table>
<tr>
<th> # </th>
<th> Jugador </th>
<th> Equipo </th>
<th> Equipo Original </th>
</tr>
<?
$contador = 1;
$teams = get_posts( array(
'post_type' => 'team',
'orderby' => 'title',
'order' => 'ASC',
'numberposts' => -1,
'post_status' => 'publish'
) );
while ( $contador <= 60 ){
?>
<tr>
<td><?php echo $contador ?></td>
<td><input type="text" name="<? 'jugador_' . $contador ?>" value="<?php echo $selecciones[$contador-1] ; ?>" />
<td><select name="<? 'equipo_' . $contador?>" ><?
foreach ( $teams as $team ) { ?>
<option value="<?php echo $team->ID; ?>" <?php checked( $equipos[$contador-1], $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
<td><select name="<? 'equipoog_' . $contador ?>" ><?
foreach ( $teams as $team ) { ?>
<option value="<?php echo $team->ID; ?>" <?php checked( $equiposog[$contador-1], $team->ID ); ?> > <?php echo $team->post_title; ?> </option> <? } ?> </select> </td>
<? $contador++; ?>
</tr>
<?php } ?>
</table>
<?}
And this is the saving function (I thought the problem was with the while, but I tried removing it and it doesn't save any info either).
function mock_save_meta_box_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['mock_fields'] ) || !wp_verify_nonce( $_POST['mock_fields'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// guarda tipo traspaso
for ($contadorid = 1; $contadorid <= 60; $contadorid++) {
if ( isset( $_REQUEST['jugador_' . $contadorid] ) ) {
update_post_meta( $post_id, '_seleccion_' . $contadorid, $_POST['jugador_' . $contadorid] );
}
if ( isset( $_REQUEST['equipo_' . $contadorid] ) ) {
update_post_meta( $post_id, '_equipo_' . $contadorid, $_POST['equipo_' . $contadorid] );
}
if ( isset( $_REQUEST['equipoog_' . $contadorid] ) ) {
update_post_meta( $post_id, '_equipoog_' . $contadorid, $_POST['equipoog_' . $contadorid] );
}
}
}
add_action( 'save_post_mock', 'mock_save_meta_box_data' );
Any ideas? Thanks in advance!

Related

Modify Wordpress Custom Price Plug-In

I tried modifying this plug-in for WordPress:
https://easydigitaldownloads.com/downloads/custom-prices/?utm_campaign=documentation&utm_source=helpscout&utm_medium=doc&utm_term=1975-custom-prices-overview
What I'm trying to achieve is moving the $ sign next to the custom price field or alternatively removing it completely.
/*
* Hook into add to cart post
*/
function edd_cp_purchase_link_top( $download_id ) {
global $edd_options;
$default_price = get_post_meta( $download_id, 'edd_cp_default_price', true );
$min_price = edd_format_amount ( get_post_meta( $download_id, 'edd_cp_min', true ) );
$custom_price = isset( $_GET ['cp_price'] ) ? edd_format_amount( $_GET ['cp_price'] ) : '';
$button_text = get_post_meta( $download_id, 'cp_button_text', true );
if ( empty( $custom_price ) && ! empty( $default_price ) ) {
$custom_price = edd_format_amount( $default_price );
}
if ( edd_cp_has_custom_pricing( $download_id ) && ! edd_single_price_option_mode( $download_id ) ) {
$wrapper_display = '';
$download = new EDD_Download( $download_id );
if ( edd_item_in_cart( $download_id, array() ) && ( ! $download->has_variable_prices() || ! $download->is_single_price_mode() ) ) {
$wrapper_display = 'style="display:none;"';
} ?>
<p class="edd-cp-container" <?php echo $wrapper_display; ?>>
<?php
echo __( 'Name your price', 'edd_cp' );
if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<?php echo edd_currency_filter( '' ); ?> <input type="text" name="edd_cp_price" class="edd_cp_price" value="<?php echo esc_attr( $custom_price ); ?>" size="30" data-min="<?php echo $min_price; ?>" style="width: 40px;" data-default-text="<?php echo esc_attr( $button_text ); ?>" />
<?php else : ?>
<input type="text" name="edd_cp_price" class="edd_cp_price" value="<?php echo esc_attr( $custom_price ); ?>" size="30" data-min="<?php echo $min_price; ?>" style="width: 80px;" data-default-text="<?php echo esc_attr( $button_text ); ?>" /><?php echo edd_currency_filter( '' );
endif;
$min_no_format = floatval( $min_price );
if( !empty( $min_no_format ) ) {
echo ' <small>(min '.edd_currency_filter( ( $min_price ) ).')</small>';
} ?>
</p>
<?php
}
}
add_filter( 'edd_purchase_link_top', 'edd_cp_purchase_link_top' );
/*
* Add additional list item if variable pricing is enabled
*/
function edd_cp_after_price_options_list( $key, $price, $download_id ) {
if( ! edd_cp_has_custom_pricing( $download_id ) ) {
return;
}
if ( ! edd_single_price_option_mode( $download_id ) ) {
return;
}
global $edd_options;
$default_price_option = edd_get_default_variable_price( $download_id );
$display = 'none';
if ( $key === $default_price_option ) {
$display = 'block';
} ?>
<div class="edd-cp-price-option-wrapper" style="display: <?php echo esc_attr( $display ); ?>;">
<?php
echo __( 'Name your price', 'edd_cp' );
if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<?php echo edd_currency_filter( '' ); ?> <input type="text" name="edd_cp_price[<?php echo esc_attr( $key ); ?>]" class="edd_cp_price" value="<?php echo esc_attr( $price['amount'] ); ?>" size="30" data-min="" />
<?php else : ?>
<input type="text" name="edd_cp_price[<?php echo esc_attr( $key ); ?>]" class="edd_cp_price" value="<?php echo esc_attr( $price['amount'] ); ?>" size="30" data-min="" data-default-text="<?php echo esc_attr( $button_text ); ?>" /><?php echo edd_currency_filter( '' );
endif; ?>
</div>
<?php }
add_action( 'edd_after_price_option', 'edd_cp_after_price_options_list', 10, 3 );
See the attached image. This is how it currently looks.
I really appreciate your help!

Reset/Refresh Search Form Button in WordPress Theme

First time posting. I appreciate any help in advance. I really appreciate it.
I am currently work with a wordpress theme to search villas around the world and I would love to add a reset button that would clear the fields, drop downs and check boxes that are populated by a custom fields app. The normal reset button does not work and since I am teaching myself PHP on the fly I am at a loss. Here is the link to the page and I will paste some of the code so you get an idea of what I am looking at.
I really appreciate any help since the theme author has been blowing me off.
LINK
<?php
global $realty_theme_option, $wp_query;
?>
<form class="property-search-form border-box" action="<?php if ( tt_page_id_template_search() ) { echo get_permalink( tt_page_id_template_search() ); } ?>">
<div class="row">
<?php if ( isset( $realty_theme_option['property-search-results-page'] ) && empty( $realty_theme_option['property-search-results-page'] ) ) { ?>
<div class="col-xs-12" style="margin-bottom: 1em">
<p class="alert alert-info"><?php esc_html_e( 'Please go to "Appearance > Theme Options > Pages" and set the page you want to use as your property search results.', 'realty' ); ?></p>
</div>
<?php } ?>
<?php
// Form select classes
$form_select_class = 'form-control';
if ( $realty_theme_option['enable-rtl-support'] || is_rtl() ) {
$form_select_class .= ' chosen-select chosen-rtl';
} else {
$form_select_class .= ' chosen-select';
}
$acf_field_array = array();
if ( isset( $realty_theme_option['property-search-features'] ) && ! tt_is_array_empty( $realty_theme_option['property-search-features'] ) ) {
$property_search_features = $realty_theme_option['property-search-features'];
} else {
$property_search_features = null;
}
$raw_search_params = get_query_var( 'property_search_parameters' );
if ( ! tt_is_array_empty( $raw_search_params ) ) {
$search_parameters = $raw_search_params;
} else if ( isset( $realty_theme_option['property-search-parameter'] ) && ! empty( $realty_theme_option['property-search-parameter'] ) ) {
$search_parameters = $realty_theme_option['property-search-parameter'];
} else {
$search_parameters = null;
}
$raw_search_fields = get_query_var('property_search_fields');
if ( ! tt_is_array_empty( $raw_search_fields ) ) {
$search_fields = $raw_search_fields;
} else if ( isset( $realty_theme_option['property-search-parameter'] ) && ! empty( $realty_theme_option['property-search-parameter'] ) ) {
$search_fields = $realty_theme_option['property-search-field'];
} else {
$search_fields = null;
}
$raw_search_labels = get_query_var('property_search_labels');
if ( ! tt_is_array_empty( $raw_search_labels ) ) {
$search_labels = $raw_search_labels;
} else if ( isset( $realty_theme_option['property-search-label'] ) && ! empty( $realty_theme_option['property-search-label'] ) ) {
$search_labels = $realty_theme_option['property-search-label'];
} else {
$search_labels = null;
}
$default_search_fields_array = array(
'estate_search_by_keyword',
'estate_property_id',
'estate_property_location',
'estate_property_type',
'estate_property_status',
'estate_property_price',
'estate_property_price_min',
'estate_property_price_max',
'estate_property_pricerange',
'estate_property_size',
'estate_property_rooms',
'estate_property_bedrooms',
'estate_property_bathrooms',
'estate_property_garages',
'estate_property_available_from'
);
$i = 0;
if ( isset( $search_form_columns ) && ! empty( $search_form_columns ) ) {
// Use $columns parameter from shortcode [property_search_form]
$count_search_fields = $search_form_columns;
} else {
// No shortcode $columns found, pick columns according to total field count
$count_search_fields = count( $search_fields );
}
if ( $count_search_fields == 1 ) {
$columns = 'col-xs-12';
} else if ( $count_search_fields == 2 ) {
$columns = 'col-xs-12 col-sm-6';
} else if ( $count_search_fields == 3 ) {
$columns = 'col-xs-12 col-sm-6 col-md-4';
} else {
$columns = 'col-xs-12 col-sm-4 col-md-3';
}
if ( is_page_template( 'template-map-vertical.php' ) ) {
$columns = 'col-xs-12 col-sm-6';
}
// Do we have any search parameters defined?
if ( isset( $search_parameters ) && ! empty( $search_parameters[0] ) ) {
foreach ( $search_fields as $search_field ) {
$search_parameter = $search_parameters[$i];
// Check If Search Field Is Filled Out
if ( ! empty( $search_field ) ) {
// Default Property Field
if ( in_array( $search_field, $default_search_fields_array ) ) {
switch ( $search_field ) {
case 'estate_search_by_keyword' :
case 'estate_property_id' :
?>
<div class="<?php echo $columns; ?> form-group">
<input type="text" name="<?php echo $search_parameter; ?>" id="<?php echo $search_parameter; ?>" value="<?php echo isset( $_GET[$search_parameter]) ? $_GET[$search_parameter] : ''; ?>" placeholder="<?php echo $search_labels[$i]; ?>" class="form-control" />
</div>
<?php
break;
case 'estate_property_location' : ?>
<div class="<?php echo $columns; ?> form-group select">
<?php
// http://wordpress.stackexchange.com/questions/14652/how-to-show-a-hierarchical-terms-list#answer-14658
if ( ! empty( $search_labels[$i] ) ) {
$search_label_location = $search_labels[$i];
} else {
$search_label_location = esc_html__( 'Any Location', 'realty' );
}
?>
<select name="<?php echo $search_parameter; ?>" id="<?php echo $search_parameter; ?>" class="<?php echo esc_attr( $form_select_class ); ?>">
<option value="all"><?php echo $search_label_location; ?></option>
<?php
$location = get_terms('property-location', array(
'orderby' => 'slug',
'parent' => 0,
'hide_empty' => false
) );
if ( isset( $_GET[$search_parameter] ) ) {
$get_location = $_GET[$search_parameter];
} else {
$get_location = null;
}
?>
<?php foreach ( $location as $key => $location ) : ?>
<option value="<?php echo $location->slug; ?>" <?php selected( $location->slug, $get_location ); ?>>
<?php
echo $location->name;
$location2 = get_terms( 'property-location', array(
'orderby' => 'slug',
'parent' => $location->term_id
) );
if ( $location2 ) :
?>
<optgroup>
<?php foreach( $location2 as $key => $location2 ) : ?>
<option value="<?php echo $location2->slug; ?>" class="level2" <?php selected( $location2->slug, $get_location ); ?>>
<?php
echo $location2->name;
$location3 = get_terms( 'property-location', array(
'orderby' => 'slug',
'parent' => $location2->term_id
) );
if ( $location3 ) :
?>
<optgroup>
<?php foreach( $location3 as $key => $location3 ) : ?>
<option value="<?php echo $location3->slug; ?>" class="level3" <?php selected( $location3->slug, $get_location ); ?>>
<?php
echo $location3->name;
$location4 = get_terms( 'property-location', array( 'orderby' => 'slug', 'parent' => $location3->term_id ) );
if( $location4 ) :
?>
<optgroup>
<?php foreach( $location4 as $key => $location4 ) : ?>
<option value="<?php echo $location4->slug; ?>" class="level4" <?php selected( $location4->slug, $get_location ); ?>>
<?php echo $location4->name; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</option>
<?php endforeach; ?>
</select>
</div>
There's a line of code that needs modifying
Reset Form
That element does not exist, the form has a class called property-search-form which you could use as the selector.
So replace the line with something like
Reset Form

Display items in order including image WooCommerce

I am trying to display the products/items within a WooCommerce order using the following php, but the items are not being shown.
The code I am using is an adaptation of this: Get cart item name, quantity all details woocommerce
Using the original code from the question above also does not display anything on my page.
Thanks in advance.
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
The full page code is here:
<?php
/*
Template Name: Store
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
<?php _e('Store Queue'); ?>
</title>
</head>
<body id="driverqueue">
<header>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</header>
<section>
<?php
global $woocommerce;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-processing',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
// $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<table class="ordertable" id="<?php echo $order_id; ?>">
<tr>
<td>
<p>Order #
<?php echo $order_id; ?> —
<time datetime="<?php the_time('c'); ?>">
<?php echo the_time('d/m/Y g:i:s A'); ?>
</time>
</p>
</td>
<td>
<?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?>
</td>
<td>
<table>
<tr>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
</tr>
</table>
</td>
<td>
<p>
<?php
do_action( 'woocommerce_admin_order_actions_start', $order );
$actions = array();
if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=awaiting-shipment&order_id=' . $post->ID ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => "complete"
);
};
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );
foreach ( $actions as $action ) {
printf( '<a class="button %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
}
do_action( 'woocommerce_admin_order_actions_end', $order );
?>
</p>
</td>
<td>
<form action="">
<input type="checkbox" class="ordercollected" value="0" />
</form>
</td>
</tr>
<?php endwhile; ?>
</section>
</body>
</html>
Solved this using info from the following link with the adapted code below:
https://wordpress.stackexchange.com/questions/180075/how-to-get-woocommerce-order-product-info
<?php
foreach ($order->get_items() as $key => $lineItem) {
//uncomment the following to see the full data
// echo '<pre>';
// print_r($lineItem);
// echo '</pre>';
$product_id = $lineItem['product_id'];
$product = wc_get_product( $product_id );
echo '<tr><td>' . $product->get_image() . '</td>'; // accepts 2 arguments ( size, attr )
echo '<td>' . 'Product: ' . $lineItem['name'] . '</td>';
echo '<td> x' . $lineItem['qty'] . '</td></tr>';
}
?>

Wordpress metabox drop down list isn't saving

I have 2 custom post types named clients and casestudies. I'm trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.
I have followed this tutorial to get a meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336
This is the meta box code I have in my functions.php file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'clients', 'side', 'default' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<?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'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
}
The meta box displays correctly on the correct post type, but when I update the post it won't save the data.
Thanks.
You don't have nonce hidden field. Save function would return nothing.
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
UPDATE:
So your cd_meta_box_cb function would be
<?php
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
<?php
}
?>

get custom field value from taxonomy in wordpress

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>
';
}
}
?>

Categories