Add Field To Custom WP Login Form - wordpress

I have a custom login form that I display on a page using the following:
$my_login_args = apply_filters( 'my_login_page_args', array(
'echo' => true,
'redirect' => site_url( $_SERVER['REQUEST_URI'] ),
'form_id' => 'my_login_form',
'label_username' => esc_html__( 'Email Address' ),
'label_password' => esc_html__( 'Password' ),
'label_remember' => esc_html__( 'Remember Me' ),
'label_log_in' => esc_html__( 'Sign In' ),
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
'remember' => true,
'value_username' => NULL,
'value_remember' => false
) );
wp_login_form( $my_login_args ); ?>
I need to pass a custom variable ($my_custom_id) stored in the login page through to the redirected page a user sees after login. Is there any way to append the URL with this variable using POST or GET or is the best solution to add this as a hidden field in the form?
Also, in terms of adding an extra field to the form I've tried adding a custom input box to the form using
add_action('login_form','my_added_login_field');
function my_added_login_field(){
//Output your HTML
?>
<p>
<label for="my_extra_field">My extra field<br>
<input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
</p>
<?php
}
However this only adds the field to the main Wordpress login and not my custom form. Any thoughts on how best to proceed?

You can you add custom field in login form at top, bottom, and in middle.
I have added login field in login_form_middle.
For Reference https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/general-template.php#L390
https://codex.wordpress.org/Customizing_the_Login_Form
/*
You can use these hooks as well to place your fields
login_form_bottom - login_form_top - login_form_middle
*/
add_filter('login_form_middle','my_added_login_field');
function my_added_login_field(){
//Output your HTML
$additional_field = '<div class="login-custom-field-wrapper"">
<label for="my_extra_field">My extra field<br>
<input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
</div>';
return $additional_field;
}

Related

How do I correctly add additional options to a WooCommerce payment gateway's options page?

I'm developing a plugin that will perform some functions when a particular payment gateway is present in WooCommerce, and it makes sense for my plugin options to be added to the same options page as the gateway's options. I don't want to change the behaviour of the gateway in any way, just display the options for my plugin on the same options page.
Based on this document, I came up with this code.
<?php
class MyPluginClass {
/**
* Initialise the class
**/
public function init() {
// Hook into the Payment tab of WooCommerce settings and add
// options to the Gateway's options page
// (Tab's ID is 'checkout' even though the tab is called payments)
add_filter( 'woocommerce_get_settings_checkout', array($this, 'add_options_to_gateway_options_page'), 10, 2 );
}
/**
* Add options to the gateway's options page
**/
public function add_options_to_gateway_options_page( $settings, $current_section_id ) {
// If the current section is not the required gateway, return the settings
// (replace gateway_id with the actual gateway ID. this is
// the 'section' argument from the URL when viewing the gateway's options page)
if ( 'gateway_id' != $current_section_id ) return $settings;
// Create settings
$my_settings = array(
array (
'title' => __('Automation options'),
'type' => 'title',
'description' => ''
),
array(
'id' => 'my_plugin_enabled',
'name' => 'my_plugin_enabled',
'title' => __('Gateway automations', 'my_plugin'),
'label' => __('Enable gateway automations', 'my_plugin'),
'description' => __('Enable the gateway automations plugin', 'my_plugin'),
'desc_tip' => true,
'type' => 'checkbox',
'default' => 'yes'
),
array(
'type' => 'sectionend',
'id' => 'my_plugin'
)
);
return $my_settings;
}
}
$myPlugin = new MyPluginClass;
$myPlugin->init();
The checkbox works and its value is saving to the DB and reading just fine on page load, along with the rest of the options on the page. The title and checkbox are displayed on the screen in the expected location and the checkbox is checked on the first load as per the default. But, the label and tooltip with the description are not displayed.
// The resulting HTML
<h2>Automation options</h2>
<table class="form-table">
<tbody>
<tr valign="top" class="">
<th scope="row" class="titledesc">Gateway automations</th>
<td class="forminp forminp-checkbox">
<fieldset>
<legend class="screen-reader-text"><span>Gateway automations</span></legend>
<label for="my_plugin_enabled">
<input name="my_plugin_enabled" id="my_plugin_enabled" type="checkbox" class="" value="1">
</label>
</fieldset>
</td>
</tr>
</tbody>
</table>
Am I doing something wrong or is there a bug somewhere?

How to add custom fields to WooCommerce product data panel

