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);
Related
I have a custom plugin which create a custom post type on activation. I have created one sub menu page under that custom post type.My codes worked fine upto that, now am trying to save from fields to options table.But data is not saving to table. Please find the codes below.
I followed this tutorial https://tommcfarlin.com/save-wordpress-submenu-page-options/
The above tutorial works fine if i create a sub menu page under 'Tools' menu, but it is not working for page cretaed under custom post type. Please advice me what are the changes required.
Codes For Creating Sub menu
function alumns_reps_submenu()
{
add_submenu_page(
'edit.php?post_type=alumns-reps',
__( 'Settings', 'alumns-reps-settings' ),
__( 'Settings', 'alumns-reps-settings' ),
'manage_options',
'alumns-settings',
'alumns_reps_settings_callback'
);
}
Codes For View
function alumns_reps_settings_callback(){
?>
<?php
/**
* Renders the content of the submenu page for the Reps-Alumns Settings page.
*
*
*/
$settings_title = __( 'General Settings' );
?>
<div class="wrap">
<h1><?php echo esc_html( $settings_title ); ?></h1>
<form method="post" action="">
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="state-reps-headding"><?php _e( 'Headding' ); ?></label></th>
<td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding') ?>" class="regular-text" /></td>
</tr>
<tr>
<th scope="row"><label for="state-reps-text"><?php _e( 'State Reps Title' ); ?></label></th>
<td><input name="state-reps-text" type="text" id="state-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('state-reps-text') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="current-reps-tab-title"><?php _e( 'Current Rep Tab Title' ); ?></label></th>
<td><input name="current-reps-tab-title" type="text" id="current-reps-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-tab-title') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="alumini-tab-title"><?php _e( 'Alumini Tab Title' ); ?></label></th>
<td><input name="alumini-tab-title" type="text" id="alumini-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('alumini-tab-title') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="current-reps-text"><?php _e( 'Current Reps Title' ); ?></label></th>
<td><input name="current-reps-text" type="text" id="current-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-text') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><?php _e( 'No Current Reps' ); ?></th>
<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No Current Reps' ); ?></span></legend>
<textarea name="no-current-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-current-reps-text') ); ?></textarea>
</p>
</fieldset></td>
</tr>
<tr>
<th scope="row"><?php _e( 'No State Reps' ); ?></th>
<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No State Reps' ); ?></span></legend>
<textarea name="no-state-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-state-reps-text') ); ?></textarea>
</p>
</fieldset></td>
</tr>
</table>
<?php submit_button(); ?>
<?php wp_nonce_field( 'alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce' ); ?>
</form>
</div><!-- .wrap -->
<?php
}
Codes For saving data to options table
add_action( 'load-alumns-settings', 'alumns_reps_save_settings' );
/**
* The method for saving the options to the database or for deleting them
* based on what the user has specified on the settings page.
*
*/
function alumns_reps_save_settings() {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
// If the user doesn't have permission to save, then display an error message
if ( ! alums_reps_user_can_save( $action, $nonce ) ) {
return;
}
if ( isset( $_POST['state-reps-headding'] ) ) {
$main_title = $_POST['state-reps-headding'];
update_option( 'state-reps-main-headding', $main_title);
} else {
delete_option( 'state-reps-headding' );
}
if ( isset( $_POST['state-reps-text'] ) ) {
$state_reps_text = $_POST['state-reps-text'];
update_option( 'state-reps-text', $state_reps_text);
} else {
delete_option( 'state-reps-text' );
}
if ( isset( $_POST['current-reps-tab-title'] ) ) {
$current_reps_tab_title = $_POST['current-reps-tab-title'];
update_option( 'current-reps-tab-title', $current_reps_tab_title);
} else {
delete_option( 'current-reps-tab-title' );
}
if ( isset( $_POST['alumini-tab-title'] ) ) {
$alumini_tab_title = $_POST['alumini-tab-title'];
update_option( 'alumini-tab-title', $alumini_tab_title);
} else {
delete_option( 'alumini-tab-title' );
}
if ( isset( $_POST['current-reps-text'] ) ) {
$current_reps_text = $_POST['current-reps-text'];
update_option( 'current-reps-text', $current_reps_text);
} else {
delete_option( 'current-reps-text' );
}
if ( isset( $_POST['no-current-reps-text'] ) ) {
$no_current_reps_text = $_POST['no-current-reps-text'];
update_option( 'no-current-reps-text', $no_current_reps_text);
} else {
delete_option( 'no-current-reps-text' );
}
if ( isset( $_POST['no-state-reps-text'] ) ) {
$no_state_reps_text = $_POST['no-state-reps-text'];
update_option( 'no-state-reps-text', $no_state_reps_text);
} else {
delete_option( 'no-state-reps-text' );
}
}
/**
* Determines if the user has permission to save the information from the settings page
*
*
* #return bool True if the user has permission to save; false, otherwise.
*/
function alums_reps_user_can_save( $action, $nonce ) {
$is_nonce_set = isset( $_POST[ $nonce ] );
$is_valid_nonce = false;
if ( $is_nonce_set ) {
$is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
}
return ( $is_nonce_set && $is_valid_nonce );
}
You are using the wrong hook on this line:
add_action( "load-{$page_hook}", 'alumns_reps_save_settings' );
The plugin Query Monitor is a must-have to a developer. You need the screen ID:
So, your load-{$page_hook} action should be:
add_action('load-alumns-reps_page_alumns-settings', function () {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
if (!alums_reps_user_can_save($action, $nonce)) {
return;
}
// DO YOUR THING
});
This is the self-contained code I used for testing. Note the wp_die() to debug the save action.
/* Required a CPT with slug "alumns-reps" */
add_action('admin_menu', function () {
add_submenu_page(
'edit.php?post_type=alumns-reps',
__('Settings', 'alumns-reps-settings'),
__('Settings', 'alumns-reps-settings'),
'manage_options',
'alumns-settings',
'alumns_reps_settings_callback'
);
});
function alumns_reps_settings_callback() {
$settings_title = __('General Settings');
?>
<div class="wrap">
<h1><?php echo esc_html($settings_title); ?></h1>
<form method="post" action="">
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="state-reps-headding"><?php _e('Headding'); ?></label></th>
<td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding') ?>" class="regular-text" /></td>
</tr>
</table>
<?php submit_button(); ?>
<?php wp_nonce_field('alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce'); ?>
</form>
</div><!-- .wrap -->
<?php
}
add_action('load-alumns-reps_page_alumns-settings', function () {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
if (!alums_reps_user_can_save($action, $nonce)) {
return;
}
// DO YOUR THING
});
function alums_reps_user_can_save($action, $nonce) {
$is_nonce_set = isset($_POST[$nonce]);
$is_valid_nonce = false;
//wp_die(printf('<pre>%s</pre>', print_r($_POST, true)));
if ($is_nonce_set) {
$is_valid_nonce = wp_verify_nonce($_POST[$nonce], $action);
}
return ($is_nonce_set && $is_valid_nonce);
}
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 can't seem to understand how to create a form with the Wordpress Settings API that only includes a textbox where the user can insert some code and then click a button that says "Save Changes" For reference I am going for something like this:
Insert Headers an Footers Plugin Screenshot
My current code: https://paste.fedoraproject.org/paste/l8JjhNYvp6NPFG1saEsc4Q
This is one example of creating an options page: https://codex.wordpress.org/Creating_Options_Pages
<?php
// create custom plugin settings menu
add_action('admin_menu', 'my_cool_plugin_create_menu');
function my_cool_plugin_create_menu() {
//create new top-level menu
add_menu_page('My Cool Plugin Settings', 'Cool Settings', 'administrator', __FILE__, 'my_cool_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) );
//call register settings function
add_action( 'admin_init', 'register_my_cool_plugin_settings' );
}
function register_my_cool_plugin_settings() {
//register our settings
register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
register_setting( 'my-cool-plugin-settings-group', 'some_other_option' );
register_setting( 'my-cool-plugin-settings-group', 'option_etc' );
}
function my_cool_plugin_settings_page() {
?>
<div class="wrap">
<h1>Your Plugin Name</h1>
<form method="post" action="options.php">
<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-cool-plugin-settings-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 esc_attr( 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 esc_attr( get_option('some_other_option') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Options, Etc.</th>
<td><input type="text" name="option_etc" value="<?php echo esc_attr( get_option('option_etc') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
You can also use another method an use functions add_settings_section and add_settings_field which both need a callback function: http://qnimate.com/wordpress-settings-api-a-comprehensive-developers-guide/
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!