Get the value from a custom field in category - php

I made a custom field in admin category interface named custom order. It is showing in the admin. But now I am having difficulties in getting the information from the field and echo it in index.php.
If I run
$thisCat = get_category( 29);
print_r($thisCat);
I don't get any information from the custom field.
echo get_post_custom_values('category_custom_order', 29); doesn't echo anything.
How should I get the value from category_custom_order?
Here is my code for custom field in functions.php:
<?php
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Custom Order</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = 'category_custom_order_' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST['category_custom_order'] ) ) {
$option_name = 'category_custom_order_' . $term_id;
update_option( $option_name, $_POST['category_custom_order'] );
}
}

You should be able to get it this way, and by the way it's not a custom field but an option.
$category_id = 29;
$category_custom_order = get_option( 'category_custom_order_' . $category_id );
echo $category_custom_order;

Related

Display categories of a Custom post type in WooCommerce product

I have created a custom post type with own categories.
I would like to link the categories of the custom post type to the WooCommerce product. e.g. This product belongs to the category Fabric cover.
These categories I have displayed in Woocommerce products in the backend.
I do that with getterm. So far, everything works. I then created a checkbox field in front of each category.
When I save the contents, all categories are stored in an array (whether I've selected it or not). I would like to save only the category I have selected. What am I doing wrong?
how can I just save the array of the selected checkbox in metabox (pro_in_cat_fields).
At the moment all values are saved. If I only check one checkbox (for example, fabric cover1), only the values that are in the same array of the checkbox need to be saved. not the values of fabric cover 1, fabric cover 2, ... etc
<?php
add_action('admin_init', 'add_pro_in_cat_boxes', 1);
function add_pro_in_cat_boxes() {
add_meta_box( 'pro_in_cat-fields', 'Save product in custom category', 'pro_in_cat_meta_box_display', 'product', 'normal', 'low');
}
function pro_in_cat_meta_box_display() {
global $post;
$pro_in_cat_fields = get_post_meta($post->ID, 'pro_in_cat_fields', true);
wp_nonce_field( 'pro_in_cat_meta_box_nonce', 'pro_in_cat_meta_box_nonce' );
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.pro_in_cat_submit').click(function(e) {
e.preventDefault();
$('#publish').click();
});
});
</script>
<table id="pro_in_cat-fieldset-one" width="100%">
<thead>
<tr>
<th width="30%"></th>
<th width="70%"></th>
</tr>
</thead>
<tbody>
<?php
$stoffcat_parent= get_terms( 'cover_cat', array( 'hide_empty' => false, 'parent' => 0 ) );
if ( $stoffcat_parent ) :
foreach( $stoffcat_parent as $parent_term ) {
echo $parent_term->name . '<br>';
$stoffcat_value = get_terms( 'cover_cat', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) );
foreach( $stoffcat_value as $child_term ) {
?>
<tr>
<td>
<input type="checkbox" class="widefat" name="pro_in_cat_status[]" value="<?php echo $checked; ?>" />
<input type="text" class="widefat" name="pro_in_cat_name[]" value="<?php if($child_term->name != '') echo esc_attr( $child_term->name ); ?>" /></td>
<td><input type="text" class="widefat" name="pro_in_cat_termid[]" value="<?php if($child_term->term_id != '') echo esc_attr( $child_term->term_id ); ?>" />
<input type="text" class="widefat" name="pro_in_cat_slug[]" value="<?php if($child_term->slug != '') echo esc_attr( $child_term->slug); ?>" /></td>
</tr>
<?php
}
}
else :
// show a blank one
?>
<tr>
<td><input type="checkbox" class="widefat" name="pro_in_cat_status[]" />
<input type="text" class="widefat" name="pro_in_cat_name[]" /></td>
<td><input type="text" class="widefat" name="pro_in_cat_termid[]" /><input type="text" class="widefat" name="pro_in_cat_slug[]" /></td>
</tr>
<?php endif; ?>
</tbody>
</table>
<p>
<input type="submit" class="pro_in_cat_submit" value="Save" />
</p>
<?php
}
add_action('save_post', 'pro_in_cat_meta_box_save');
function pro_in_cat_meta_box_save($post_id) {
if ( ! isset( $_POST['pro_in_cat_meta_box_nonce'] ) ||
! wp_verify_nonce( $_POST['pro_in_cat_meta_box_nonce'], 'pro_in_cat_meta_box_nonce' ) )
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$pro_in_catold = get_post_meta($post_id, 'pro_in_cat_fields', true);
$pro_in_catnew = array();
$pro_in_catstatus = $_POST['pro_in_cat_status'];
$pro_in_catnames = $_POST['pro_in_cat_name'];
$pro_in_cattermid = $_POST['pro_in_cat_termid'];
$pro_in_catslug = $_POST['pro_in_cat_slug'];
$count = count( $pro_in_catnames );
for ( $i = 0; $i < $count; $i++ ) {
if ( $pro_in_catstatus[$i] != '' ) {
$pro_in_catnew[$i]['pro_in_cat_status'] = stripslashes( strip_tags( $pro_in_catstatus[$i] ) );
}
if ( $pro_in_catnames[$i] != '' ) {
$pro_in_catnew[$i]['pro_in_cat_name'] = stripslashes( strip_tags( $pro_in_catnames[$i] ) );}
if ( $pro_in_cattermid[$i] != '' ) {
$pro_in_catnew[$i]['pro_in_cat_termid'] = stripslashes( strip_tags( $pro_in_cattermid[$i] ) );}
if ( $pro_in_catslug[$i] != '' ) {
$pro_in_catnew[$i]['pro_in_cat_slug'] = stripslashes( strip_tags( $pro_in_catslug[$i] ) );}
}
if ( !empty( $pro_in_catnew ) && $pro_in_catnew != $pro_in_catold )
update_post_meta( $post_id, 'pro_in_cat_fields', $pro_in_catnew );
elseif ( empty($pro_in_catnew) && $pro_in_catold )
delete_post_meta( $post_id, 'pro_in_cat_fields', $pro_in_catold );
}

