add_action( ‘personal_options_update’, ‘my_save_extra_profile_fields’ );
add_action( ‘edit_user_profile_update’, ‘my_save_extra_profile_fields’ );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( ‘edit_user’, $user_id ) )
return false;
/* Copy and paste this line for additional fields. Make sure to change ‘twitter’ to the field ID. */
update_usermeta( $user_id, ‘name’, $_POST[‘name’] );
}
Thanks
]]>My problem is that I can not create more custom fields inside my custom meta boxes.
I have this:
function add_custom_meta_box_productos() {
add_meta_box(
'custom_meta_box_productos', // $id
backend_product_metabox_title, // $title
'show_custom_meta_box_productos', // $callback
'products', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box_productos');
// Field Array
$prefix_productos = 'custom_';
$custom_meta_fields_productos = array(
array(
'label'=> 'Label 1',
'desc' => 'Title',
'id' => 'label1',
'type' => 'text'
),
array(
'label'=> 'Label 2',
'desc' => 'Title',
'id' => 'label2',
'type' => 'text'
),
array(
'label'=> 'Label 3',
'desc' => 'Title',
'id' => 'label3',
'type' => 'text'
),
array(
'label'=> 'Label 4',
'desc' => 'Title',
'id' => 'label4',
'type' => 'text'
),
);
// The Callback
function show_custom_meta_box_productos() {
global $custom_meta_fields_productos, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($custom_meta_fields_productos as $field_producto) {
// get value of this field if it exists for this post
$meta_producto = get_post_meta($post->ID, $field_producto['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field_producto['id'].'">'.$field_producto['label'].'</label></th>
<td>';
switch($field_producto['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field_producto['id'].'" id="'.$field_producto['id'].'" value="'.$meta_producto.'" size="30" />
<br /><span class="description">'.$field_producto['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_custom_meta_producto($post_id) {
global $custom_meta_fields_productos;
// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_fields_producto as $field_producto) {
$old = get_post_meta($post_id, $field_producto['id'], true);
$new = $_POST[$field_producto['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field_producto['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field_producto['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_custom_meta_producto');
First, I create the metabox and create an array with 4 different input text. But when I introduce information and clic in publish, they do not save nothing. The problem is that WordPress does not create the id custom field for each input text. This is a piece of code but I have more fields in this metabox and save the information properly. But, If I create new fields, these fields do not save the information. Any idea?
Thanks!
]]>In my WordPress backend I have some custom posts and, in one of them, I want to display in a dropdown field some posts related with but ordered by post type. I want to display something like this.
<select multiple="multiple" style="height:200px; width:300px"> <optgroup label="Productos"> <option>Producto 1</option> <option>Producto 2</option> <option>Producto 3</option> <option>Producto 4</option> <option>Producto 5</option> <option>Producto 6</option> </optgroup> <optgroup label="Paso 1"> <option>Subpagina 1</option> <option>Subpagina 2</option> <option>Subpagina 3</option> </optgroup> </optgroup> <optgroup label="Paso 2"> <option>Subpagina 1</option> <option>Subpagina 2</option> <option>Subpagina 3</option> </optgroup> </optgroup> <optgroup label="Paso 3"> <option>Subpagina 1</option> <option>Subpagina 2</option> <option>Subpagina 3</option> </optgroup> </select>
Here is the code that displays all of posts that I want but without order. I figure out that I have to use the id of the custom post type but I do not how to, any idea?
Note: I`ll like to save more than one post at the same time.
// Add the Meta Box
function add_custom_meta_box_related() {
add_meta_box(
'custom_meta_box_related', // $id
'Related Information', // $title
'show_custom_meta_box_related', // $callback
'related', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box_related');
// Field Array
$prefix_related = 'custom_';
$custom_meta_fields_related = array(
array(
'label' => 'Related Items',
'desc' => 'Select a related item(s)',
'id' => $prefix_materiales.'post_id',
'type' => 'post_list',
'post_type' => array('products','paso1','paso2','paso3','paso4','compra'),
)
);
// The Callback
function show_custom_meta_box_related() {
global $custom_meta_fields_related, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($custom_meta_fields_related as $field_related) {
// get value of this field if it exists for this post
$meta_related = get_post_meta($post->ID, $field_related['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field_related['id'].'">'.$field_related['label'].'</label></th>
<td>';
switch($field_related['type']) {
// case items will go here
// post_list
case 'post_list':
$items = get_posts( array (
'post_type' => $field_related['post_type'],
'posts_per_page' => -1
));
echo '<select multiple name="'.$field_related['id'].'" id="'.$field_related['id'].'">
<option value="">Select One or more</option>'; // Select One
foreach($items as $item) {
echo '<option value="'.$item->ID.'"',$meta_related == $item->ID ? ' selected="selected"' : '','> '.$item->post_title.'</option>';
} // end foreach
echo '</select><br /><span class="description">'.$field_related['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_custom_meta_related($post_id) {
global $custom_meta_fields_related;
// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_fields_related as $field_related) {
$old = get_post_meta($post_id, $field_related['id'], true);
$new = $_POST[$field_related['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field_related['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field_related['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_custom_meta_related');
Thanks in advance
]]>Well, I have a little problem with my custom meta box. I create a custom post which includes a custom meta field dropdown. The dropdown displays the title of the posts from another custom posts that I want to link with when save. But when I clic on publish the dropdown doesn′t save.
Here is my code.
Here I create my custom post:
function my_custom_post_category_product() {
$labels = array(
'name' => _x( 'Categories Products', 'post type general name' ),
'singular_name' => _x( 'Categories Products', 'post type singular name' ),
'add_new' => _x( 'Add New' ),
'add_new_item' => __( 'Add New Categories Products' ),
'edit_item' => __( 'Edit Categories Products' ),
'new_item' => __( 'New Categories Products' ),
'all_items' => __( 'All Categories Products' ),
'view_item' => __( 'View Categories Products' ),
'search_items' => __( 'Search Categories Products' ),
'not_found_in_trash' => __( 'No Categories Products found in the Trash' ),
'not_found' => __( 'No Categories Products found' ),
'parent_item_colon' => '',
'menu_name' => 'Categoria Producto'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our categories specific data',
'public' => true,
'menu_position' => 100,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
'taxonomies' => array('category'),
);
register_post_type( 'category_product', $args );
}
add_action( 'init', 'my_custom_post_category_product' );
function my_updated_messages_category_product( $messages ) {
global $post, $post_ID;
$messages['categories'] = array(
0 => '',
1 => sprintf( __('Categories Products updated. <a href="%s">View Categories Products</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Categories updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Categories Products restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Categories Products published. <a href="%s">View Categories Products</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Categories Products saved.'),
8 => sprintf( __('Categories Products submitted. <a target="_blank" href="%s">Preview Categories Products</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Categories Products scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Categories Products</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Categories Products draft updated. <a target="_blank" href="%s">Preview Categories Products</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( 'post_updated_messages', 'my_updated_messages_category_product' );
And then the function to create the custom meta box and save the data:
add_action('add_meta_boxes', 'product_post_options_box',10,2);
/* saved the data */
add_action( 'save_post', 'save_product_post_options_box' );
function product_post_options_box($post_type, $post) {
if ('category_product' == $post_type) //only add to your post type
add_meta_box('post_info', 'Product Information', 'product_post_info', 'category_product', 'side', 'high');
}
function product_post_info() {
global $post;
/* List the posts*/
$posts = get_posts( array(
'post_type' => array( 'categories' ),
'posts_per_page' => -1
)
);
//get saved data
$saved = get_post_meta($post->ID,'_categories',true);
if (empty($saved))
$saved = array();
//var_dump($saved);
echo '<select name="categories" id="categories" multiple>'.
'<option value="" selected="selected">Select a post</option>';
foreach ($posts as $p) {
$link = get_permalink( $p->ID);
$selected = (in_array($link,$saved)) ? ' selected="selected"' : '';
echo '<option'.$selected.' value="'.$link.'">'.get_the_title( $p->ID ).'</option>';
}
echo '</select>';
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'categories_nonce' );
}
function save_product_post_options_box($post_ID){
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// First we need to check if the current user is authorised to do this action.
if ( ! current_user_can( 'edit_post', $post_ID ) )
return;
// Secondly we need to check if the user intended to change this value.
if ( ! isset( $_POST['categories_nonce'] ) || ! wp_verify_nonce( $_POST['categories_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Thirdly check the post type
if ('categories' != get_post_type($post_ID))
return;
$mydata = $_POST['categories'];
// Do something with $mydata
// either using
update_post_meta( $post_ID, '_category_product', $mydata, true);
}
At this point:
//get saved data
$saved = get_post_meta($post->ID,'_categories',true);
if (empty($saved))
$saved = array();
//var_dump($saved);
If I show $saved in order to see what it contains it shows the custom posts from WordPress. I have no idea where is the bug. Any idea?
Note: The dropdown must save multiple options (1 post or more)
Thanks a lot!
]]>