Echo Option Value Based On Wordpress User Role - php

If someone can figure this out, man you would make my week!
Basically what I'm trying to do is echo a different value in a option based on a the users role on Wordpress.
If user is administrator echo = user#email.com
If user is subscriber echo = admin#email.com
Here is the code I am working with:
if ( ! empty( $note_emails ) || $include_custom ) { ?>
<div class="gv-note-email-container">
<label for="gv-note-email-to-<?php echo $entry_slug_esc; ?>" class="screen-reader-text"><?php echo $strings['also-email']; ?></label>
<select class="gv-note-email-to" name="gv-note-to" id="gv-note-email-to-<?php echo $entry_slug_esc; ?>">
<?php foreach ( $note_emails as $email ) {
?>
<option value="admin#email.com">admin#email.com</option>
<?php }
if( $include_custom ) { ?>
<option value="custom"><?php echo self::strings('other-email'); ?></option>
<?php } ?>
</select>
<fieldset class="gv-note-to-container">
<?php if( $include_custom ) { ?>
<div class='gv-note-to-custom-container'>
<label for="gv-note-email-to-custom-<?php echo $entry_slug_esc; ?>"><?php echo $strings['email-label']; ?></label>
<input type="text" name="gv-note-to-custom" placeholder="<?php echo $strings['email-placeholder']; ?>" id="gv-note-to-custom-<?php echo $entry_slug_esc; ?>" value="" />
</div>
<?php } ?>
<div class='gv-note-subject-container'>
<label for="gv-note-subject-<?php echo $entry_slug_esc; ?>"><?php echo $strings['subject-label']; ?></label>
<input type="text" name="gv-note-subject" placeholder="<?php echo $strings['subject']; ?>" id="gv-note-subject-<?php echo $entry_slug_esc; ?>" value="" />
</div>
</fieldset>
</div>
<?php }

Have you tried this?
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
//This user is administrator
}

Related

how we can enable and disable required field attribute from back end using PHP Wordpress

i have a option in my plugin that make this field required or not but how we can manage please any one give me some solution i have try but fail
<div class="create-account">
<p>
<label for="label_birthday"><?php echo esc_attr( $birthday_label, 'dobc' ); ?>
<?php if ( 'yes' === $birthday_required ) : ?>
<span class="required" style="color:red;">*</span>
<?php endif; ?>
</label>
<input type="date" class="input-text" name="dobc_date_field" id="date of birthd " value="<?php echo esc_html( $show_date ); ?>" required/>
<?php wp_nonce_field( 'birthday_nonce_action_checkout', 'birday_fields_checkout_nonce' ); ?>
</p>
</div>
<div class="clear"></div>
<?php
}
If you wanna just html required attribute you can do something like this.
<?php $required = ($birthday_required == 'yes') ? 'required' : '' ; ?>
<input name="dobc_date_field" <?php echo $required; ?> />

How to get information from an already populated form

I am trying to update a database record using a submit button, however the form that information is being taken from has itself been populated by another submit button.
The PHP is below:
if (isset($_POST['edit']))
{
$editUser = User::locate($_POST['userSelect']);
$setCompany = Company::findCompany();
$exCompanyId = $editUser->user_company_id;
$isAdmin = $editUser->user_admin;
$eUserFirstname = $editUser->user_firstname;
$eUserLastname = $editUser->user_lastname;
$eUserUsername = $editUser->user_username;
$eUserEmail = $editUser->user_email;
$eUserCompany = $editUser->user_company;
$adminArray = array("Yes","No");
if ($exCompanyId > NULL)
{
$exCompany = Company::locateId($exCompanyId);
$companyValue = $exCompany->company_name;
}
else
{
$companyValue = "";
}
if ($isAdmin > 0)
{
$userAdmin = $adminArray[0];
}
else
{
$userAdmin = $adminArray[1];
}
}
if (isset($_POST['confirm']))
{
$updateUserRecord = User::locate($eUserUsername);
$newCompanyValue = $_POST['setCompany'];
$isAdmin = $_POST['userAdmin'];
$newCompany = Company::locate($newCompanyValue);
$companyId = $newCompany->company_id;
$updateUserRecord->user_company_id = $companyId;
$updateUserRecord->user_admin = $isAdmin;
$editUser->updateUserAdmin();
$message = "You have successfully updated the user account";
echo "<script type='text/javascript'>alert('$message');</script>";
}
The HTML code is below:
<form action="" method="post">
<p>
<label>First Name:</label>
<label><?php echo $eUserFirstname ?></label>
</p>
<p>
<label>Last Name:</label>
<label><?php echo $eUserLastname ?></label>
</p>
<p>
<label>Username:</label>
<label><?php echo $eUserUsername ?></label>
</p>
<p>
<label>Email Address:</label>
<label><?php echo $eUserEmail ?></label>
</p>
<p>
<label>User Company:</label>
<label><?php echo $eUserCompany ?></label>
</p>
<p>
<label>Set Company:</label>
<select name="setCompany">
<option><?php echo $companyValue ?></option>
<?php
foreach ($setCompany as $srow)
{
?>
<option id="<?=
$srow->company_id
?>">
<?=
$srow->company_name
?>
</option>
<?php
}
?>
</select>
</p>
<p>
<label>Administrator:</label>
<select name="userAdmin">
<option><?php echo $userAdmin ?></option>
<?php
foreach ($adminArray as $arow)
{
?>
<option>
<?=
$arow
?>
</option>
<?php
}
?>
</select>
</p>
<input type="submit" name="cancel" value="Cancel">
<input type="submit" name="confirm" value="Confirm">
</br>
</form>
From my investigations so far the variables aren't transferring to the 2nd if statement, and I'm not sure how to make them available to it.
Store the first form's submission in hidden input fields alongside your echo statements. For example: <input type="hidden" name="field_name" value="<?php echo htmlspecialchars($field); ?>">
Or, alternatively, update the DB after first form posts.

