Wordpress add user number field in contact information part - wordpress

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');

Related

Custom User Settings In A Plugin Template

I have no problem submitting the initial default user data, and no problems recalling data. However, i can not figure out why this form is throwing an error (needed to submit user data).
Here is the code I'm using to create the default data...
// DEFAULT USER OPTIONS
add_action( 'admin_init', 'sci_pages_register_setting' );
function sci_pages_register_setting()
{
$user = wp_get_current_user();
$default_options=array(
'sci_avatar'=>'http://.../blank_avatar.png',
'sci_facebook'=>'',
'sci_twitter'=>''
);
add_option($user->user_nicename . '_plugin_options',$default_options);
register_setting( $user->user_nicename . '_plugin_options', $user->user_nicename . '_plugin_options', 'sci_pages_sanitize' );
}
function sci_pages_sanitize( $in )
{
return $in;
}
And this is the form code... And I'm really not sure what it is that I need to do for this to...
A. Stop throwing up an error
B. Allow users to submit this information from the front-end page.
<?PHP
function sci_options_page()
{
?>
<div class="wrap">
<h1>User Options</h1>
<?PHP
$user = wp_get_current_user();
$options = get_option($user->user_nicename . '_plugin_options');
settings_fields($user->user_nicename . '_plugin_options');
?>
<form method="post" action="#">
<table class="form-table">
<tr valign="top">
<th><h2>General</h2><th>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_avatar">Avatar:</label></th>
<td><input type="text" id="sci_logo" size="50" name="sci_options[sci_avatar]" value="<?php echo $options['sci_avatar']; ?>" /></td>
</tr>
<tr valign="top">
<th><h2>Social Links</h2><th>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_facebook">Facebook:</label></th>
<td><input type="text" id="sci_vimeo" size="50" name="sci_options[sci_facebook]" value="<?php echo $options['sci_facebook']; ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_twitter">Twitter:</label></th>
<td><input type="text" id="sci_skype" size="50" name="sci_options[sci_twitter]" value="<?php echo $options['sci_twitter']; ?>" /></td>
</tr>
</table>
<?php
submit_button();
?>
</form>
</div>
<?php
}
sci_options_page();
?>
This is what I see in the front-end when accessing the page...
What am I doing wrong (other than feeling like a complete noob right now)?

How do I create a form with a textbox when writing a Wordpress Plugin/

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/

Manage custom posttype function using an additional menu in dashboard

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
}

How to show WordPress admin menus in a custom dashboard widget?

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!

BuddyPress Xprofile fields to display on “wordpress user edit admin panel”

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);

Resources