I'm sorry if this is an old question but I really can't find answer for this. I am new to wordpress plugin development. How can I do a somewhat like a callback before a form is submitted using WP Options API? In a native PHP, we can just say as
if (isset($_POST['submit'])) {
$options = $_POST['zt_ftp_settings'];
var_dump($options);
}
However, I am using the WP Options API. It saves the data correctly, I just want to do some more after saving the data.
Here is what I did,
<form method="post" action="options.php">
<?php
settings_fields('zt_ftp_settings_options' );
do_settings_sections( 'zt_ftp_settings_options' );
$option = get_option('zt_ftp_settings');
?>
<h2>FTP Settings</h2>
<p>Please provide your ftp credentials.</p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">FTP Hostname</th>
<td>
<input name="zt_ftp_settings[zt_ftp_host_name]" type="text" id="zt_ftp_host_name" value="<?php echo $option['zt_ftp_host_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Username</th>
<td>
<input name="zt_ftp_settings[zt_ftp_user_name]" type="text" id="zt_ftp_user_name" value="<?php echo $option['zt_ftp_user_name']?>" class="regular-text">
</td>
</tr>
<tr>
<th scope="row">FTP Password</th>
<td>
<input name="zt_ftp_settings[zt_ftp_password]" type="password" id="zt_ftp_password" value="<?php echo $option['zt_ftp_password']?>" class="regular-text">
</td>
</tr>
</tbody>
</table>
<?php submit_button( 'Save Settings' ); ?>
Thanks.
Use WP AJAX functionality.
Use below JS,
$("#submit").click(function(){
var url = window.location.href;
jQuery.post(
ajaxurl,
{
'action': 'delete_search',
'tid': tid,
},
function(response){
location.reload();
});
});
Replace your submit button ID and use your form values.
Use below code in function.php
function prefix_ajax_delete_search() {
global $wpdb;
$user_id = get_current_user_id();
$wpdb->delete('sf_save_search', array('id' => $_POST['tid']) );
wp_die();
}
add_action( 'wp_ajax_delete_search', 'prefix_ajax_delete_search' );
As per your requirement change the function name and variables
Related
I need to add a users from WordPress backend. So, I want to generate unique User IDs for them. But I can achieve it. Can somebody help me out on this.
Here is what I cam so far. It is better if I could append the WordPress default generated unique user id to end of my field.
// Customize the user fields
function custom_user_profile_fields($user) {
$rand = randomGen(1000,5000,1);
$auth_Token = '2021-ABCD-'.$rand[0];
?>
<h3>Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="srn">Student Registration Number</label></th>
<td>
<input type="text" class="regular-text" name="srn" value="<?php echo (get_option('srn') ? esc_attr( get_the_author_meta( 'srn', $user->ID ) ) : $auth_Token); ?>" id="srn" readonly /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id) {
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_usermeta($user_id, 'srn', $_POST['srn']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
When a user added I can't get the previously saved Index Number to the field. It's generating another and showing again. As I am not an expert for this, could anybody help me out with this?
Thanks in advance
You were on the right path to solve the problem, but you made some mistakes.
You can use the working code below:
Creation of fields
add_action( 'user_new_form', 'bks_add_student_reg_number_field' );
add_action( 'edit_user_profile', 'bks_add_student_reg_number_field' );
add_action( 'show_user_profile', 'bks_add_student_reg_number_field' );
function bks_add_student_reg_number_field( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="student_reg_number"><?php _e("Student Registration Number"); ?></label></th>
<td>
<?php if (is_object($user)) { ?>
<input type="text" name="student_reg_number" id="student_reg_number" value="<?php echo esc_attr( get_the_author_meta( 'student_reg_number', $user->ID )); ?>" class="regular-text" readonly disabled="disabled"/><br />
<?php } else { ?>
<input type="text" name="student_reg_number" id="student_reg_number" class="regular-text" /><br />
<span class="description"><?php _e("Please enter Student registration number."); ?></span>
<?php } ?>
</td>
</tr>
</table>
<?php }
This creates a field option at new user generation and edit form.
The field is only enabled when a new user is created. If the registered user has already have a value the value would be shown with no access to change it.
Save field value
add_action( 'user_register', 'bks_student_reg_number_field' );
function bks_student_reg_number_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if(!current_user_can('manage_options'))
return false;
$reg_value = get_user_meta($user_id, 'student_reg_number', true);
if (isset( $_POST['student_reg_number']) && $_POST['student_reg_number'] !== $reg_value) {
update_user_meta($user_id, 'student_reg_number', $_POST['student_reg_number'].'-'.$user_id);
}
}
Only user_register hook is used to prevent updation on profile edit.
You can see the user_id appended with a - infront of it in the field and the field is readonly and disabled.
If you want user to be able to edit the id after registering as well then. do the following.
Add profile_update hook. add_action( 'profile_update', 'bks_student_reg_number_field' ); as well so that the code runs when you update the user as well.
Remove readonly and disabled attribute from the form.
The code is TESTED and WORKS.
In my woocommerce project I want one input field in view order page,If customer click the view order it will show one text box to add notes,I guess,this values should be store in (wp_woocommerce_order_itemmeta) table.Can you help me..I tried something..
function add_name_on_field() {
echo '<form class="cart" method="post" enctype="multipart/form-data"><table class="variations"
cellspacing="0"> <tbody><tr>
<td class="label"><label for="name">Add notes</label></td>
<td class="value">
<input type="text" name="name-on-tshirt" value="" /> <button type="submit"
class="">Submit</button</td></tr></tbody></table></form>'; }
add_filter( 'woocommerce_order_item_quantity_html',
'add_name_on_field' );
For storing I tried this..
function tshirt_order_meta_handler( $item_id ) {
if( WC()->session->__isset( $cart_item_key.'_name_on_tshirt' ) ) {
wc_add_order_item_meta( $item_id, "name_on_tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') );}}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );
You need to check out: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
There it states how to proceed doing what you want.
when i install this wordpress plugin it gives me:
The plugin generated 189 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
this is my code:
add_action('wp_head', 'wpse_43672_wp_head', 0);
/**
* Hooks into the `wp_head` action.
*/
function wpse_43672_wp_head(){ ?>
<script>
if (navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/) ||
navigator.userAgent.match(/Windows Phone/i) ||
navigator.userAgent.match(/ZuneWP7/i)
) {
// some code
self.location="<?php echo plugins_url(); ?>/zeevmobile/mobile-page.php";
}
</script>
<?php }?>
<style>
li#toplevel_page_zeevmobile-zeevmobile .wp-menu-image {
background: url(<?php echo plugins_url(); ?>/zeevmobile/images/icon.png) no-repeat 4px !important;
}
</style>
<?php
// create custom plugin settings menu
add_action('admin_menu', 'zeev_create_menu');
function zeev_create_menu() {
//create new top-level menu
add_menu_page('zeev movile redirect', 'zeev mobile', 'administrator', __FILE__, 'zeev_settings_page');
//create new top-level menu
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
//register our settings
register_setting( 'zeev-settings-group', 'zeev_above_text' );
register_setting( 'zeev-settings-group', 'zeev_normal_site' );
register_setting( 'zeev-settings-group', 'zeev_mobile_site' );
}
function zeev_settings_page() {
?>
<div class="wrap">
<h2>Your Mobile Buttons Links</h2>
<form method="post" action="options.php">
<?php settings_fields('zeev-settings-group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Some text above buttons</th>
<td><textarea name="zeev_above_text" cols="90"><?php echo get_option('zeev_above_text'); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">Normal Site Link</th>
<td><textarea name="zeev_normal_site" cols="90"><?php echo get_option('zeev_normal_site'); ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row">Mobile Site Link</th>
<td><textarea name="zeev_mobile_site" cols="90"><?php echo get_option('zeev_mobile_site'); ?></textarea></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }?>
i know that the "unexpected output" problem is coming from this file because when i delete all the php in it i don't get the problem.
can you please help me fix it ?
i uploaded it here:
http://zeevm.co.il/zeev-mobile-new.zip
Per comments, the error message is basically WP complaining that something is getting echoed when the file is included.
From a cursory glance at your code, it looks like your <style> tag is getting echoed -- it's not scoped within a function. There might be other similar issues elsewhere in the plugin.
i have built several diffrent templates for my WP site each calling diffrent metas.
ok, im looking for a way to add a meta-box that will only display if the correct page template is selected.
say i have a homepage.php template, this template calls some specific meta info that only the homepage uses, a call to action of example, so i need the meta-box to appear once the proper page template is selected and disappear if a different page template is selected.
any help is appreciated.
get_page_template will return the template you are looking at. You can switch on this to display the content you want.
ok i finally found a way around it guys and here it is:
<?php
function my_admin() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php'){
add_meta_box( 'product_info',
'Informacion de Producto',
'display_product_info_meta_box',
'ex_producto', 'normal', 'high'
);
}
}
function display_product_info_meta_box( $product_info ) {
$product_price = floatval( get_post_meta( $product_info->ID, 'product_price', true ) );
$product_code = esc_html( get_post_meta( $product_info->ID, 'product_code', true ) );
?>
<table>
<tr style="width: 100%">
<td >Precio Producto</td>
<td><input type="text" size="80" name="product_info_product_price" value="<?php echo $product_price; ?>" /></td>
</tr>
<tr style="width: 100%">
<td >Codigo de Producto</td>
<td><input type="text" size="80" name="product_info_product_code" value="<?php echo $product_code; ?>" /></td>
</tr>
</table>
<?php
}
add_action( 'admin_init', 'my_admin' );
?>
so... yeap i hope that dose someone out there some good :)
I have created a menu to manage user records for wordpress admin. While listing the record I have used check box to select and unselect the record as in Pages menu.
I have to select whole address or name on checkbox click. But here what happens is that when I click on name it selects whole checkbox including name and address. I do not have an idea how the check box works.
Here is the code
<?php
foreach($record as $record_row)
{
$name = $record_row->name;
$address = $record_row->name;
?>
<tr>
<th class="check-column" scope="row">
<input name="names[]" class="one" type="checkbox" value="<?php echo $record_row->ID; ?>">
</th>
<th class="check-column" scope="row">
<input name="address[]" class="two" type="checkbox" id="address[]" value="<?php echo $record_row->ID; ?>">
</th>
<td class="column-icon media-icon" width="30%"><?php echo $record_row->name; ?></td>
<td class="post-title page-title column-title"><?php echo $record_row->name; ?></td>
</tr>
<?php
}?>
Can anyone help me. Thanks in advance.
Get rid of the id attribute on the checkbox - it's invalid syntax for starters, and every id in your document must be unique!