I have created a custom product type for events in WooCommerce. Now I want to add some custom fields to it.
This can be achieved with functions like woocommerce_wp_select(), woocommerce_wp_text_input() etc. However, as far as I know, whith these function you can only add text input fields, textarea's, select boxes and select dropdowns.
I want to add a date picker field, a file upload field and an url input field.
I have created these fields and they do render on the admin panel as they should, but the values are not saved (although I'm using the action hook 'woocommerce_process_product_meta'). Only the first 2 fields (event_type and event_location), which are created with the WooCommerce function, are stored properly.
What am I doing wrong here?
My code:
add_action('woocommerce_product_data_panels', 'okappi_add_custom_fields');
function okappi_add_custom_fields()
{ ?>
<div id="event_details" class="panel woocommerce_options_panel hidden">
<div class="options_group" class="show_if_event">
<? woocommerce_wp_select([
'id' => 'event_type',
'label' => __('Event type', 'custom'),
'wrapper_class' => 'show_if_event',
'value' => get_post_meta(get_the_ID(), 'event_type', true),
'options' => array('online' => 'Online', 'international' => 'International', 'internal' => 'Internal'),
]); ?>
<? woocommerce_wp_text_input([
'id' => 'event_location',
'label' => __('Event location', 'custom'),
'wrapper_class' => 'show_if_event',
'value' => get_post_meta(get_the_ID(), 'event_location', true),
]); ?>
<p class="show_if_event form-field event_start_date_field">
<label for="event_start_date">Start date</label>
<input type="date" id="event_start_date" name="event_start_date" class="date short">
</p>
<p class="show_if_event form-field event_end_date_field">
<label for="event_end_date">End date</label>
<input type="date" id="event_end_date" name="event_end_date" class="date short">
</p>
<p class="show_if_event form-field event_pdf_field">
<label for="event_pdf">PDF upload</label>
<input type="file" id="event_pdf" name="event_pdf" class="date short">
</p>
<p class="show_if_event form-field event_link_field">
<label for="event_link">Event link</label>
<input type="url" id="event_link" name="event_link" class="date short" placeholder="https://www.my-event.com/">
</p>
</div>
</div>'
<? }
add_action('woocommerce_process_product_meta', 'save_custom_fields');
function save_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$event_type = isset($_POST['event_type']) ? $_POST['event_type'] : '';
$product->update_meta_data('event_type', sanitize_text_field($event_type));
$event_location = isset($_POST['event_location']) ? $_POST['event_location'] : '';
$product->update_meta_data('event_location', sanitize_text_field($event_location));
$event_start_date = isset($_POST['event_start_date']) ? $_POST['event_start_date'] : '';
$product->update_meta_data('event_start_date', sanitize_text_field($event_start_date));
$event_end_date = isset($_POST['event_end_date']) ? $_POST['event_end_date'] : '';
$product->update_meta_data('event_end_date', sanitize_text_field($event_end_date));
$event_pdf = isset($_POST['event_pdf']) ? $_POST['event_pdf'] : '';
$product->update_meta_data('event_pdf', sanitize_text_field($event_pdf));
$event_link = isset($_POST['event_link']) ? $_POST['event_link'] : '';
$product->update_meta_data('event_link', sanitize_text_field($event_link));
$product->save();
}

page content function for wp_insert_post

I want to add two text fields of username and password in the page content. Basically i am making my plugin. I have made a page which is connected to my plugin. I want to add content in this page by using function.
my page code is:
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function() {
$post = array(
'page_template' => '',
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Checklists',
'post_status' => 'publish' ,
'post_title' => 'Checklists',
'post_type' => 'page',
'post_content' => 'my_function()'
);//insert page and save the id
$newvalue = wp_insert_post( $post, false ); //save the id in the database
update_option( 'hclpage', $newvalue );
}
my_function()
{
// A login form will be here.
}
Is it possible ??? what will be the best way to do it in the plugin file of wordpress ???
To add content I did not use function. But I use php variable.
$form = '<form action="..../wp-content/plugins/wp-link-with-parse/php-sdk/test.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<div id="lower">
<input type="submit" name="login" value="Login">
</form>';
to assign content use:
'post_content'=> $form
i know this post is from 5 years ago, but i'm currently trying to do a wordpress plugin, and i found a solution for adding content to a custom page in wordpress, what i did is to create a .php file with all the content, including php code, and then include that code in my post_type page by creating a variable and adding that file to that variable with file_get_contents, something like this:
enter image description here
hope it can be useful

Formatting buttons in a form (CodeIgniter)