Need to change check box to multi select box php

I need to help to convert checkbox to multi select drop down PHP code -
<?php if ( ! is_tax( 'job_listing_type' ) && empty( $job_types ) ) : ?>
<ul class="job_types">
<?php foreach ( get_job_listing_types() as $type ) : ?>
<li><label for="job_type_<?php echo $type->slug; ?>" class="<?php echo sanitize_title( $type->name ); ?>"><input type="checkbox" name="filter_job_type[]" value="<?php echo $type->slug; ?>" <?php checked( in_array( $type->slug, $selected_job_types ), true ); ?> id="job_type_<?php echo $type->slug; ?>" /> <?php echo $type->name; ?></label></li>
<?php endforeach; ?>
</ul>
<input type="hidden" name="filter_job_type[]" value="" />
<?php elseif ( $job_types ) : ?>
<?php foreach ( $job_types as $job_type ) : ?>
<input type="hidden" name="filter_job_type[]" value="<?php echo sanitize_title( $job_type ); ?>" />
<?php endforeach; ?>
<?php endif; ?>

Put the value as title in text field

i want to get the partcular text value as tittle in that field
How can i do..
<input type="text"
<?php if($name_one != '') { ?> value="<?php echo $name_one;?>" <?php echo set_value('name1'); ?>
<?php } else { ?> value="<?php echo set_value('name1'); ?>" <?php } ?>
name="act1" id="act1" title="<?php echo set_value('name1'); ?>">
You can try like this
<input type="text"
value="<?php echo (!empty($name_one)) ? $name_one : $name1 ; ?>"
Single line if condition

Trying to send a hidden input value to next page

