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>
Related
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 );
}
I'm trying to add and change the input type from text to radio button to use in my wordpress theme user profile settings like Gender: Male Female, but can't do the trick in the code. don't know how to change it.
tried with some code from w3schools given below, but that doesn't work too.
Tried radio type, but doesn't work
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="female") echo "checked";?> value="female">Female
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="male") echo "checked";?> value="male">Male
</div>
i have to change the code to radio type, gonna use them in wordpress functions.php
add_action( 'themer_after_user_profile_registration_fields_action', 'wpt_show_user_profile_custom_inputs' );
function wpt_show_user_profile_custom_inputs( $uid ) {
$user_new_field = get_user_meta( $uid, 'user_new_field', true );
$new_field = empty( $_POST['user_new_field'] ) ? $user_new_field : stripslashes( $_POST['user_new_field'] ); ?>
<div class="field">
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="text" value="<?php echo $new_field; ?>" name="user_new_field" size="40" placeholder="<?php echo _x( 'My New Field Placeholder', 'Placeholder for: New field', 'themer' ); ?>" />
</div>
<?php }
// Save the field value from user settings page
add_action( 'themer_user_profile_extra_fields_update', 'wpt_save_user_profile_custom_inputs' );
function wpt_save_user_profile_custom_inputs( $uid ) {
if ( isset( $_POST['save-info'] ) ) {
if ( isset( $_POST['user_new_field'] ) ) {
update_user_meta( $uid, 'user_new_field', $_POST['user_new_field'] );
}
}
}
You can do this by replacing your code with below code in functions.php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra Field", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label ><?php _e("Gender"); ?></label></th>
<td>
<input type="radio" name="gender" id="gender" value="male" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'male'){echo ' checked '; } ?>class="regular-text" />Male
<input type="radio" name="gender" id="gender" value="female" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'female'){echo ' checked '; } ?> class="regular-text" />Female
<br />
<span class="description"><?php _e("Please enter your gender."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'gender', $_POST['gender'] );
}
Controller code:
public function create(){
$row = $this->model->set_attribute( ( isset( $_POST["model"] ) )?$_POST["model"]:array() );
if( !empty( $_POST["model"] ) ){
$rand = rand( 10000000, 9999999 ). '_';
if( !empty( $_FILES['model']['name']['image'] ) ){
move_uploaded_file( $_FILES['model']['tmp_name']['image'], FCPATH. '/assets/uploads/'. $rand. $_FILES['model']['name']['image'] );
$_POST['model']['image'] = $rand. $_FILES['model']['name']['image'];
}
$id = $this->model->insert( $_POST["model"] );
if( $id != null ){
$row = $this->model->get( $id );
app::set_flash( "success", "Record is added." );
$this->_redirect( "index" );
}
}
$this->_render( "form", array(
"model" => $this->model,
"page_heading" => $this->page_heading,
"row" => $row,
) );
}
views
<div class="form-group">
<label for="image"><?php echo( $model->attributeLabels( 'image' ) ); ?> <small class="danger highlight "><?php echo form_error( 'image' ) ?></small></label>
<input type="file" class="form-control" name="model[image]" id="main_image" placeholder="<?php echo( $model->attributeLabels( 'image' ) ); ?>" value="<?php echo $row->image; ?>" />
<?php echo( $row->image ); ?>
</div>
In the above controller function are store the image name in database but not upload the image in image directory assets/uploads
it should have attribute enctype="multipart/form-data" are missed in form
I am trying to write a small plugin in wordpress, I have a small meta box with text input and a radio button in which user needs to add information, and I want that the imformation that is saved in the post meta box will be displayed after save (in the current state the input text resets to the default plaholder after save).
this is the relevant code:
add_action( 'add_meta_boxes', 'asset_price' );
function asset_price() {
add_meta_box(
'asset_price',
__( 'asset price', 'myplugin_textdomain' ),
'asset_price_box_content',
'asset',
'normal',
'high'
);
}
function asset_price_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'asset_price_box_content_nonce' );
echo '<label for="asset_price"></label>
<input type="text" id="asset_price" name="asset_price" placeholder="insert price" />
<input type="radio" name="currency" value="percent" checked="checked">%
<input type="radio" name="currency" value="Dollar">$';
}
add_action( 'save_post', 'asset_price_box_save' );
function asset_price_box_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ( !wp_verify_nonce( $_POST['asset_price_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
$old = get_post_meta($post_id, "asset_price", true);
$new = $_POST["asset_price"];
if ($new && $new != $old) {
$product_price = $_POST['asset_price'];
update_post_meta( $post_id, 'asset_price', $product_price );
} elseif ('' == $new && $old) {
delete_post_meta($post_id, "asset_price", $old);
}
}
Thanks in advance
You can get the meta box value using get_post_meta() function and display it in the textbox
function asset_price_box_content( $post ) {
$price = get_post_meta($post->ID, "asset_price", true);
wp_nonce_field( plugin_basename( __FILE__ ), 'asset_price_box_content_nonce' );
echo '<label for="asset_price"></label>
<input type="text" id="asset_price" name="asset_price" placeholder="insert price" value="'.$price.'" />
<input type="radio" name="currency" value="percent" checked="checked">%
<input type="radio" name="currency" value="Dollar">$';
}
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;