Just wondering how to go about giving a form in CodeIgniter a class? I've tried just formatting buttons, hoping that the submit button would change in the form, but it didn't.
echo form_open('User/feed' class='buttonClass');
echo form_submit('NF', 'News Feed');
echo form_close();
I couldn't find anything which seemed to help me online.
Thank you!
echo form_open('User/feed', array( 'class' => 'classname' ));
// will become:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/User/feed" class="classname" />
echo form_submit('NF', 'News Feed');
// will become:
<input type="submit" name="NF" value="News Feed" />
echo form_close();
// will become:
</form></div></div>
Now keep in mind, adding classes and other attributes via the array in the first line up there only adds them to the Form line. I would recomend, if you're doing this in view, writing pure html and adding in the information needed. More like:
<form method="post" accept-charset="utf-8" action="<?= base_url('User/feed'); ?>" class="classname">
<div>
Something here for the form
<input type="text" name="stuff" />
</div>
<input type="submit" name="NF" value="News Feed" class="myButton" />
</form>
Also, of note, you can create buttons using an array and assign class and other attributes that way. Such as:
$myButton = array(
'class' => 'myButton',
'name' => 'NF',
'value' => 'News Feed',
);
echo form_button($myButton);
Lastly, and I think this is what you're aiming for, you can do the same with form_submit:
$myButton = array(
'class' => 'mySubmitButton',
'name' => 'nfSubmit',
'value' => 'Submit',
);
echo form_submit($data);
Per the documentation, the form helper accepts an array as the second parameter, allowing one to apply various options, such as a class. For example:
$attributes = array(
'class'=>'myClass'
);
echo form_open('User/feed', $attributes);
Documentation
Form Helper - http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

WordPress Radio Boxes - selects only the last one

So I have a form as shown bellow. its a bit long. It contains three radio boxes. Every time I select one, doesn't matter which, and then hit submit, the last radio element shows up as selected instead of the one I clicked. I var dump the option (in this case aisis_core['display_rows']) and it will say the value of the radio element i selected instead of the current on selected.
So I select lists, it will show lists but the radio box selected is no_posts. Can some one tell me what I am doing wrong?
<form action="options.php" method="post">
<input type='hidden' name='option_page' value='aisis_options' /><input type="hidden"
name="action" value="update" /><input type="hidden" id="_wpnonce" name="_wpnonce"
value="f0385965c6" /><input type="hidden" name="_wp_http_referer" value=
"/WordPressDev/wp-admin/admin.php?page=aisis-core-options&settings-updated=true" />
<fieldset>
<div class="control-group">
<label class="radio"><input type="radio" id="rows" class="display" name=
"aisis_core[display_rows]" value="display_rows" checked="checked" /> Display
posts as rows. </label>
<div class="control-group">
<label class="radio"><input type="radio" class="display" name=
"aisis_core[display_rows]" value="list" checked="checked" /> Display posts a
list. </label>
</div>
<div class="control-group">
<label class="radio"><input type="radio" id="noDisplay" class="display" name=
"aisis_core[display_rows]" value="no_posts" checked="checked" /> Display no
posts.</label>
<div class="no_posts_section borderBottom">
<div class="well headLine">
<h1>Display No Rows</h1>
<p>If you choose to display no rows please give me a url of the page or
content you would like to display instead.</p>
<p class="text-info"><strong>Note:</strong> Formatting of said content is
up you. All we do is display it.</p>
</div>
<div class="control-group">
<div class="controls">
<input type="url" name="aisis_core[index_page_no_posts]" value=
"http://google.ca" placeholder="Url" />
</div>
</div>
</div>
<div class="control-group">
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-large" />
</div>
</div>
</div>
</div>
</fieldset>
</form>
The function I am using from wordpress is:
checked('radio_box_value', isset($options['display_rows']), false)
Note: radio_box_value is replaced with what ever the value of the radio box is.
In this case only the last radio box has the "checked" in it's tag, when it should be which ever one I chose.
How are the elements being created?
The following is how I create the elements, they print out what you see above in the html for the radio buttons. These are done similar to, but not exactly, zend framework.
Its pretty straight forward what were doing, create the element, add the options to the element and then return it.
I hope this gives a better picture as to how these are being created.
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', isset($options['display_rows']) && $options['display_rows'] == 'no_posts', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
"checked" function seems to need the value as second parameter as is explained here http://codex.wordpress.org/Function_Reference/checked
Try like this:
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list',(isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
This will not give warning when isn't declared the variable $options['display_rows'] (that as you said is a possibility in your case) and will pass the value to the WordPress function to compare with.
You'll want to check the value in the checked condition, not you're just checking if any value is being selected, which is always true after a submit
change
checked('display_rows', isset($options['display_rows']), false)
to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
and for the list to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
I did manage to write this:
public function set_element_checked($value, $option, $key){
$options = get_option($option);
if(isset($options[$key]) && $options[$key] == $value){
return 'checked';
}
}
which does exactly what I want. compare the element value to that of the $option[$key] and if they match return checked. can be called via:
'checked' => set_element_checked('display_rows', 'aisis_core', 'display_rows');

Resources