How to get pages as a checkbox option in user profile - wordpress

i want to list the pages in checkbox in user profile for the users role to check specified page for them
function my_user_field( $user ) {
$user_meta = get_the_author_meta( 'user_interests', $user->ID, true );?>
<table class="form-table">
<tr>
<th>
<label for="Dealing Type">Multi pages select for blurbs
</label></th><td>
<?php
$mypages = get_pages( array(
'sort_column' => 'post_date',
'sort_order' => 'desc'
) );
if(count($mypages)){
foreach( $mypages as $page )
{
$title = $page->post_title;
$slug = $page->post_name;
?>
<label for='user_interests_<?php echo $page->post_name; ?>' class='blurb_user_label_checkbox'>
<input id='user_interests_<?php echo $page->post_name; ?>' name='user_interests[<?php echo $page->post_title; ?>]' type='checkbox' value='$page->post_title'
<?php if ( in_array( $page->post_title, $user_meta ) ) echo ' checked="checked"'; ?> class='blurb_user_checkbox'>
<?Php echo $title; ?>
</label>
<?php
}
}
var_dump($user_meta);
?>
</td>
</tr>
</table>
function my_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'user_interests', $_POST['user_interests']);
}
add_action( 'user_new_form', 'my_user_field' );
add_action( 'show_user_profile', 'my_user_field' );
add_action( 'edit_user_profile', 'my_user_field' );
add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );
add_action('user_register', 'my_save_custom_user_profile_fields');
The checkbox is not working when options saved so please help me somebody. i hope guys you can help me.if there any other methods also please give me

Related

How to get values of product category custom meta field in admin single product edit page?

With the following code i added a custom meta field to a product category
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[custom_term_meta]"><?php _e( 'Short form', 'woocommerce' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','woocommerce' ); ?></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[custom_term_meta]"><?php _e( 'Short form', 'woocommerce' ); ?></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'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','woocommerce' ); ?></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 );
Any idea how to populate a select option dropdown like the following example with the custom meta value & category name for all cargeories if the parent category has the ID 33 in the product edit page? I only miss the custom meta value part - seems the category names working fine.
<?php // Populate Dropdown with Brand from Parent Category with ID 33 ?>
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'taxonomy' => 'product_cat',
'parent' => '33'
);
$categories = get_categories($args);
foreach($categories as $cat) {
?>
<option value="<?php echo $cat->name; ?>" ><?php echo $cat->CUSTOM-META???; ?></option>
<?php
}
?>
To get custom meta value of the given category you can use get_term_meta function.
<option value="<?php echo $cat->name; ?>" >
<?php echo get_term_meta($cat->term_id,'custom_term_meta',true); ?>
</option>

WordPress custom user_meta not saving

