I have a strange problem. I have added custom field to User Profile and used the_author_meta() function for display fields content. Yeasterday works everything great. But today it doesn't work.
Custom field is in User Profile. When I click update profile - the content of fields remains in place (so information should be saved). But on authors page where I have <?php the_author_meta( 'skills' ); ?> happens nothing.
I followed this tutorial when I was creating my own custom field:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
And this one when I tried to find the error in my code:
http://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/
They both are similar. I worked with page templates since yesterday, so it maybe caused the problem. So I deleted everything what I've added since yesterday. But nothing changed.
Here is my code:
// CUSTOM FIELD IN USER ADMIN - SAVE
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Skills - Färdigheter</h3>
<table class="form-table">
<tr>
<th><label for="skills">Skills</label></th>
<td>
<textarea type="text" name="skills" rows="4" cols="50" id="skills" class="regular-text"><?php echo esc_attr( get_the_author_meta( 'skills', $user->ID ) ); ?></textarea><br>
<span class="description">Please enter your skills.</span>
</td>
</tr>
</table>
<?php }
// CUSTOM FIELD IN USER ADMIN
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_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_user_meta( $user_id, 'skills', $_POST['skills'] );
}
Now I think that you will save the user meta successfully. Only you need to display from them.
Now you can use the function "get_user_meta($user_id, $key, $single);".
Visit it here: https://codex.wordpress.org/Function_Reference/get_user_meta
Thanks
Problem solved! The solution was to use <?php echo $curauth->skills; ?> instead of <?php the_author_meta( 'skills' ); ?> or <?php get_user_meta($user_id, $key, $single); ?>
I don't know why, but it doesn't matter :D
Related
I am using Wordpress Learnpress plugin. I want to show some custom fields. So, add_meta_box(); the output that's mean save and update is okay. But, doesn't show in the front end view of single course page template.
N.B: In post single.php, the output is okay.
Please see the following code:
function.php:
add_action( 'admin_init', 'add_my_tab1' );
function add_my_tab1(){
$screens = ['post', 'lp_course'];
add_meta_box('fav_tab1_title', 'Tab 1 Title', 'fav_tab1_title', $screens);
}
function fav_tab1_title(){
global $post;
$get_all_meta_values = get_post_custom($post->ID);
$fav_tab1_title = $get_all_meta_values["fav_tab1_title"][0];
echo '<div><label style="width:130px;display:block;">Tab 1 Title:</label>
<input type="text" name="fav_tab1_title" size="100" value="'.$fav_tab1_title.'" /></div>';
}
add_action('save_post', 'save_fav_tab1_title_title');
function save_fav_tab1_title_title(){
global $post;
update_post_meta($post->ID, "fav_tab1_title", $_POST["fav_tab1_title"]);
}
?>
In single course:
echo get_post_meta( get_the_ID(), 'fav_tab1_title', true);?>
But the output is null. Please help me...
I am trying to add 2 custom fields with 15 character input limit to Dokan Vendor Registeration form. I am completely new to wordpress and have little coding knowledge on how to do it. I need help in codes which can help me do it.
I refered other forums and changed the Dokan Seller-registeration-form.php to include these fields but they are not getting saved in the Dokan Vendor Profile Settings as well as wordpress backend.
These fields also need to be updated in the Admin Backend in wordpress.
I have tried the link below in stack as solution.
Add custom fields to existing form on Dokan (wordpress/woocoomerce).
I am missing something which i have no clue to where it needs to be modified.
I appreciate your help. Please let me know if you need any more details.
Regards
Abhilash
Here I am showing how you can add extra field in the registration form. [Not with input limit]
You need a child-theme before doing this process. If you have installed a child theme already then you can override the seller registration form through it.
Please create a folder called dokan into your child-theme and then create another folder into dokan folder called global. Now, copy the seller-registration-form.php file from your wp-content/plugins/dokan-lite/templates/global folder and paste it to your child-theme
Now, from your child-theme open the seller-registration-form.php file and add the extra field on your desire position like below –
You can use this code if you want to just copy and paste (Field for GST Number) –
<p class="form-row form-group form-row-wide">
<label for="shop-phone"><?php esc_html_e( 'GST Number', 'dokan-custom-codes' ); ?>
<span class="required">*</span></label>
<input type="text" class="input-text form-control" name="gst_id" id="gst_id" value="<?php if ( ! empty( $postdata['gst_id'] ) ) echo esc_attr($postdata['gst_id']); ?>" required="required" />
</p>
After adding the input field you will be able to see the registration field on the vendor registration form-
Now, you have to save the field data and need to show on user profile.
Just open your functions.php file and paste the below code –
function dokan_custom_seller_registration_required_fields( $required_fields ) {
$required_fields['gst_id'] = __( 'Please enter your GST number', 'dokan-custom' );
return $required_fields;
};
add_filter( 'dokan_seller_registration_required_fields',
'dokan_custom_seller_registration_required_fields' );
function dokan_custom_new_seller_created( $vendor_id, $dokan_settings ) {
$post_data = wp_unslash( $_POST );
$gst_id = $post_data['gst_id'];
update_user_meta( $vendor_id, 'dokan_custom_gst_id', $gst_id );
}
add_action( 'dokan_new_seller_created', 'dokan_custom_new_seller_created', 10, 2 );
/* Add custom profile fields (call in theme : echo $curauth->fieldname;) */
add_action( 'dokan_seller_meta_fields', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<?php if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! user_can( $user, 'dokandar' ) ) {
return;
}
$gst = get_user_meta( $user->ID, 'dokan_custom_gst_id', true );
?>
<tr>
<th><?php esc_html_e( 'Gst Number', 'dokan-lite' ); ?></th>
<td>
<input type="text" name="gst_id" class="regular-text" value="<?php
echo esc_attr($gst); ?>"/>
</td>
</tr>
<?php
}
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( 'manage_woocommerce' ) ) {
return;
}
update_usermeta( $user_id, 'dokan_custom_gst_id', $_POST['gst_id'] );
}
If you want to change the field name or meta key then you have to change the meta key or field name accordingly on every place. On this code, I have used the meta key for the field as dokan_custom_gst_id and use the field id as gst_id
After saving the above code, you will be able to use vendor GST Number on his/her user profile –
I thought of a sandbox starter plugin (sterilized code below) to save a url for a user. So in the admin section there is a button. When the user clicks it a new window opens taking them to the configured url. I know really simple.
I have the plugin form and post handling functions working. What I am now running into is confusion on using wp_options table.
Specifically how to save per USER settings into wp_options table.
I can think of three ways.
Add a third hidden field on the configuration page that contains the users id. Then the entry is made into wp_options. On recall I would find the entry in wp_options matching the userid. What I am unsure of here is if wp_options canned function would over write....In other words if user A saved www.google.com then 10 minutes later user B saves www.bing.com does the canned wp_options canned function overwrite user A? Further can I specify particulars for recall?
The second is to save the settings in the users table....as a sort of user setting...but to do that am I not stepping on convention and further risking upgrade / compatibility?
Custom table which it seems to me is the way to go....but you guys know far more than I so I wanted to get your input.
Here is the code...again sterilized so some things might not make sense because I have changed themfor my playground purposes.
function register_setting()
{
register_setting( 'Options-group', 'OptionsA_options', 'options_callback');
register_setting( 'Options-group', 'url' );
}
function options_callback()
{
alert('Options Callback');
}
add_action( 'admin_init', 'register_setting' );
function my_plugin_menu()
{
add_options_page( 'Test Options', 'Button Click', 'activate_plugins', 'UNIque Name', 'some_options' );
}
add_action( 'admin_menu', 'my_plugin_menu' );
function clicky_options()
{
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<h2>Config</h2>
<form method="post" action="somepage_options.php">
<?php settings_fields( 'Options-group' );?>
<?php do_settings_sections( 'Options-group' );?>
<table class="form-table">
<tr valign="top">
<th scope="row">Site Url (including http:// OR https:// )</th>
<td><input type="text" name="url" value="<?php echo esc_attr( get_option('url') ); ?>" /></td>
</tr>
</table>
<?php submit_button('Sign On');?>
</form>
</div>
<?php
}
Form post handling:
$url = $_POST['url'];
$FullUrl = $url."/"
header("Location: $FullUrl", true, 301);
exit();
So basically, you are trying to save the user inputs in 'wp_options' table by using 'register_settings'. Instead of that you can simply save the form and add the input to usermeta to handle the settings per user -
$user_id = get_current_user_id();
update_user_meta( $user_id, 'input_url', $_POST['url'] );
You can use 'update_user_option' if dealing with multisite.
I have been working with Wordpress and Woocommerce to add a custom meta field to my admin products page. I have everything working fine except that when I save the product, it does not show the info I entered in my meta box.
I checked the database and the data does indeed save, I saw the data I entered in the value and the meta field name was in the meta key. So that leads me to believe everything is working except showing the value on the admin product page once the product has been saved.
I think that the problem lies within one of these two functions but not certain, it could be in the save function though where it calls the update_post_meta.
here is where I think my problem is:
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
This is the function that saves the data, just incase the problem could be in here?
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
Looking through the code nothing is sticking out to me. Everything is working except when I save the product, the data entered doesnt show (but it is in the database) so Im thinking it could be a conflict with when it is saved, showing the saved values in the fields.
Thanks for your help
Update:
Specifically I think it is this line that is jamming me up:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
I know this is a little late but i managed to solve this and wanted to share it with you.
Basically WooCommerce have dropped $variation_data. You will now need to add $variation to your function and get the id. With the ID you can them get the post_meta / custom field. This can be done like so:
function variable_fields( $loop, $variation_data, $variation ) {
$get_variation_id = $variation->ID;
$data_to_get = get_post_meta($get_variation_id, '_my_custom_field', true );
This line:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
Then becomes:
value="<?php echo $data_to_get; ?>"
That's it! i hope this helps you and anyone else that comes across this page.
I would recommend trying the Advanced Custom Fields (ACF) plugin, assuming this is for your site and not code to go into a published plugin.
I also started trying to code custom fields into a site, then realised after several pages of code that goes way beyond the business needs of "just adding a field", that the ACF plugin does the same job in ten clicks and two minutes. Sometimes you just have to be pragmatic, and accept that occasionally the best way of winning the debugging game is not to play the coding game in the first place.
Apologies if that is not the answer you or SO is looking for.
Thought about asking on wordpress answers, but they would tell me it's a "plugin thing".. If you disagree then feel free to pass it over to them.
I'm using this code in my functions.php which offers a dropdown menu of available categories when a user clicks the new post button. This forces them to choose a category BEFORE the new blank post is created.
The problem is I'm getting "invalid post type" error and have it traced it back to a plugin called "adminimizer".
The new post never gets created, the address bar gets stuck at mysite.com/wp-admin/post-new.php?category_id%5B%5D=4&continue=Continue&post_type= (it should say "post" after the type=.. and I get an error box saying "invalid post type".
I'd love to hear any thoughts on why my script might not be completing. Here's the code I'm using:
//ADD DROPDOWN MENU TO CHOOSE NEW POST CATEGORY (CHOOSE THE CAT ID’S YOU WANT TO APPEAR)
add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'post';
if ( isset( $_REQUEST['post_type'] ) ) {
$post_type = $_REQUEST['post_type'];
}
// Only do this for posts
if ( 'post' != $post_type ) {
return;
}
if ( array_key_exists( 'category_id', $_REQUEST ) ) {
add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
return;
}
// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;
include( ABSPATH . 'wp-admin/admin-header.php' );
$dropdown = wp_dropdown_categories( array(
'orderby' => 'name',
'include' => '3, 4',
'name' => 'category_id[]',
'hide_empty' => false,
'echo' => false,
) );
$category_label = __( 'Create New' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>
<form method="get">
<table class="orange">
<tbody>
<tr valign="top">
<th scope="row">{$category_label}</th>
<td>{$dropdown}</td>
</tr>
<tr>
<td></td>
<th><input name="continue" type="submit" class="button-primary" value=" {$continue_label}" /></th>
</tbody>
</table>
<input type="hidden" name="post_type" value="{$post_type}" />
</form>
</div>
HTML;
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit();
}
// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
UPDATE / MORE INFO. On further investigation, I changed a bit of my code towards the end from hidden to text. This reveals an input field that is usually automatically filled with the post type "post". The adminimize plugin appears to prevent this from happening, even though I have not set it to hide anything?
<input type="text" name="post_type" value="{$post_type}" />
I guess the easy solution would be just don't use the adminimizer plugin, but it's very useful to keep things simple for new site owners. I've asked the plugin author about this, but don't expect an answer any time soon - thanks
Well it turns out that the plugin author (Frank) offers great support. He spotted there was a problem with this line & simply commented it out. Now everything works as expected :)
//extract( $GLOBALS );