I'm not entirely sure way but the default values for a custom form aren't displaying. Any ideas?
Is it something to do with locating templates?
$blav_options_address = array(
'street' => 'Street Address',
'county' => 'County Address',
'postcode' => 'Postcode',
);
function blavou_setup_address() {
global $blav_options_address;
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
?>
<?php if ( isset( $_GET['settings-updated'] ) ) {
echo "<div class='updated'><p>Settings Saved. <a href='?page=blav-setup-social'>Continue Setup</a>.</p></div>";
} ?>
<?php locate_template( 'admin/templates/blav-setup-address.php', TRUE, TRUE ); ?>
<?php
}
<div id="blav-wrapper">
<div class="blav-nav-wrapper">
<h5 class="standard-title">Photographers Address</h5>
<form method="post" action="options.php" class="standard-form">
<?php $settings = get_option( 'blav_options_address', $blav_options_address ); ?>
<?php settings_fields( 'blav_theme_options_address' );?>
<input type="text" name="blav_options_address[street]" value="<?php esc_attr_e($settings['street']); ?>"/>
<input type="text" name="blav_options_address[county]" value="<?php esc_attr_e($settings['county']); ?>"/>
<input type="text" name="blav_options_address[postcode]" value="<?php esc_attr_e($settings['postcode']); ?>"/>
<input type="submit" value="Save"></input>
</form>
</div><!--end blav nav wrapper-->
</div><!--end blav-wrapper-->
I suspect that get_option() is not doing what you expect. When you execute:
$settings = get_option( 'blav_options_address', $blav_options_address );
then the only time that the default ($blav_options_address) will be used is if the option blav_options_address is not found. If that option is in the database, then the returned value will be used regardless of whether it is of the correct form or not, e.g.even if it is not an array. If it is not an array with the correct keys, then of course nothing will be displayed for the input values.
Related
I have been able to add a custom text input field, in woocommerce's order details page, but I can't save the data submitted data.
This is the code:
add_action( 'woocommerce_order_details_before_order_table', 'add_custom_Field',10,2 );
function add_custom_Field( $order_id) {
$user = wp_get_current_user();
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo esc_attr( $order_id->custom_URL ); ?>" />
</p>
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php
function submit()
{
update_user_meta( $user_id, 'custom_URL', sanitize_text_field( $_POST['custom_URL'] ) );
echo "Your function on button click is working";
}
if(array_key_exists('test',$_POST)){
submit();
}
}
Do you know what I'm doing wrong?
The following will allow you to add a user custom field to customer order detail and to save its value on submission:
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note based on your comment:
If the custom url has to be different for each order, you can't save it as user meta data, but instead as $order item meta data... But the code will be slightly different and you will need to define how will be your custom urls.
i have single-mypost.php to display mypost single post, in which user can submit a form for msgs (msgs is another custom post type for private messages). when user submits form new post is perfectly added to msgs but single mypost customfields are changing to empty. i have added code am using
<form method="post" action="" class="gform" onSubmit="return validatebid()">
<h2>Message</h2>
<input type="hidden" name="msgfor" value="<?php echo $post->ID; ?>"/>
<input type="hidden" name="msgby" value="<?php echo get_current_user_id(); ?>"/>
<input type="hidden" name="msgdate" value="<?php echo date('Y-m-d H:i:s'); ?>"/>
<div class="row">
<label>Message</label>
<div class="field">
<textarea name="textareafield"></textarea>
</div>
</div>
<div class="row">
<input type="hidden" name="task" value="msg" />
<input type="submit" name="submit" class="red-btn" value="Message Now"/>
</div>
</form>
once user submits form am using wp_insert_post to insert post. code i have added before get_header.
if ( isset( $_POST[ 'task' ] ) && $_POST[ 'task' ] == 'msg' ) {
$post_info = array(
'post_title' => wp_strip_all_tags( $_POST[ "msgfor" ] . '-' .
$_POST[ "msgby" ] . '-' . $_POST[ "msgdate" ] ),
'post_content' => $_POST[ 'textareafield' ],
'post_type' => 'msgs',
'post_status' => 'publish'
);
$pid = wp_insert_post( $post_info );
echo $pid;
update_post_meta( $pid, "msgfor", $_POST[ "msgfor" ] );
update_post_meta( $pid, "msgby", $_POST[ "msgby" ] );
update_post_meta( $pid, "msgdate", $_POST[ "msgdate" ] );
}
Please add
<?php if ( isset( $_POST[ 'textareafield' ] ) ) {
echo $_POST[ 'textareafield' ];
}?>
between <textarea name="textareafield"></textarea>
I think the second part of the code should be in a separate file, it will look something like this:
require('wp-load.php'); // make sure that it is the correct path to this file
... your code here ...
As an option, make the form submission via AJAX, with wp_ajax_ action hook.
The key idea is not using wp_insert_post() inside your single-mypost.php template.
i'm trying to update a custom table for my wordpress plugin, first im getting the roles from my table then display it using a form and this works fine, now the problem is that when i check the boxs it update in database, but when i uncheck it, it dont update at all and if i uncheck it all i get this error message
Warning: Invalid argument supplied for foreach() in C:\wamp\www\wp_test\wp-content\plugins\Data\settings.php on line 52
<?php
$roles=get_editable_roles();
global $wpdb;
$table_name = $wpdb->prefix. "Author_detailed_repport";
?>
<h3>Settings Page</h3>
<h4>Add/Remove a role from filter list</h4>
<p>This setting allow you to add/remove roles from the filter<br />
list, here down a list of all the roles existing in your website, all<br />
you have to do is to check/uncheck wich you wanna add/rmove from filter list</p>
<form action="<?php $_REQUEST['PHP_SELF'] ?>" method="post">
<?php
require_once('../wp-config.php');
$i=0;
foreach($roles as $role)
{
$rom=$role['name'];
$results = $wpdb->get_results( "SELECT * FROM ".$table_name." WHERE role= '".$rom."'" );
if ($results==NULL)
{$wpdb->insert( $table_name, array(
'role' => $role['name'],
'statut' => '',
'post_number' => '',
'activate' => ''
));
}?>
<input type="checkbox" name="cat[]" value="<?php echo $i+1 ;?>" <?php checked($results[0]->statut, $i+1); ?> />
<input type="hidden" name="ww" value="0">
<?php
?>
<label>
<?php echo $results[0]->role;?></label><br />
<?php $i++; } ?>
<input type="submit" value="save" name="saveme" />
</form>
<?php
if(isset($_POST['saveme']))
{
$cats=$_POST['cat'];
foreach($cats as $cam)
{
if(isset($cam))
{
$wpdb->update( $table_name, array(
'statut' => $cam
),array('ADR_id' => $cam),array('%d'));}
else
{
$wpdb->update( $table_name, array(
'statut' => '0'
),array('ADR_id' => $cam),array('%d'));
}
}
}
?>
Because $roles is not set yet and there you need to do a another check
If(isset($roles)) {
//foreach loop goes in here
}
IN QW question & Answer ( wordpress plugin ) ,how can user add question without registering(log in) or entering their email address ?
I want that all user(guest) can be able to ask their questions without entering their email address or even registering.
I had the same issue and I just resolved it.
DWQA version 1.3.3
You have to replace the function
function dwqa_require_field_submit_question()
with your own version. You find the old function at line 79 in your template-functions.php in the plugin folder.
Just removing the function from the queue will make it impossible to add new questions. So you just add the adjusted version of the function to your functions.php in your theme folder and and remove the standard function and add the adjusted function.
Add following to your functions.php:
function dwqa_require_field_submit_question_adjusted(){
?>
<input type="hidden" name="dwqa-action" value="dwqa-submit-question" />
<?php wp_nonce_field( 'dwqa-submit-question-nonce-#!' ); ?>
<?php
$subscriber = get_role( 'subscriber' );
?>
<?php if ( ! is_user_logged_in() && ! dwqa_current_user_can( 'post_question' ) ) { ?>
<input type="hidden" name="login-type" id="login-type" value="sign-up" autocomplete="off">
<div class="question-register clearfix">
<label for="user-email"><?php _e( 'You need an account to submit question and get answers. Create one:','dwqa' ) ?></label>
<div class="register-email register-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your email','dwqa' ) ?>" name="user-email">
</div>
<div class="register-username register-input">
<input type="text" size="20" value="" class="input" placeholder="Choose an username" name="user-name-signup" id="user-name-signup">
</div>
<div class="login-switch"><?php _e( 'Already a member?','dwqa' ) ?> <a class="credential-form-toggle" href="<?php echo wp_login_url(); ?>"><?php _e( 'Log In','dwqa' ) ?></a></div>
</div>
<div class="question-login clearfix dwqa-hide">
<label for="user-name"><?php _e( 'Login to submit your question','dwqa' ) ?></label>
<div class="login-username login-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your username','dwqa' ) ?>" id="user-name" name="user-name">
</div>
<div class="login-password login-input">
<input type="password" size="20" value="" class="input" placeholder="<?php _e( 'Type your password','dwqa' ) ?>" id="user-password" name="user-password">
</div>
<div class="login-switch"><?php _e( 'Not yet a member?','dwqa' ) ?> <a class="credential-form-toggle" href="javascript:void( 0 );" title="<?php _e( 'Register','dwqa' ) ?>"><?php _e( 'Register','dwqa' ) ?></a></div>
</div>
<?php } else if ( ! is_user_logged_in() && dwqa_current_user_can( 'post_question' ) ) { ?>
//Here was the old code which had shown the E-mail input and login information
<?php }
}
This function will replace the old function
function dwqa_require_field_submit_question()
Now just remove the old function and add the new to the queue in your functions.php :)
remove_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question' );
add_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question_adjusted' );
That is it :). Maybe not the best way, but it avoids changing the plugin code.
Have a nice day
//Removes BuddyBar from non-admins only
function splen_remove_admin_bar() { if( !is_super_admin() )
add_filter( ‘show_admin_bar’, ‘__return_false’ );
}
add_action(‘wp’, ‘splen_remove_admin_bar’);
Not sure what've I've done wrong, probably something really silly like I've missed an attribute or something.
Right I'm trying to build a feature on a theme that displays on the homepage. That bit I can do just fine. What I want to do is make this a little easier for the user. So they click image slider on the left hand side, add a title, add a description and! An image from the media library.
So far, to simplify my idea, I just want to add a custom meta field, as far as I understand, so the user can add an image address.
My problem is this meta box is not displaying in the admin panel and I'm not sure why? I've been reading and follow serval tutorials etc. Just can't seem to get it to display? Can anybody see anything wrong?
Here's what I've built
function post_support(){
add_theme_support('post-formats',array('aside','Image Slider'));
}
/**
* create slider, adding post register
*/
function slider_create_slider(){
register_post_type('Image Slider',
array('labels'=>
array('name'=>__('Image slider'),
'singular_name'=>__('Image slider'),
'add_new'=>__('Add New slider object'),
'edit_item'=>__('Edit Slide object'),
'new_item'=>__('Add New Slide object'),
'view_item'=>__('View slide object'),
'search_items'=>__('Search Slide Objects'),
'not_found'=>__('No Slide objects found'),
'not_found_in_trash'=>__('No slide Objects found in the bin.')),
'public'=>true,
'show_ui'=>true,
'capibility_type'=>'post','hierarchical'=>false,'rewrite'=>true,'menu_position'=>20,'supports'=>array('title','editor','thumbnail')));
}
/**
* adding slider's meta boxes
*/
function slider_add_meta_boxes($post){
//Only need to add image meta
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box',__('image'),'side','default');
}
function slider_image_meta_box(){
$image=get_post_meta($post->ID,'ImageSliderMeta',true);
//only testing atm
?>
<label>Image (url)</label><input name="ImageSliderMeta" value="<?echo$image;?>"/>
?>
}
function save_image_meta_box(){
global$post;
update_post_meta($post->ID,'ImageSliderMeta',$_POST['ImageSliderMeta']);
}
add_action('after_setup_theme','post_support');
add_action('init','slider_create_slider');
add_action('add_meta_boxes','slider_add_meta_boxes');
add_action('save_post','save_image_meta_box');
Is it because I've structured my add_action's incorrectly?
Update
Your add_meta_box function is not passing the correct arguments.
Argument #4, $screen is the post types you want to show the meta boxes for. Use the following;
function slider_create_slider(){
register_post_type('imageslider'
...
}
function slider_add_meta_boxes($post)
{
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box', 'imageslider');
}
http://codex.wordpress.org/Function_Reference/add_meta_box
I did it a few times. First, you must create the post type. In your plugin file, you must require the files, that define your custom post types and meta boxes. In your meta box file, you mostly generate a HTML code of the form element and add a function, that updates the post meta. I will give you an example (options_myplugin.php, required in the main file):
function myplugin_staff_meta_box_add() {
add_meta_box('myplugin-staff-edit', 'Staff Social Meta Box', 'myplugin_meta_box_staff_cb', 'staffs', 'normal', 'high');
}
function myplugin_meta_box_staff_cb($post) {
$position = get_post_meta($post->ID, 'position', true);
$facebook = get_post_meta($post->ID, 'facebook', true);
$twitter = get_post_meta($post->ID, 'twitter', true);
$gplus = get_post_meta($post->ID, 'gplus', true);
$linkedin = get_post_meta($post->ID, 'linkedin', true);
wp_nonce_field('staff_meta_box_nonce', 'staff_meta_box_nonce');
?>
<p class="myplugin_option_box">
<label for="position" class="myplugin-desc"><?php _e('Position', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="position" type="text" name="position"
class="myplugin-option-input" value="<?php echo $position; ?>">
<span>Ex: Project Manager</span>
</p>
<p class="myplugin_option_box">
<label for="facebook" class="myplugin-desc"><?php _e('Facebook', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="facebook" type="text" name="facebook"
class="myplugin-option-input" value="<?php echo $facebook; ?>">
<span>Ex: http://facebook.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="twitter" class="myplugin-desc"><?php _e('Twitter', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="twitter" type="text" name="twitter"
class="myplugin-option-input" value="<?php echo $twitter; ?>">
<span>Ex: http://twitter.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="gplus" class="myplugin-desc"><?php _e('Google Plus', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="gplus" type="text" name="gplus"
class="myplugin-option-input" value="<?php echo $gplus; ?>">
<span>Ex: http://plus.google.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="linkedin" class="myplugin-desc"><?php _e('LinkedIn', 'myplugin-nictiate-toolkit'); ?></label>
<input id="linkedin" type="text" name="linkedin" class="myplugin-option-input" value="<?php echo $linkedin; ?>"/>
<span>Ex: http://linkedin.com/myplugintheme</span>
</p>
<?php
}
add_action('save_post', 'myplugin_save_staff_data');
function myplugin_save_staff_data($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!isset($_POST['staff_meta_box_nonce']) || !wp_verify_nonce($_POST['staff_meta_box_nonce'], 'staff_meta_box_nonce'))
return;
if (!current_user_can('edit_post'))
$allowed = array(
'a' => array(
'href' => array()
)
);
if (isset($_POST['position']))
update_post_meta($post_id, 'position', wp_kses($_POST['position'], $allowed));
if (isset($_POST['facebook']))
update_post_meta($post_id, 'facebook', wp_kses($_POST['facebook'], $allowed));
if (isset($_POST['twitter']))
update_post_meta($post_id, 'twitter', wp_kses($_POST['twitter'], $allowed));
if (isset($_POST['gplus']))
update_post_meta($post_id, 'gplus', wp_kses($_POST['gplus'], $allowed));
if (isset($_POST['linkedin']))
update_post_meta($post_id, 'linkedin', wp_kses($_POST['linkedin'], $allowed));
}
From now you will be able to display the meta values via get_post_meta.