I was wondering if it is possible to make fields in the admin area (for instance on the user page) required or optional.
Here I have take reference URL https://www.cssigniter.com/how-to-add-a-custom-user-field-in-wordpress/
Showing the user field
As mentioned in the registration form, actions ‘show_user_profile‘ and ‘edit_user_profile‘ are available for adding our own user fields. The former fires when users are seeing/editing their own profile information, while the latter fires when a user (such as an admin) sees/edits another user’s profile. Both actions pass a WP_User object as their sole parameter. Our previous code, which already uses those actions, was this:
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) { ?>
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th>
<td><?php echo esc_html( get_the_author_meta( 'year_of_birth', $user->ID ) ); ?></td>
</tr>
</table>
<?php
}
Let’s go ahead and change the plain text year of birth, to an input element, so that it may accept user input.
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );
function crf_show_extra_profile_fields( $user ) {
$year = get_the_author_meta( 'year_of_birth', $user->ID );
?>
<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th>
<td>
<input type="number"
min="1900"
max="2017"
step="1"
id="year_of_birth"
name="year_of_birth"
value="<?php echo esc_attr( $year ); ?>"
class="regular-text"
/>
</td>
</tr>
</table>
<?php
}
Let’s check our profile page:
Related
I am wondering how to achieve the following.
I try to create a number field input in the wordpress user profile under contact information.
So far if i use show_user_profile & edit_user_profile hooks, the field will appear on the bottom of the profile page.
Is it possible to create a number field under the contact information part (standard in wordpress?)
I tried to add different priorities to the actions, but that didnt solve it.
So far i have the following:
function addurenuser(){
$userid = get_current_user_id();
if ( current_user_can('update_plugins',$userid)){ ?>
<table class="form-table">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr( get_the_author_meta( 'uren', $user->ID ) ); ?>">
</td>
</tr>
</table> <?php
}
}
add_action( 'show_user_profile', 'addurenuser' );
add_action( 'edit_user_profile', 'addurenuser' );
Use this code
function addurenuser()
{
echo '<script>
jQuery(document).ready(function($){
$(".your-custom-field").insertAfter($(".user-email-wrap").closest("table"));
});
</script>';
$userid = get_current_user_id();
if (current_user_can('update_plugins', $userid)) { ?>
<table class="form-table your-custom-field">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr(get_the_author_meta('uren', $userid->ID)); ?>">
</td>
</tr>
</table> <?php
}
}
add_action('show_user_profile', 'addurenuser');
add_action('edit_user_profile', 'addurenuser');
I have created a child theme for twenty-seventeen in wordpress and created a slider function to display slider images and also a custom post type for the function.
Now I created an extra menu under appearances in dashboard as Slider settings and I need to manage the slider using that settings.
In that settings I need to have the following
o Enable slider option (check box)
o Enable slider only for logged in users (check box)
o Set a global title for slider block (text field)
I Added in back end but no condition added in front end to display slides based on this.
How could I do this?
You can use this code and change page for rendering options:
<?php
add_action('admin_menu', 'create_menu');
function create_menu() {
add_options_page(__( 'Plugin Settings', 'textdomain' ),__( 'Plugin Settings', 'textdomain' ), 'administrator', __FILE__, 'settings_page', __FILE__);
add_action( 'admin_init', 'mysettings' );
}
function mysettings() {
register_setting( 'settings-group', 'enable_slider' );
register_setting( 'settings-group', 'enable_slider_loggedin' );
register_setting( 'settings-group', 'slider_title' );
}
function settings_page() {
?>
<div class="wrap">
<h2><?php _e('PluginSettings','textdomain'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'settings-group' ); ?>
<?php do_settings_sections( 'settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e(' Enable slider','textdomain'); ?></th>
<td>
<input name="enable_slider" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Enable slider only for logged in users','textdomain'); ?></th>
<td>
<input name="enable_slider_loggedin" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider_loggedin' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Title For Slider','textdomain'); ?></th>
<td><input type="text" name="slider_title" value="<?php echo get_option('slider_title'); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Using like this:
if ( get_option ('enable_slider') == 1 ) {
// enable
} else {
//disable
}
I want to show WordPress administration menus in custom dashboard widgets. How to do it?
Or just paste this tested solution in theme functions.php and modify. Then wherever you need you may call your admin setting by get_option()
corrected with input from b__ and tested again
function register_mysettings() {
register_setting( 'michal-option-group', 'new_option_name' );
register_setting( 'michal-option-group', 'some_other_option' );
}
add_action( 'admin_init', 'register_mysettings' );
function add_michal_dashboard_widget(){
wp_add_dashboard_widget(
'michal_dashboard_widget', // slug.
'Michal Dashboard Widget', // title
'michal_dashboard_widget_function' // widget code
);
}
function michal_dashboard_widget_function(){
if (isset($_POST['new_option_name'])) update_option( 'new_option_name', sanitize_text_field( $_POST['new_option_name']));
if (isset($_POST['some_other_option'])) update_option( 'some_other_option', sanitize_text_field( $_POST['some_other_option']));
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<?php settings_fields( 'michal-option-group' ); ?>
<?php do_settings_sections( 'michal-option-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Some Other Option</th>
<td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<?php
}
add_action( 'wp_dashboard_setup', 'add_michal_dashboard_widget' );
First of all: you should create a dashboard widget. you can read more about how to do it here:
Dashboard Widgets API
Now for showing the menu you should take a look at this post:
Get all available admin pages in Wordpress
Good luck!
I am using supermassive-BuddyPress theme with wordpress 3.8 and BuddyPress 1.9.I have added one additional field(Mci Number) to my registration form.
I want this xprofile field values in Wordpress user edit admin panel.
website:http://harsh031.0fees.net/register/
I tried below query and added to function.php. but ended up with nothing.Can you please help.?
<?php
add_action( 'show_user_profile', 'showmy_extra_profile_fields' );
add_action( 'edit_user_profile', 'showmy_extra_profile_fields' );
function showmy_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label>Mci Number</label></th>
<td>
<?php
if( function_exists( 'xprofile_get_field_data' ) ) {
$xprofile_value = xprofile_get_field_data('Mci Number', $user->ID );
}
else {
$xprofile_value = '';
}
?>
<input type="text" name="Mci Number" id="Mci Number" value="<?php echo esc_attr( $xprofile_value ); ?>" class="regular-text" readonly />
</td>
</tr>
</table>
<?php
}
?>
Please try below code to add the MCI Number in user profile from admin side:
function addd_new_fields($data) {
if(!empty($data)) {
$mciNumbar= get_user_meta($data->data->ID, 'mcinumber', true);
?>
<h3>Register Additional Fields</h3>
<table class="form-table">
<tr>
<th><label for="mcinumber">MCI Numbar</label></th>
<td><input type="text" id="mcinumber" class="regular-text" value="<?php echo ($mciNumbar) ? $mciNumbar: ''; ?>" name="mcinumber"></td>
</tr>
</table>
<?php
}
}
add_action('edit_user_profile','addd_new_fields',0,1);
function save_new_fields_value($user_id) {
if (!empty($user_id)) {
update_user_meta($user_id,'mcinumber', $_POST['mcinumber']);
}
}
add_action('edit_user_profile_update', 'save_new_fields_value',0,1);
I am new to the PHP and I am trying to create a theme options in wordpress. Rest of the the theme options are working fine but the option 3 which I had created is not showing the text at front end
<?php
//register settings
function theme_settings_init(){
register_setting( 'theme_settings', 'theme_settings' );
}
//add settings page to menu
function add_settings_page() {
add_menu_page( __( 'Theme Settings' ), __( 'Theme Settings' ), 'manage_options', 'settings', 'theme_settings_page');
}
//add actions
add_action( 'admin_init', 'theme_settings_init' );
add_action( 'admin_menu', 'add_settings_page' );
//define your variables
$color_scheme = array('default','blue','green',);
//start settings page
function theme_settings_page() {
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
//get variables outside scope
global $color_scheme;
?>
<div>
<div id="icon-options-general"></div>
<h2><?php _e( 'Elegant Theme Settings' ) //your admin panel title ?></h2>
<?php
//show saved options message
if ( false !== $_REQUEST['updated'] ) : ?>
<div><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'theme_settings' ); ?>
<?php $options = get_option( 'theme_settings' ); ?>
<table>
<!-- Option 1: Custom Logo -->
<tr valign="top">
<th scope="row"><?php _e( 'Custom Logo' ); ?></th>
<td><input id="theme_settings[custom_logo]" type="text" size="36" name="theme_settings[custom_logo]" value="<?php esc_attr_e( $options['custom_logo'] ); ?>" />
<label for="theme_settings[custom_logo]"><?php _e( 'Enter the URL to your custom logo' ); ?></label></td>
</tr>
<!-- Option 2: Color Scheme -->
<tr valign="top">
<th scope="row"><?php _e( 'Color Scheme' ); ?></th>
<td><select name="theme_settings[color_scheme]">
<?php foreach ($color_scheme as $option) { ?>
<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
<?php } ?>
</select>
<label for="theme_settings[color_scheme]"><?php _e( 'Choose Your Color Scheme' ); ?></label></td>
</tr>
<!-- Option 3: Intro Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Intro' ); ?></th>
<td><label for="theme_settings[headline]"><?php _e( 'Enter your Intro Headline' ); ?></label>
<br />
<textarea id="theme_settings[headline]" name="theme_settings[headline]" rows="5" cols="36"><?php esc_attr_e( $options['headline'] ); ?></textarea></td>
</tr>
<!-- Option 4: Tracking Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Tracking Code' ); ?></th>
<td><label for="theme_settings[tracking]"><?php _e( 'Enter your analytics tracking code' ); ?></label>
<br />
<textarea id="theme_settings[tracking]" name="theme_settings[tracking]" rows="5" cols="36"><?php esc_attr_e( $options['tracking'] ); ?></textarea></td>
</tr>
</table>
<p><input name="submit" id="submit" value="Save Changes" type="submit"></p>
</form>
</div><!-- END wrap -->
<?php
}
//sanitize and validate
function options_validate( $input ) {
global $select_options, $radio_options;
if ( ! isset( $input['option'] ) )
$input['option'] = null;
$input['option'] = ( $input['option'] == 1 ? 1 : 0 );
$input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
if ( ! isset( $input['radioinput'] ) )
$input['radioinput'] = null;
if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
$input['radioinput'] = null;
$input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
return $input;
}
?>
All the other options are working fine but when I am adding the text box in the intro it is not displaying the content on the front end
<?php //show tracking code for the header
echo $options['headline'];?>
Please help me
You are using the $options directly in the second option.
<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
I think it is overwriting your second option.