Add custom dimension fields to each variation settings for variable products

I'm trying to add a "Built Dimensions" fields to each product variation settings.
Here's a mock of what I'm trying to accomplish:
I've followed these following tips but they aren't doing quite what I want:
http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
Add Advanced Custom Fields to WooCommerce Product Variation
Those are adding it to one of the other data tabs. I need it per variation. Each variation has a built dimension and a shipping dimension.
With the 2 hooked functions below you will get exactly what you are expecting like in your mock:
// Add variation custom "dimentions" fields
add_action( 'woocommerce_variation_options_dimensions','add_variation_options_built_dimensions', 10, 3 );
function add_variation_options_built_dimensions( $loop, $variation_data, $variation ){
$variation_built_lenght = get_post_meta($variation->ID,"_built_lenght", true );
if( ! $variation_built_lenght ) $variation_built_lenght = "";
$variation_built_width = get_post_meta($variation->ID,"_built_width", true );
if( ! $variation_built_width ) $variation_built_width = "";
$variation_built_height = get_post_meta($variation->ID,"_built_height", true );
if( ! $variation_built_height ) $variation_built_height = "";
?>
<p class="form-field form-row dimensions_field built_dimensions hide_if_variation_virtual form-row-last">
<label for="product_built_length"><?php
// translators: %s: dimension unit
printf(
__( 'Built dimensions (L×W×H) (%s)', 'woocommerce' ),
get_option( 'woocommerce_dimension_unit' )
);
?></label>
<?php echo wc_help_tip( __( 'Built length x width x height in decimal form', 'woocommerce' ) ); ?>
<span class="wrap">
<input id="product_built_length" placeholder="<?php esc_attr_e( 'Built length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_lenght_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_lenght ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_width_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_width ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="built_height_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_height ); ?>" />
</span>
</p>
<?php
}
//Save variation custom "dimentions" fields
add_action( 'woocommerce_save_product_variation','save_variation_options_built_dimensions', 10 ,2 );
function save_variation_options_built_dimensions( $variation_id, $loop ){
$built_lenght = $_POST["built_lenght_$loop"];
if(!empty($built_lenght))
update_post_meta( $variation_id, '_built_lenght', sanitize_text_field($built_lenght) );
$built_width = $_POST["built_width_$loop"];
if(!empty($built_width))
update_post_meta( $variation_id, '_built_width', sanitize_text_field($built_width) );
$built_height = $_POST["built_height_$loop"];
if(!empty($built_height))
update_post_meta( $variation_id, '_built_height', sanitize_text_field($built_height) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce 2.6.x and 3+.
You will get this:

wordpress edit wp_users table field from admin

I added a field "money" to wp_users table, whehe store a user money amount. Now i want to take ability for admin to change that input field value.
I can get the value with function added to functions.php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>User money</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Amount</label></th>
<td>
<input type="text" name="money" id="money" value="<?php echo esc_attr( get_the_author_meta( 'money', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter user money</span>
</td>
</tr>
</table>
<?php }
but how can i update that field?
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
update_user_meta($user_id, 'money', $_POST['money']);
}
Would you please try above code?
finally I made in such a way
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
$money = $_POST['money'];
global $wpdb;
$wpdb->query("UPDATE wp_users SET money='$money' WHERE ID = '$user_id'");
}