I am trying to tweak Wordpress Jigoshop according to my customer's needs and I got a bit stuck. What I need is: when a product variation is selected, some additional options appear in the form of radio buttons which customers must chose. I've managed to get everything working, but what I need now is to send the selected radio button to the cart, checkout and so on when the submit button is clicked.
I am trying to use their product customization function to do this and their function is:
if (!function_exists('jigoshop_product_customize_panel')) {
function jigoshop_product_customize_panel() {
global $_product;
if ( isset( $_POST['Submit'] ) && $_POST['Submit'] == 'Save Personalization' ) {
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom_products[$_POST['customized_id']] = trim( wptexturize( $_POST['jigoshop_customized_product'] ));
jigoshop_session::instance()->customized_products = $custom_products;
}
if ( get_post_meta( $_product->ID , 'customizable', true ) == 'yes' ) :
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$_product->ID] ) ? $custom_products[$_product->ID] : '';
$custom_length = get_post_meta( $_product->ID , 'customized_length', true );
$length_str = $custom_length == '' ? '' : sprintf( __( 'You may enter a maximum of %s characters.', 'jigoshop' ), $custom_length );
echo '<div class="panel" id="tab-customize">';
echo '<p>' . apply_filters('jigoshop_product_customize_heading', __('Enter your personal information as you want it to appear on the product.<br />'.$length_str, 'jigoshop')) . '</p>';
?>
<form action="" method="post">
<input type="hidden" name="customized_id" value="<?php echo esc_attr( $_product->ID ); ?>" />
<?php
if ( $custom_length == '' ) :
?>
<textarea
id="jigoshop_customized_product"
name="jigoshop_customized_product"
cols="60"
rows="4"><?php echo esc_textarea( $custom ); ?>
</textarea>
<?php else : ?>
<input
type="text"
id="jigoshop_customized_product"
name="jigoshop_customized_product"
size="<?php echo $custom_length; ?>"
maxlength="<?php echo $custom_length; ?>"
value="<?php echo esc_attr( $custom ); ?>" />
<?php endif; ?>
<p class="submit"><input name="Submit" type="submit" class="button-alt add_personalization" value="<?php _e( "Save Personalization", 'jigoshop' ); ?>" /></p>
</form>
<?php
echo '</div>';
endif;
}
}
I tried modifying their function to suit my needs and this is what I've come up with (where get_cod is the id and name of the hidden input and "Adauga in cos" is the value of my submit button):
if (!function_exists('salveaza_cod_material')) {
function salveaza_cod_material() {
global $_product;
if ( isset( $_POST['submit']) && $_POST('submit') == 'Adauga in cos') {
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom_products[$_POST['customized_id']] = trim( wptexturize( $_POST['get_cod'] ));
jigoshop_session::instance()->customized_products = $custom_products;
}
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$_product->ID] ) ? $custom_products[$_product->ID] : '';
}}
However the value isn't sent to the next page. Can anybody please help? Cheers!
Here's an update containing more of the code:
<form action="<?php echo esc_url( $_product->add_to_cart_url() ); ?>" class="variations_form cart" method="post">
<fieldset class="variations">
<?php foreach ( $attributes as $name => $options ): ?>
<?php $sanitized_name = sanitize_title( $name ); ?>
<div>
<span class="select_label"><?php echo jigoshop_product::attribute_label('pa_'.$name); ?></span>
<select id="<?php echo esc_attr( $sanitized_name ); ?>" name="tax_<?php echo $sanitized_name; ?>">
<option value=""><?php echo __('Choose an option ', 'jigoshop') ?>…</option>
<?php foreach ( $options as $value ) : ?>
<?php if ( taxonomy_exists( 'pa_'.$sanitized_name )) : ?>
<?php $term = get_term_by( 'slug', $value, 'pa_'.$sanitized_name ); ?>
<option value="<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?> </option>
<?php else : ?>
<option value="<?php echo esc_attr( sanitize_title( $value ) ); ?>"><?php echo $value; ?></option>
<?php endif;?>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>
</fieldset>
<div id="piele-neagra" class="colors" style="display:none">
<ul class="materiale">
<li><input type="radio" name="piele-neagra" value="L73">
<p><img class="alignnone size-full wp-image-155" title="L73" src="http://www.scaune-directoriale.ro/wp-content/uploads/materiale/piele-neagra/L73.gif" alt="L73" width="72" height="72" /></p>
</li>
</ul>
</div>
<div id="stofa-mf" class="colors" style="display:none">
<ul class="materiale">
<li><input type="radio" name="tapiterie" value="MF01" />
...
<div id="stofa-rg" class="colors" style="display:none"> Stofa RG</div>
<div class="clear"></div>
<span id="cod_material"><?php echo esc_attr( $custom ); ?></span>
<span><?php echo trim( wptexturize( $_POST['get_cod'] )); ?></span>
<div class="single_variation"></div>
<?php do_action('jigoshop_before_add_to_cart_form_button'); ?>
<div class="variations_button" style="display:none;">
<input type="hidden" name="variation_id" value="" />
<input type="hidden" name="customized_id" value="<?php echo esc_attr( $_product->ID ); ?>" />
<input type="hidden" id="get_cod" name="get_cod" value="" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<div class="quantity"><input name="quantity" value="1" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div>
<input type="submit" id="submit_cart" class="button-alt" value="<?php esc_html_e('Add to cart', 'jigoshop'); ?>" />
</div>
<?php do_action('salveaza_cod_material'); ?>
<?php do_action('jigoshop_add_to_cart_form'); ?>
</form>
And this is the section of the cart that receives the value:
if ( !empty( $values['variation_id'] )) {
$product_id = $values['variation_id'];
} else {
$product_id = $values['product_id'];
}
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$product_id] ) ? $custom_products[$product_id] : '';
if ( ! empty( $custom_products[$product_id] ) ) :
?>
<dl class="customization">
<dt class="customized_product_label"><?php echo apply_filters('jigoshop_customized_product_label', __('Personal: ','jigoshop') ); ?></dt>
<dd class="customized_product"><?php echo esc_textarea( $custom ); ?></dd>
</dl>
<? php
endif;
?>
It's just a typo: in if ( isset( $_POST['submit']) && $_POST('submit') == 'Adauga in cos'), replace $_POST('submit')by $_POST['submit']

Categories