Related
I'm trying to add a custom field to the dokan shipping settings... I've tried this code but it breaks the site.
add_filter( 'dokan_shipping_settings_form_top', 'extra_fields', 10, 2);
function extra_fields( $current_user, $profile_info ){
$seller_url= isset( $profile_info['seller_url'] ) ? $profile_info['seller_url'] : '';
?>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address">
<?php _e( 'Website', 'dokan' ); ?>
</label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="seller_url" id="reg_seller_url" value="<?php echo $seller_url; ?>" />
</div>
</div>
<?php
}
//save the field value
add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST['seller_url'] ) ) {
$dokan_settings['seller_url'] = $_POST['seller_url'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
// show on the store page
add_action( 'dokan_store_header_info_fields', 'save_seller_url', 10);
function save_seller_url($store_user){
$store_info = dokan_get_store_info( $store_user);
?>
<?php if ( isset( $store_info['seller_url'] ) && !empty( $store_info['seller_url'] ) ) { ?>
<i class="fa fa-globe"></i>
<?php echo esc_html( $store_info['seller_url'] ); ?>
<?php } ?>
<?php
}
I've tried to use 'dokan_shipping_settings_form_top' but it's not working. I am using the Dokan Regular Shipping (dashboard/settings/regular-shipping) rather than Zone Wise shipping.
I am trying to make custom meta box and display it's value in single post,but code doesn't display value in single post.The value is stored in post at admin side,i want to display that value as front end side as well.Right now i am using twenty seventeen theme.i am using this code for custom meta box in function.php file:
add_action( 'add_meta_boxes', 'm_param_meta_box_add' );
function m_param_meta_box_add() {
add_meta_box( 'm_param_post', 'Box Title', 'm_param_post_meta_box_cb', 'post', 'normal', 'high' );
}
function m_param_post_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
if ( isset( $values['m_meta_description'] ) ) {
$m_meta_description_text = esc_attr( $values['m_meta_description'][0] );
}
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="m_meta_description">Post Description</label></th>
<td><textarea rows="3" cols="50" name="m_meta_description"><?php echo $m_meta_description_text; ?></textarea></td>
</tr>
</table>
<?php
} // close m_param_post_meta_box_cb function
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;
// Make sure your data is set before trying to save it
if( isset( $_POST['m_meta_description'] ) ) {
update_post_meta( $post_id, 'm_meta_description', wp_kses( $_POST['m_meta_description']) );
}
}
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
The code that i am trying in single.php is look like:
<?php
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
?>
Try to insert below code in your "template-parts/post/content" file
<?php
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo 'meta box value: ' . $m_meta_description;
?>
$meta = get_post_meta($post->ID,'meta-box-text', true);
if($meta != '') {
echo $meta;
}else {
echo "Can't Display The Content";
}
Use this code.
Here meta-box-text is the name attribute.
Don't forget to insert this code in loop
Another way:
$meta_print_value=get_post_meta(get_the_ID(),'meta-box-text',true);
echo($meta_print_value);
Is there a way to add the page attachment feature to the edit media screen? So if I add/edit a media item, I can also attach/re-attach it to a page.
Here's the code I have so far:
function my_post_submitbox_misc_actions( $id ){
$post = get_post( $id );
if( $post->post_parent > 0 ) {
if( get_post( $post->post_parent ) ) {
$title = _draft_or_post_title( $post->post_parent );
}
?>
<div class="misc-pub-section misc-pub-attachment">
Attached to: <strong><?php echo $title ?></strong>
(<a class="hide-if-no-js" onclick="findPosts.open( ''media[]'', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Re-Attach' ); ?></a>)
</div>
<?php
} else { ?>
<?php _e( '(Unattached)' ); ?><br />
<a class="hide-if-no-js" onclick="findPosts.open( 'media[]', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a>
<?php
}
}
add_action( 'attachment_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
And here's my call to the database:
global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment'", $parent_id ) );
if ( isset( $attached ) ) {
$location = 'upload.php';
if ( $referer = wp_get_referer() ) {
if ( false !== strpos( $referer, 'upload.php' ) )
$location = $referer;
}
$location = add_query_arg( array( 'attached' => $attached ) , $location );
wp_redirect( $location );
}
I've use these combined functions, in my functions.php file, to add a link in the media library (upload.php in admin area) for each media item in the table to attach/re-attach that item to anything.
// Functions to allow one to re-attach an image to a post
function upload_columns( $columns ) {
// Unset( $columns['parent'] );
$columns['better_parent'] = 'Re-Attach';
return $columns;
}
add_filter( 'manage_upload_columns', 'upload_columns' );
function media_custom_columns( $column_name, $id ) {
$post = get_post($id);
if( $column_name != 'better_parent' )
return;
if( $post->post_parent > 0 ) {
if( get_post( $post->post_parent ) ) {
$title = _draft_or_post_title( $post->post_parent );
}
?>
<strong><?php echo $title ?></strong>, <?php echo get_the_time( __( 'Y/m/d' )); ?>
<br />
<a class="hide-if-no-js" onclick="findPosts.open( 'media[]', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Re-Attach' ); ?></a>
<?php
}else {
?>
<?php _e( '(Unattached)' ); ?><br />
<a class="hide-if-no-js" onclick="findPosts.open( 'media[]', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a>
<?php
}
}
add_action( 'manage_media_custom_column', 'media_custom_columns', 10, 2 );
I know this doesn't put the option where you are describing, but it's a start in the right direction.
UPDATE:
Note that I'll leave the above code in case someone wants to have the re-attach option in their library table.
As for your question... Here is the code, explanation to follow:
function my_post_submitbox_misc_actions( $id ) {
global $pagenow, $typenow;
// We only want to run the code on a specific page
if( $pagenow != 'post.php' || $typenow != 'attachment' ) {
return;
}
$post = get_post( $id );
if( $post->post_parent > 0 ) {
if( get_post( $post->post_parent ) ) {
$title = _draft_or_post_title( $post->post_parent );
}
?>
<div class="misc-pub-section misc-pub-attachment">
Attached to: <strong><?php echo $title ?></strong>
( <a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e( 'Re-Attach' ); ?></a> )
</div>
<?php
} else {
_e( '(Unattached)' ); ?><br />
<a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e( 'Attach' ); ?></a>
<?php
}
}
add_action( 'attachment_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
// Function to call the find_posts_div pop up OUTSIDE the post form
function my_post_submitbox_misc_form() {
global $pagenow, $typenow;
// We only want to run the code on a specific page
if( $pagenow != 'post.php' || $typenow != 'attachment' ) {
return;
}
// Enqueue our scripts
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox'); // needed for find posts div
wp_enqueue_script('media');
wp_enqueue_script('wp-ajax-response');
?>
<form name="plugin_form" id="plugin_form" method="post" action="/wp-content/themes/<?php echo get_template() . '/test.php'; ?>">
<?php
wp_nonce_field('plugin_nonce');
find_posts_div();
?>
</form>
<?php
}
add_filter('admin_footer','my_post_submitbox_misc_form');
BREAKDOWN
The first function is very similar to what you already have in your code above. I believe the only change I made was to add the check to make sure we only run this code on the edit attachment page. You might need to tweak this, as I tested it, but not fully.
I also changed the way we call findPosts.open(). We now pass a variable called 'action' and set it's value to 'find_posts' so we can check for it later...
So the first function simply shows the post the attachment is already attached to, and lets you re-assign it if desired... or shows you an option to just attach it. The re-attach and attach are just links that, when clicked, launch findPosts.open() which looks for a hidden div/inputs on the page... we haven't created those yet.
The second function is the key... first, you do need to enqueue scripts and one style. The import piece of code here is the find_posts_div() call. This is what makes the magic happen, but all this does is creates hidden divs and form fields in a pop-up, just waiting to be called (our anchors in the first function). This NEEDS TO BE in a separate function so that we can use add_filter to call the function OUTSIDE OF the post form.
At first I tried to have it all together in one function. The browser strips out our <form> tag because we are attempting to place a form inside another form (the post form), and that's a no no. So by calling it in the admin_footer, we load the code outside the form.
Wrapping the find_posts_div() in a form allows us to submit the results of that form to where ever we want, to do whatever we want with it. In our case, we create a new page (test.php) and submit the results there, so we can do what we need.
So far, the test.php page is as follows:
<?php
echo '<pre>';print_r($_POST);echo '</pre>';
die();
?>
This will show you all the values of $_POST... feel free to add more hidden values and what not, but the 'found_post_id' value is the post id from the selected value from the pop up. Then you can check out lines 103 - 141 of upload.php to find code to do the actual re-attach. There might be a hook or something better, but I didn't have time to look.
Hope this helps!
So after a little help from Daniel and some research, I was finally able to make it work! Here's how my code looks now:
function spd_submitbox_misc_actions( $id ) {
global $pagenow, $typenow;
// We only want to run the code on a specific page
if( $pagenow != 'post.php' || $typenow != 'attachment' ) {
return;
}
$post = get_post( $id );
if( $post->post_parent > 0 ) {
if( get_post( $post->post_parent ) ) {
$title = _draft_or_post_title( $post->post_parent );
}
?>
<div class="misc-pub-section misc-pub-attachment">
Attached to: <strong><?php echo $title ?></strong>
<a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e( ' (Re-Attach) ' ); ?></a>
</div>
<?php
} else { ?>
<div class="misc-pub-section misc-pub-attachment">
Attached to: (Unattached) <a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e( 'Attach' ); ?></a>
</div>
<?php
}
}
add_action( 'attachment_submitbox_misc_actions', 'spd_submitbox_misc_actions' );
// Function to call the find_posts_div pop up OUTSIDE the post form
function spd_post_submitbox_misc_form() {
global $pagenow, $typenow;
// We only want to run on edit media page
if( $pagenow != 'post.php' || $typenow != 'attachment' )
return;
// Enqueue our scripts
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox'); // needed for find posts div
wp_enqueue_script('media');
wp_enqueue_script('wp-ajax-response');
$send_to = get_template_directory_uri() . '/inc/spdfunctions/spd_post_actions.php';
?>
<form name="attach-to-post" method="post" action="<?php echo $send_to; ?>">
<?php find_posts_div(); ?>
<input type="hidden" name="attach-to-post" value="attach" />
<input type="hidden" name="attachment-id" value="<?php the_ID(); ?>" />
</form>
<?php
}
add_filter('admin_footer','spd_post_submitbox_misc_form');
Then to process the submission:
if ( $_REQUEST['action'] && isset( $_POST['attach-to-post'] ) ) {
$location = $_SERVER['DOCUMENT_ROOT'];
include( $location . '/wp-load.php' );
$parent_id = $_POST['found_post_id'];
$attach_id = (int) $_POST['attachment-id'];
$attached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( %d )", $parent_id, $attach_id ) );
//redirect back to edit media screen
if ( isset( $attached ) ) {
$referer = wp_get_referer();
$location = add_query_arg( array( 'attached' => $attached ) , $referer );
wp_redirect( $location );
}
}
Thanks again, Daniel, for all your help. It was very much appreciated!
I have created a 2x shortcodes that displays a youtube video and vimeo video when the user enters the videos id in it, for emample: [youtube id=""] [vimeo id=""].
Instead of entering this in my post I am trying to create a custom meta box that the user can enter the id in and then this is displayed.
So far I have been able to create a plugin to display the meta box but I'm not sure how to get it to save the youtube/vimeo ID and display the video.
Will I need 2x different meta boxes? One that the user can enter the youtube video ID in and the other where the vimeo ID can be entered and then this is displayed on the front end?
The code I have for the meta box is:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Youtube/Vimeo ID:</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $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['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
?>
I also think I need to run a loop on my single-video.php template so that wordpress knows to display the ID from the custom meta box. For the loop this is all I have so far:
$args = array( 'post_type' => 'videos', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
That isnt finished though so I would really appreciate any help.
You are going in correct way. You need to create metabox for selecting video_type it will youtube/vimeo. then you need to give one text box for adding id for respective video type.
code will look like as follows
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$vedio_type = get_post_meta($post->ID,'my_vedio_type',true);
$vedio_id = get_post_meta($post->ID,'my_meta_box_text',true);
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Select vedio type:</label>
<!-- added select for selecting vedio type -->
<select name="my_vedio_type" id="my_vedio_type">
<option <?php echo ($vedio_type == 'youtube') ? "selected='selected'" : "" ;?> value="youtube">Youtube</option>
<option <?php echo ($vedio_type == 'vimeo') ? "selected='selected'" : "" ;?> value="vimeo">Vimeo</option>
</select>
<!-- added select for selecting vedio type -->
</p>
<p>
<label for="my_meta_box_text">Youtube/Vimeo ID:</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $vedio_id; ?>" />
</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['my_vedio_type'] ) )
update_post_meta( $post_id, 'my_vedio_type', wp_kses( $_POST['my_vedio_type'], $allowed ) );
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
?>
after that you can retrieve the these values using following code in custom post type single.php file
<?php
if(get_post_meta($post->ID,'my_vedio_type',true) == "youtube"){
$youtube_id = get_post_meta($post->ID,'my_meta_box_text',true);
}
if(get_post_meta($post->ID,'my_vedio_type',true) == "vimeo"){
$vimeo_id = get_post_meta($post->ID,'my_meta_box_text',true);
}
?>
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]);
}