I have built a custom plugin and I need to be able to add custom meta to the user profile screen.
I have the meta showing but they don't seem to update. This is the first instance of this meta so it doesn't exist in the DB already.
The files are located in my plugin dir and not in my theme.
<?php
add_action( 'show_user_profile', 'add_extra_social_links' );
add_action( 'edit_user_profile', 'add_extra_social_links' );
function add_extra_social_links( $user )
{
?>
<h3>Web App Information</h3>
<table class="form-table">
<tr>
<th><label for="companyname">Company Name</label></th>
<td>
<?php
$args = array(
'post_type' => array('operators','clients'),
'posts_per_page' => -1,
'orderby' => 'title',
);
$myposts = get_posts( $args );
echo '<select id="companyname"name="companyname">';
echo '<option value="' . esc_attr(get_the_author_meta( 'companyname', $user->ID )) . '">';
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<option value="<?php echo $post->post_title; ?>">
<?php echo $post->post_title; ?>
</option>
<?php endforeach;
echo '</select>';
wp_reset_postdata();?>
</td>
</tr>
<tr>
<th><label for="companyadmin">Is this user a company admin.</label></th>
<td>
<?php
echo '<select id="companyadmin" name="companyadmin">';
echo '<option value="' . esc_attr(get_the_author_meta( 'companyadmin', $user->ID )) . '">';
echo '<option value="Y">Yes, is admin.</option>';
echo '<option value="N">No, is not admin.</option>';
echo '</select>';
?>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_social_links' );
add_action( 'edit_user_profile_update', 'save_extra_social_links' );
function save_extra_social_links( $user_id )
{
update_user_meta( $user_id,'companyname', $_POST['companyname'] );
update_user_meta( $user_id,'companyadmin', $_POST['companyadmin'] );
}
?>
try use function get_current_user_id in $user_id
i think you use parameter but not send in hook

How to arrange the custom field on woocommerce checkout page

I know how to add a custom field to the checkout page, and then how to process the field also.
But when I add a custom field to my form, it always appears at the end of the form. How can I make it appear on top of other fields?
My current script:
<?php *// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'message_field' => array(
'type' => 'textarea',
'required' => true,
'label' => __( 'Message Field' )
),
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'WRITE A MESSAGE TO RECIPIENT' ); ?><span>(<?php _e( 'Leave blank if not required' ); ?>)</span></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
// don't forget appropriate sanitization if you are using a different field type
if( isset( $posted['message_field'] ) ) {
update_post_meta( $order_id, '_message_field', sanitize_text_field( $posted['message_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 1);
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){ ?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'message_field:' ); ?></th>
<td><?php echo get_post_meta( $order_id, '_message_field', true ); ?></td>
</tr>
<tr>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_message_field', true ) . '</p>';
// echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
?>*
Instead of hooking into woocommerce_checkout_after_customer_details you need to hook into woocommerce_checkout_before_customer_details, that way your custom field will appear on top of other fields.
So change the following line of code
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
to
add_action( 'woocommerce_checkout_before_customer_details' ,'kia_extra_checkout_fields' );
Please try to remove class="extra-fields"
<div id="my_custom_checkout_field">
<h3><?php _e( 'WRITE A MESSAGE TO RECIPIENT' ); ?><span>(<?php _e( 'Leave blank if not required' ); ?>)</span></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
I have used it with id and it works well for me. Like this

Wordpress/WooCommerce Registration

I'm using a plugin (User Profiles Made Easy) that allows a user to choose their own role when registering on my website. It does a good job with that, but what it doesn't do is allow me to conditionally show/hide other fields based on the role they choose.
I thought of Gravity Form and their User Registration add-on, but it won't allow the user to choose their own role when registering.
The WooCommerce tie-in is this: When the buyer checks out, I could collect the extra data at that time (instead of during registration), but I would want to only show the fields relevant to their chosen role. Any ideas?
Borrowing from my tutorial on cutomizing WooCommerce checkout I think we can just wrap certain fields role/capability conditional logica via current_user_can().
// Add a new checkout field
function kia_filter_checkout_fields($fields){
if( current_user_can('some_capability' ) ){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
if( current_user_can('some_capability' ) ){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php } ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( current_user_can( 'some_capability' && isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){
if( current_user_can('some_capability' ) ){
?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'Some Field:' ); ?></th>
<td data-title="Telephone"><?php echo get_post_meta( $order_id, '_some_field', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
}
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){
if( current_user_can('some_capability' ) ){
?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

Custom Post Type ID Issue - When Querying Other Post Types

I have quite a complex problem to solve which has baffled me for the last day or so.
Basically, I have two custom post types 'Sessions' and 'Recipes'. These are showing fine in the backend of WordPress.
Attached to the session are several meta boxes, 'Introduction Video', 'Session video', 'Goals', 'Fitness Level' and 'Featured Recipes'.
The 'Featured Recipes' meta box is special, as in it queries the WordPress database to get all posts within the 'recipe' custom post type and then use these values to populate a select box.
Now as you can see from the screen shot attached, the select box for the recipe custom post type is pulling through the correct items and ID's, and it also saves the data and pulls back the correct selection and associates it with the correct post ID for the 'session' post type. Great!
The problem arises with the other fields within the post, they seem to be using the first recipe ID as the value for pulling through the data for each field, rather than the 'session' custom post type ID.
Now I can get this to work by writing various while loops based on specific query arguments but that creates another issue. The data is saved in the correct place but it uses the first 'session' cpt ID to pull back the data for all posts.
Now, I believe it is something to do with the 'recipe' meta box creation, as as soon as you remove this function and it's related functions from the functions.php file, everything works as it should.
I think I should be using for loops instead of while loops, I'm going to try this now.
Hopefully some of you PHP gurus out there will be able to point me in the right direction. I've included both my functions.php file and a screen shot from the admin. Cheers.
<?php
add_action( 'init', 'tfp_session_cpt' );
function tfp_session_cpt() {
register_post_type( 'session',
array(
'labels' => array(
'name' => __( 'Sessions' ),
'singular_name' => __( 'Session' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'tfp_recipe_cpt' );
function tfp_recipe_cpt() {
register_post_type( 'recipe',
array(
'labels' => array(
'name' => __( 'Recipes' ),
'singular_name' => __( 'Recipe' )
),
'public' => true,
'has_archive' => true,
)
);
}
/*add_action( 'init', 'tfp_tip_cpt' );
function tfp_tip_cpt() {
register_post_type( 'tip',
array(
'labels' => array(
'name' => __( 'Tips' ),
'singular_name' => __( 'Tip' )
),
'public' => true,
'has_archive' => true,
)
);
}*/
add_action( 'add_meta_boxes', 'tfp_add_introduction_video' );
add_action( 'add_meta_boxes', 'tfp_add_session_video' );
add_action( 'add_meta_boxes', 'tfp_add_goals' );
add_action( 'add_meta_boxes', 'tfp_add_levels' );
add_action( 'add_meta_boxes', 'tfp_add_recipes' );
/*add_action( 'add_meta_boxes', 'tfp_add_tips' );*/
add_action( 'save_post', 'tfp_save_introduction_video');
add_action( 'save_post', 'tfp_save_session_video');
add_action( 'save_post', 'tfp_save_goals');
add_action( 'save_post', 'tfp_save_levels');
add_action( 'save_post', 'tfp_save_recipes');
/*add_action( 'save_post', 'tfp_save_tips');*/
function tfp_add_introduction_video() {
add_meta_box('tfp_introduction_video', 'Introduction Video', 'tfp_introduction_video_html', 'session');
}
function tfp_add_session_video() {
add_meta_box('tfp_session_video', 'Session Video', 'tfp_session_video_html', 'session');
}
function tfp_add_goals() {
$types = array ('session');
foreach ($types as $type) {
add_meta_box('tfp_goals', 'Goals', 'tfp_goals_html', $type);
}
}
function tfp_add_levels() {
add_meta_box('tfp_levels', 'Fitness Levels', 'tfp_levels_html', 'session');
}
function tfp_add_recipes() {
add_meta_box('tfp_recipes', 'Select a Featured Recipe', 'tfp_recipes_html', 'session', 'side');
}
function tfp_add_tips() {
add_meta_box('tfp_tips', 'Tips', 'tfp_tips_html', 'session', 'side');
}
function tfp_introduction_video_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$introductionVideo = get_post_meta( $postID, 'introduction_video_embed_code', true );
echo $post->ID;
?>
<label for="introduction_video_embed_code">YouTube Embed Code</label>
<textarea class="large-text code" id="introduction_video_embed_code" name="introduction_video_embed_code" value="<?php echo $introductionVideo; ?>"><?php echo get_post_meta( $post->ID, 'introduction_video_embed_code', true ); ?></textarea>
<?php }
function tfp_session_video_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$sessionVideo = get_post_meta( $post->ID, 'session_video_embed_code', true ); ?>
<?php echo $post->ID; ?>
<label for="session_video_embed_code">YouTube Embed Code</label>
<textarea class="large-text code" id="session_video_embed_code" name="session_video_embed_code" value="<?php echo $sessionVideo; ?>"><?php echo $sessionVideo; ?></textarea>
<?php }
function tfp_goals_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$goals = get_post_meta( $post->ID, 'goals', true ); ?>
<?php echo $post->ID; ?>
<label for="weight_loss">
<input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
<?php _e("Weight Loss"); ?>
</label>
<br />
<label for="improve_fitness">
<input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Fitness"); ?>
</label>
<br />
<label for="improve_health">
<input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Health"); ?>
</label>
<br />
<?php }
function tfp_levels_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$levels = get_post_meta( $post->ID, 'levels', true ); ?>
<?php echo $post->ID; ?>
<label for="beginner">
<input type="checkbox" name="level[]" id="beginner" value="Beginner" <?php if (in_array('Beginner', $levels)) echo "checked='checked'"; ?> />
<?php _e("Beginner"); ?>
</label>
<br />
<label for="advanced">
<input type="checkbox" name="level[]" id="advanced" value="Advanced" <?php if (in_array('Advanced', $levels)) echo "checked='checked'"; ?> />
<?php _e("Advanced"); ?>
</label>
<br />
<?php }
function tfp_recipes_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$recipes = get_post_meta( $post->ID, 'recipe_name', true );
$recipeArgs = array(
'post_type' => 'recipe'
);
$recipeQuery = new WP_Query($recipeArgs); ?>
<?php echo $post->ID; ?>
<select name="recipe_names" id="recipes">
<option value="null">Select a Featured Recipe</option>
<?php while ( $recipeQuery->have_posts() ) : $recipeQuery->the_post(); ?>
<option id="<?php echo $post->ID; ?>" value="<?php the_title(); ?>" <?php $title = get_the_title(); if($recipes == $title) echo "selected='selected'";?>><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
<?php }
/*function tfp_tips_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$tipArgs = array(
'post_type' => 'tip'
);
$tipQuery = new WP_Query($tipArgs); ?>
<?php echo $post->ID; ?>
<?php while ( $tipQuery->have_posts() ) : $tipQuery->the_post();
$tipID = 'tip_' . $post->ID;
$tips = get_post_meta( $post->ID, $tipID, true );?>
<?php echo $tipID; ?>
<label for="<?php echo $tipID; ?>">
<input type="checkbox" name="<?php echo $tipID; ?>" id="<?php echo $tipID; ?>" value="1" <?php if ($tips == 1) echo "checked='checked'"; ?> />
<?php the_title(); ?>
</label>
<br />
<?php endwhile;
}*/
function tfp_save_introduction_video($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'introduction_video_embed_code', $_POST['introduction_video_embed_code']);
}
function tfp_save_session_video($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'session_video_embed_code', $_POST['session_video_embed_code']);
}
function tfp_save_goals($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'goals', $_POST['goal']);
}
function tfp_save_levels($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'levels', $_POST['level']);
}
function tfp_save_recipes($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'recipe_name', $_POST['recipe_names']);
}
/*function tfp_save_tips($post) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;
global $post;
$tipArgs = array(
'post_type' => 'tip'
);
$tipQuery = new WP_Query($tipArgs);
while ( $tipQuery->have_posts() ) : $tipQuery->the_post();
$tipID = 'tip_' . $post->ID;
update_post_meta($post->ID, $tipID, $_POST[$tipID]);
endwhile;
}*/
// Extra user profile fields, specifically goal and fitness level checkboxes and radio buttons
add_action( 'show_user_profile', 'gaf_user_profile_fields' );
add_action( 'edit_user_profile', 'gaf_user_profile_fields' );
// Function to generate field HTML
function gaf_user_profile_fields( $user ) { ?>
<h3><?php _e("Goals and Fiteness Level", "blank"); ?></h3>
<table class="form-table">
<tr>
<th scope="row"><?php _e("Goals"); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e("Goals"); ?></span>
</legend>
<?php $single = true; ?>
<?php $goals = get_user_meta( $user->ID, 'goals', $single ); ?>
<label for="weight_loss">
<input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
<?php _e("Weight Loss"); ?>
</label>
<br />
<label for="improve_fitness">
<input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Fitness"); ?>
</label>
<br />
<label for="improve_health">
<input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Health"); ?>
</label>
<br />
</fieldset>
</td>
</tr>
<tr>
<?php $fitnessLevel = get_the_author_meta('fitness_level', $user->ID); ?>
<th scope="row"><?php _e("Fitness Level"); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e("Fitness Level"); ?></span>
</legend>
<input class="tog" type="radio" name="fitness_level" value="beginner" <?php if ($fitnessLevel == 'beginner') { ?>checked="checked"<?php } ?> /><label>Beginner</label>
<br />
<input class="tog" type="radio" name="fitness_level" value="advanced" <?php if ($fitnessLevel == 'advanced') { ?>checked="checked"<?php } ?> /><label>Advanced</label>
<br />
</fieldset>
</td>
</tr>
</table>
<?php }
// Function and actions to save the data from the fields
add_action( 'personal_options_update', 'save_gaf_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_gaf_user_profile_fields' );
function save_gaf_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'goals', $_POST['goal'] );
update_user_meta( $user_id, 'fitness_level', $_POST['fitness_level'] );
}
?>
[EDIT]
I'm certainly getting closer with this function but it's not quite right as it's pulling through the same title.
function tfp_recipes_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$recipeArgs = array(
'post_type' => 'recipe'
);
$recipeQuery = get_posts($recipeArgs);
$recipeName = get_post_meta( $post->ID, 'recipe_name', true );
$recipeTitle[] = get_query_var('name');
echo $recipeTitle;
?>
<select name="recipe_names" id="recipes">
<option value="null">Select a Featured Recipe</option>
<?php foreach ( $recipeQuery as $recipe ) : setup_postdata($recipeQuery); ?>
<option id="<?php echo $recipe->ID; ?>" value="<?php echo $recipeTitle; ?>" <?php if($recipeName == $recipeTitle) echo "selected='selected'";?>><?php echo $recipeName; ?></option>
<?php endforeach; ?>
</select>
<?php }
OK Guys, I solved my issue using the following function.
function tfp_recipes_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$recipeArgs = array(
'post_type' => 'recipe'
);
$recipes = get_posts($recipeArgs);
$recipeName = get_post_meta( $post->ID, 'recipe_name', true );
?>
<select name="recipe_names" id="recipes">
<option value="null">Select a Featured Recipe</option>
<?php foreach ( $recipes as $recipe ) : setup_postdata($recipe); ?>
<option id="<?php echo $recipe->ID; ?>" value="<?php $recipeTitle = $recipe->post_title; echo $recipeTitle; ?>" <?php if($recipeName == $recipeTitle) echo "selected='selected'";?>><?php echo $recipeTitle; ?></option>
<?php endforeach; ?>
</select>
<?php }
Though now I'm having another issue saving another custom metabox. The function to add the metabox is:
function tfp_tips_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
echo $post->ID;
$tipArgs = array(
'post_type' => 'tip'
);
$tips = get_posts($tipArgs);
$tipData = get_post_meta( $post->ID, $tipID, true );
foreach ( $tips as $tip ) : setup_postdata($tip);
$tipID = 'tip_' . $tip->ID; ?>
<label for="<?php echo $tipID; ?>">
<input type="checkbox" name="<?php echo $tipID; ?>" id="<?php echo $tipID; ?>" value="1" <?php if ($tipData == 1) echo "checked='checked'"; ?> />
<?php $tipTitle = $tip->post_title; echo $tipTitle; ?>
</label>
<br />
<?php endforeach;
}
Similar to the recipe function except this adds checkboxes each with a specific ID. Now the function to save it is:
function tfp_save_tips($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;
global $tipID;
add_post_meta($post->ID, $tipID, $_POST[$tipID], true) or update_post_meta($post->ID, $tipID, $_POST[$tipID]);
}
The problem is it's not saving the data, and is going to a blank page.
[EDIT]
OK, I have the data saving, as well as the checkboxes being checked correctly when you go back to the post. The only issue is it's posting to a blank page, which is no good. Needs to refresh the editor.
The function 'm now using is:
function tfp_save_tips($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['session'], plugin_basename(__FILE__) ) ) return;
$tipArgs = array(
'post_type' => 'tip'
);
$tips = get_posts($tipArgs);
foreach ( $tips as $tip ) : setup_postdata($tip);
$tipID = 'tip_' . $tip->ID;
endforeach;
add_post_meta($post_id, $tipID, $_POST[$tipID], true) or update_post_meta($post_id, $tipID, $_POST[$tipID]);
}

Resources