How to get Text Area in WordPress posts section?

I need to change this code to implement "More info" field as a text field in my WordPress post section.
The "More info" field looks like this:
I use smartmetabox. It has 2 files:
textarea.php:
<textarea name="<?php echo $id?>" id="<?php echo $id?>" rows="5" cols="100" class="custom"><?php echo $value?></textarea>
text.php:
<input type="text" name="<?php echo $id?>" id="<?php echo $id?>" value="<?php echo $value?>" class="regular-text" />
And my_file.php code is:
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', __('Information', 'addict'), 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$creteria_1_text = isset( $values['creteria_1_text'] ) ? esc_attr( $values['creteria_1_text'][0] ) : '';
$check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="creteria_1_text"><b><?php _e("More info, 'addict') ?></b></label>
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['creteria_1_text'] ) )
update_post_meta( $post_id, 'creteria_1_text', wp_kses( $_POST['creteria_1_text'], $allowed ) );
}
// function for show rating content
//$key_1_value = get_post_meta($post->ID, 'my_meta_box_text', true);
?>
After some digging i found that i should replace this code:
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>
To:
<textarea name="creteria_1_text" id="creteria_1_text" rows="5" cols="100" class="custom"><?php echo $creteria_1_text; ?></textarea>

default value not getting displayed in input field wordpress

I have written code for creating theme options
but default values not getting displayed in text field
Also settings saved message also not getting displayed
Thank you
<?php
// Default options values
$xyz_options = array(
'footer_copyright' => '© ' . date('Y') . ' ' . get_bloginfo('name'),
'facebook'=>'http://www.facebook.com/'
);
if ( is_admin() ) : // Load only if we are viewing an admin page
function xyz_register_settings() {
// Register settings and call xyz initiation functions
register_setting( 'xyz_theme_options', 'xyz_options', 'xyz_validate_options' );
}
add_action( 'admin_init', 'xyz_register_settings' );
function xyz_theme_options() {
// Add theme options page to the admin menu
add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'xyz_theme_options_page' );
}
add_action( 'admin_menu', 'xyz_theme_options' );
// Function to generate options page
function xyz_theme_options_page() {
global $xyz_options;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Options' ) . "</h2>";
// This shows the page's name and an icon if one has been provided ?>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<form method="post" action="options.php">
<?php $settings = get_option( 'xyz_options', $xyz_options ); ?>
<?php settings_fields( 'xyz_theme_options' );
/* This function outputs some hidden fields required by the form,
including a nonce, a unique number used to ensure the form has been submitted from the admin page
and not somewhere else, very important for security */ ?>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="facebook">Facebook url</label>
</th>
<td>
<input id="facebook" name="xyz_options[facebook]" type="text" value="<?php esc_attr_e($settings['facebook']); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="footer_copyright">Footer Text 1</label>
</th>
<td>
<input id="footer_copyright" name="xyz_options[footer_copyright]" type="text" value="<?php esc_attr_e($settings['footer_copyright']); ?>" />
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save Options" />
</p>
</form>
</div>
<?php
}
function xyz_validate_options( $input ) {
global $xyz_options;
$settings = get_option( 'xyz_options', $xyz_options );
}
endif; // EndIf is_admin()
I have wriiten above code.
Please help
Thank you
I tested your code and the default values are being displayed correctly.
Doing a var_dump($_REQUEST); shows what's the problem with the update message, we have to check simpy for $_REQUEST['settings-updated'].
You can remove the is_admin() checking, as admin_init and admin_menu only run on admin.
The validation function should be something like this, validating each input field and returning a clean array with sanitized values:
function xyz_validate_options( $input ) {
$new_input = array();
if( isset( $input['facebook'] ) )
$new_input['facebook'] = esc_url( $input['facebook'] );
if( isset( $input['footer_copyright'] ) )
$new_input['footer_copyright'] = esc_attr( $input['footer_copyright'] );
return $new_input;
}

Categories