I added a field "money" to wp_users table, whehe store a user money amount. Now i want to take ability for admin to change that input field value.
I can get the value with function added to functions.php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>User money</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Amount</label></th>
<td>
<input type="text" name="money" id="money" value="<?php echo esc_attr( get_the_author_meta( 'money', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Enter user money</span>
</td>
</tr>
</table>
<?php }
but how can i update that field?
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
update_user_meta($user_id, 'money', $_POST['money']);
}
Would you please try above code?
finally I made in such a way
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
$money = $_POST['money'];
global $wpdb;
$wpdb->query("UPDATE wp_users SET money='$money' WHERE ID = '$user_id'");
}
Related
I'm trying to update the post content of a specified posttype. For some reason it's not working.
I have already successfully used the code in the backend in the mail page for editing. Now I want to use it also in the frontend for employees for quick administration.
$id is transferred correctly. I used this code
This is my code snippet:
<?php $id = $_GET['car']; ?>
<?php
if ( isset( $_POST['submit'] ) ){
$fields = [
'car-highlight-2',
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $id, $field, sanitize_text_field( $_POST[$field] ) );
}
}}
?>
<form method="post" action="">
<tr>
<td><input type="text" id="car-highlight-2" name="car-highlight-2" value="<?php echo get_post_meta($id, 'car-highlight-2', true); ?>"></td>
</tr>
<input type='submit' value='save' />
</form>
I forgot to add name="submit" to the input.
I'm trying to add and change the input type from text to radio button to use in my wordpress theme user profile settings like Gender: Male Female, but can't do the trick in the code. don't know how to change it.
tried with some code from w3schools given below, but that doesn't work too.
Tried radio type, but doesn't work
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="female") echo "checked";?> value="female">Female
<input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="male") echo "checked";?> value="male">Male
</div>
i have to change the code to radio type, gonna use them in wordpress functions.php
add_action( 'themer_after_user_profile_registration_fields_action', 'wpt_show_user_profile_custom_inputs' );
function wpt_show_user_profile_custom_inputs( $uid ) {
$user_new_field = get_user_meta( $uid, 'user_new_field', true );
$new_field = empty( $_POST['user_new_field'] ) ? $user_new_field : stripslashes( $_POST['user_new_field'] ); ?>
<div class="field">
<label><?php _e( 'gender', 'themer' ); ?></label>
<input type="text" value="<?php echo $new_field; ?>" name="user_new_field" size="40" placeholder="<?php echo _x( 'My New Field Placeholder', 'Placeholder for: New field', 'themer' ); ?>" />
</div>
<?php }
// Save the field value from user settings page
add_action( 'themer_user_profile_extra_fields_update', 'wpt_save_user_profile_custom_inputs' );
function wpt_save_user_profile_custom_inputs( $uid ) {
if ( isset( $_POST['save-info'] ) ) {
if ( isset( $_POST['user_new_field'] ) ) {
update_user_meta( $uid, 'user_new_field', $_POST['user_new_field'] );
}
}
}
You can do this by replacing your code with below code in functions.php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra Field", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label ><?php _e("Gender"); ?></label></th>
<td>
<input type="radio" name="gender" id="gender" value="male" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'male'){echo ' checked '; } ?>class="regular-text" />Male
<input type="radio" name="gender" id="gender" value="female" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'female'){echo ' checked '; } ?> class="regular-text" />Female
<br />
<span class="description"><?php _e("Please enter your gender."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'gender', $_POST['gender'] );
}
Totally stumped. I have a checkbox on the user profile page but it refuses to update. Its not even an array set value or anything, just never sets...Seems like no data gets passed to the post value...
updated as per #RST and now as per #simon-pollard
// This will show below the color scheme and above username field
add_action( 'profile_personal_options', 'extra_profile_fields' );
add_action( 'personal_options_update', 'update_field_value' );
add_action( 'edit_user_profile_update', 'update_field_value' );
function extra_profile_fields( $user ) {
// get the value of a single meta key
$user_id = $user->ID;
echo "user id: " . $user_id . "<br/>";
$meta_value = get_user_meta( $user->ID, 'emailPrefs', true ); // $user contains WP_User object
// do something with it.
echo "checked value: " . $meta_value. "<br/>";
?>
<h2>Email Settings</h2>
<table class="form-table">
<th scope="row" id="lbl-subtitle" for="email-settings">Email on Timer Reset</th>
<td><fieldset>
<form method='post' action="profile.php">
<input type="checkbox" id="email-settings" name='email-settings'
<?php if ($meta_value == '1'){ echo 'checked'; } ?> value ="<?php echo $meta_value?>" />
</form>
<?php submit_button(); ?>
</td>
</fieldset>
</table>
<?php
}
function update_field_value($user) {
$user_id =$user->ID;
if (isset($_POST['email-settings']) && $_POST['email-settings'] == 'on') {
update_user_meta( $user_id, 'emailPrefs', '1');
} else {
update_user_meta( $user_id, 'emailPrefs', NULL);
}
}
Thanks #Simon. With his help, I saw that the $user object was not accessible on that update hook meaning the user_meta never updated. To work around this I've created a hidden form field to store my userID so that it's available as a post variable.
Here is the Final Code:
// This will show below the color scheme and above username field
add_action( 'profile_personal_options', 'extra_profile_fields' );
add_action( 'personal_options_update', 'update_field_value' );
add_action( 'edit_user_profile_update', 'update_field_value' );
function extra_profile_fields( $user ) {
// get the value of a single meta key
$user_id = $user->ID;
echo "user id: " . $user_id . "<br/>";
$meta_value = get_user_meta( $user->ID, 'emailPrefs', true ); // $user contains WP_User object
// do something with it.
echo "checked value: " . $meta_value. "<br/>";
?>
<h2>Email Settings</h2>
<table class="form-table">
<th scope="row" id="lbl-subtitle" for="email-settings">Email on Timer Reset</th>
<td><fieldset>
<form method='post' action="profile.php">
<input type='hidden' name="user_id" value="<?php echo $user_id ?>" />
<input type="checkbox" id="email-settings" name='email-settings'
<?php if ($meta_value == '1'){ echo 'checked'; } ?> />
</form>
<?php submit_button(); ?>
</td>
</fieldset>
</table>
<?php
}
function update_field_value($user_id) {
$user_id = $_POST['user_id'];
// echo $user_id;
// echo $_POST['email-settings'];
// wp_die();
if ( isset($_POST['email-settings'], $_POST['user_id'] ) && $_POST['email-settings'] == 'on') {
update_user_meta( $user_id, 'emailPrefs', '1');
} else {
update_user_meta( $user_id, 'emailPrefs', NULL);
}
}
Leaving in the commented out portion so future readers can check this for themselves.
I made a custom field in admin category interface named custom order. It is showing in the admin. But now I am having difficulties in getting the information from the field and echo it in index.php.
If I run
$thisCat = get_category( 29);
print_r($thisCat);
I don't get any information from the custom field.
echo get_post_custom_values('category_custom_order', 29); doesn't echo anything.
How should I get the value from category_custom_order?
Here is my code for custom field in functions.php:
<?php
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Custom Order</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = 'category_custom_order_' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST['category_custom_order'] ) ) {
$option_name = 'category_custom_order_' . $term_id;
update_option( $option_name, $_POST['category_custom_order'] );
}
}
You should be able to get it this way, and by the way it's not a custom field but an option.
$category_id = 29;
$category_custom_order = get_option( 'category_custom_order_' . $category_id );
echo $category_custom_order;
I have written code for creating theme options
but default values not getting displayed in text field
Also settings saved message also not getting displayed
Thank you
<?php
// Default options values
$xyz_options = array(
'footer_copyright' => '© ' . date('Y') . ' ' . get_bloginfo('name'),
'facebook'=>'http://www.facebook.com/'
);
if ( is_admin() ) : // Load only if we are viewing an admin page
function xyz_register_settings() {
// Register settings and call xyz initiation functions
register_setting( 'xyz_theme_options', 'xyz_options', 'xyz_validate_options' );
}
add_action( 'admin_init', 'xyz_register_settings' );
function xyz_theme_options() {
// Add theme options page to the admin menu
add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'xyz_theme_options_page' );
}
add_action( 'admin_menu', 'xyz_theme_options' );
// Function to generate options page
function xyz_theme_options_page() {
global $xyz_options;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Options' ) . "</h2>";
// This shows the page's name and an icon if one has been provided ?>
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this shows the notification ?>
<form method="post" action="options.php">
<?php $settings = get_option( 'xyz_options', $xyz_options ); ?>
<?php settings_fields( 'xyz_theme_options' );
/* This function outputs some hidden fields required by the form,
including a nonce, a unique number used to ensure the form has been submitted from the admin page
and not somewhere else, very important for security */ ?>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="facebook">Facebook url</label>
</th>
<td>
<input id="facebook" name="xyz_options[facebook]" type="text" value="<?php esc_attr_e($settings['facebook']); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="footer_copyright">Footer Text 1</label>
</th>
<td>
<input id="footer_copyright" name="xyz_options[footer_copyright]" type="text" value="<?php esc_attr_e($settings['footer_copyright']); ?>" />
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save Options" />
</p>
</form>
</div>
<?php
}
function xyz_validate_options( $input ) {
global $xyz_options;
$settings = get_option( 'xyz_options', $xyz_options );
}
endif; // EndIf is_admin()
I have wriiten above code.
Please help
Thank you
I tested your code and the default values are being displayed correctly.
Doing a var_dump($_REQUEST); shows what's the problem with the update message, we have to check simpy for $_REQUEST['settings-updated'].
You can remove the is_admin() checking, as admin_init and admin_menu only run on admin.
The validation function should be something like this, validating each input field and returning a clean array with sanitized values:
function xyz_validate_options( $input ) {
$new_input = array();
if( isset( $input['facebook'] ) )
$new_input['facebook'] = esc_url( $input['facebook'] );
if( isset( $input['footer_copyright'] ) )
$new_input['footer_copyright'] = esc_attr( $input['footer_copyright'] );
return $new_input;
}