i know it's a duplicate question, but please be kind with me, i have to solve this. I have a custom form in Wordpress, sending an email template via AJAX. Everything is working fine, except those single fields attachments in the image below. those input fields are triggered by jquery to receive the image and i have tested if the form input file are not empty with Jquery before sending the email. When i put the hard code it's works, otherwise it's not. Please guide me to solve this. Any help would be appreciate.
my HTML form:
<form id="tratamento_form" method="POST" action="#" enctype="multipart/form-data">
<div class="form-group ">
<label for="name" class="">Nome completo</label>
<input id="name" class="form-control " placeholder="José Aparecido " type="text" name="nome" id="name" autocomplete="off">
</div>
<div class="form-group ">
<label for="message" class="" >Selecione 6 fotos</label>
<div class="row">
<div class="col-sm-4">
<input type="file" id="real-file" name="img1" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button"><i class="fas fa-plus"></i></button>
<span id="custom-text">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file1" name="img2" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button1"><i class="fas fa-plus"></i></button>
<span id="custom-text1">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file2" name="img3" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button2"><i class="fas fa-plus"></i></button>
<span id="custom-text2">Enviar foto (jpg, png)</span>
</div>
</div>
</div>
</div>
<input type="file" id="testfile" name="tesinput" value="">
<span id="custom-text12">Enviar foto (jpg, png)</span>
<div class="form-group ">
<div class="row">
<div class="col-sm-4">
<input type="file" id="real-file3" name="img4" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button3"><i class="fas fa-plus"></i></button>
<span id="custom-text3">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file4" name="img5" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button4"><i class="fas fa-plus"></i></button>
<span id="custom-text4">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file5" name="img6" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button5"><i class="fas fa-plus"></i></button>
<span id="custom-text5">Enviar foto (jpg, png)</span>
</div>
</div>
</div>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg btn-block submit " value="Enviar " type="submit" name="button_submit" id="button_submit">
</div>
<!-- to be change Hidden value added for WordPress ajax security and to validate function name -->
<input type="hidden" name="action" value="field_action_for_ajax_tratamento"/>
<div class="msgs-formulario"></div>
<!-- Start success message -->
<div class="alert alert-success" id="success_message" role="alert">Mensagem enviada com sucesso <i class="far fa-thumbs-up"></i> Aguarde o nosso retorno!
</div>
</form>
function contact_ajax_tratamento() {
// This calls the javascript we just created for the form validation
wp_localize_script( 'tratamento_ajax', 'wp_ajax', array(
'tratamento' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( "ajax_nonce" ) // this is a unique token to prevent form hijacking
) );
// Enable any admin to run ajax_login() in AJAX and POST the form
add_action( 'wp_ajax_field_action_for_ajax_tratamento', 'ajax_tratamento_form_process' );
// Enable any user with no privileges to run ajax_login() in AJAX and POST the form
add_action( 'wp_ajax_nopriv_field_action_for_ajax_tratamento', 'ajax_tratamento_form_process' );
}
// Initiate the ajax enquiry form and add the validation javascript file
add_action( 'init', 'contact_ajax_tratamento' );
add_action( 'wp_enqueue_scripts', 'contact_ajax_tratamento' );
// Function to send the email using the email template
function ajax_tratamento_form_process() {
ob_start();
// include THE MAIL TEMPLATE
include('assets/template/tratamento.php');
$message = ob_get_contents();
ob_end_clean();
// HARD CODE IMAGE
// $atachments = array( get_home_path() . '/wp-content/uploads/2020/03/Wedding.jpg');
$uploadedfile = $_FILES['image'];
$upload_overrides = array( "test_form" => false );
$uploaded_file = wp_handle_upload ($file, $upload_overrides);
if( isset( $uploaded_file ["image"] )) {
$file_name_and_location = $uploaded_file ["image"];
$file_title_for_media_library = $title;
$attachment = array(
"post_mime_type" => $uploaded_file_type,
"post_title" => addslashes( $file_title_for_media_library ),
"post_content" => "",
"post_status" => "inherit"
);
if( ! is_null( $post )) {
if ( ! is_numeric( $post )) {
$post = $post->ID;
} // if ()
$attachment ['post_parent'] = $post;
} // if ()
$id = wp_insert_attachment( $attachment, $file_name_and_location );
require_once( ABSPATH."wp-admin/includes/image.php" );
$attach_data = wp_generate_attachment_metadata( $id, $file_name_and_location );
wp_update_attachment_metadata( $id, $attach_data );
} // if ()
// $headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: ' . trim( $_POST[ 'name' ] ) . ' <' . trim( $_POST[ 'email' ] ) . '>' . "\r\n";
wp_mail('example#example.com', "FORMULARIO TRATAMENTO - SUBJECT", $message, $headers, $atachments);
$results['feedbacks'] = "All OK!";
wp_send_json($results);
die();
}
Related
Here's the code for my HTML I just want to see the image, not the file name. The form is on the registration side of WordPress I passed all the data except on the attachment file, I checked some tutorials but I can't do it. Thank you.
<form method="POST" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
<?php if (isset($_COOKIE['register_error'])) { ?>
<div class="alert alert-danger" role="alert">
<?php echo $_COOKIE['register_error']; ?>
</div>
<?php } ?>
<input type="hidden" name="action" value="register_account">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>First Name <span title="required">*</span></label>
<input type="text" class="form-control" name="firstname" value="<?php echo isset($_COOKIE['register_firstname']) ? $_COOKIE['register_firstname'] : ''; ?>" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Last Name <span title="required">*</span></label>
<input type="text" class="form-control" name="lastname" value="<?php echo isset($_COOKIE['register_lastname']) ? $_COOKIE['register_lastname'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Phone <span title="required">*</span></label>
<input type="text" class="form-control" name="phone" value="<?php echo isset($_COOKIE['register_phone']) ? $_COOKIE['register_phone'] : ''; ?>" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Company <span title="required">*</span></label>
<input type="text" class="form-control" name="company" value="<?php echo isset($_COOKIE['register_company']) ? $_COOKIE['register_company'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>Address <span title="required">*</span></label>
<input type="text" class="form-control" name="address" value="<?php echo isset($_COOKIE['register_address']) ? $_COOKIE['register_address'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>City <span title="required">*</span></label>
<input type="text" class="form-control" name="city" value="<?php echo isset($_COOKIE['register_city']) ? $_COOKIE['register_city'] : ''; ?>" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>State <span title="required">*</span></label>
<input type="text" class="form-control" name="state" value="<?php echo isset($_COOKIE['register_state']) ? $_COOKIE['register_state'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>Zip Code <span title="required">*</span></label>
<input type="text" class="form-control" name="zipcode" value="<?php echo isset($_COOKIE['register_zipcode']) ? $_COOKIE['register_zipcode'] : ''; ?>" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Email Address <span title="required">*</span></label>
<input type="email" class="form-control" name="email" value="<?php echo isset($_COOKIE['register_email']) ? $_COOKIE['register_email'] : ''; ?>" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Username <span title="required">*</span></label>
<input type="text" class="form-control" name="username" value="<?php echo isset($_COOKIE['login_username']) ? $_COOKIE['login_username'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12" id="password-field"></div>
<div class="col-lg-6">
<div class="form-group">
<label>Password <span title="required">*</span></label>
<input type="password" class="form-control password" id="pw" name="password" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label>Confirm Password <span title="required">*</span></label>
<input type="password" class="form-control password" id="confirm_pw" name="confirm_password" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Tax ID <span title="required">*</span></label>
<input type="text" class="form-control" name="tax_id" value="<?php echo isset($_COOKIE['register_tax_id']) ? $_COOKIE['register_tax_id'] : ''; ?>" required>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Upload presale permit certificate:<span title="required">*</span></label>
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload" required>
</div>
</div>
</div>
<p>*Note: You must have an order with a minimum of $200.00 to checkout.</p>
<button type="submit" class="btn green--btn" id="registerbtn" name="register">Register</button>
</form>
Array Result using Print code
[file_upload] => Array ( [0] => apartment.png )
PHP Code
if (isset($_POST['register'])) {
// $username = explode('#', $_POST['email']);
// $username = $username[0];
$username = $_POST['username'];
$user_data = array(
'first_name' => $_POST['firstname'],
'last_name' => $_POST['lastname'],
'user_email' => $_POST['email'],
'user_login' => $username,
'nickname' => $username,
'user_nicename' => $username,
'user_pass' => $_POST['password'],
'role' => 'pending_wholesale_customer'
);
$user_id = wp_insert_user($user_data);
if ( is_wp_error($user_id) ) {
setcookie("register_error", $user_id->get_error_message(), time() + (60 * 5), '/');
foreach ($_POST as $key => $value) {
if ($key != 'confirm_password' && $key != 'password') {
$cookie_key = 'register_' . $key;
setcookie($cookie_key, $value, time() + (60 * 5), "/");
}
}
wp_redirect( 'new-account-registration' ); exit;
} else {
unset($_COOKIE['register_error']);
setcookie('register_error', '', time() - 3600, '/');
foreach ($_POST as $key => $value) {
unset($_COOKIE[$key]);
setcookie($key, '', time() - 3600, '/');
}
}
update_user_meta($user_id, 'billing_company', $_POST['company']);
update_user_meta($user_id, 'billing_address_1', $_POST['address']);
update_user_meta($user_id, 'billing_city', $_POST['city']);
update_user_meta($user_id, 'billing_state', $_POST['state']);
update_user_meta($user_id, 'billing_postcode', $_POST['zipcode']);
update_user_meta($user_id, 'billing_phone', $_POST['phone']);
add_user_meta($user_id, 'user_tax_id', $_POST['tax_id']);
add_user_meta($user_id, 'file_upload', $_POST['fileToUpload']);
$creds = array();
$creds['user_login'] = $_POST['email'];
$creds['user_password'] = $_POST['password'];
$user = wp_signon( $creds );
wp_redirect( 'registration-confirmation' ); exit;
}
Here's to show the value of file upload I know this one is already a file name, but I can't see the attachment file on the array code.
echo esc_html( get_the_author_meta( 'file_upload', $user->ID ) );
I am using the script below to send my contact form. But if any user refresh the page, the email is sent a second time with the same data. I tried to use
$_POST = array();
But it's not working. I also tried to use header('location'), but I always got "headers already sent issue..." so I am quite confused on how to proceed.
Here is my code, thanks for your help.
<?php if(!empty($_POST)) :
if(empty($_POST['foo'])) :
if(!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message'])):
$firstname = wp_strip_all_tags(trim($_POST['firstname']));
$lastname = wp_strip_all_tags(trim($_POST['lastname']));
$email = wp_strip_all_tags(trim($_POST['email']));
$subject = wp_strip_all_tags(stripslashes(trim($_POST['subject'])));
$message = wp_strip_all_tags(stripslashes(trim($_POST['message'])));
$content = nl2br($message);
ob_start();
include('inc/email/contact-template.php');
$html = ob_get_clean();
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n" . 'From: '. $firstname . " " . $lastname . ' <' . $email . '>' . "\r\n" . 'Reply-To: '. $firstname . " " . $lastname . ' <' . $email . '>';
if(wp_mail(get_option('admin_email'), $subject, $html, $headers)) :
$_POST = array();
$success = "Votre message a bien été envoyé. Nous vous recontacterons dans les plus brefs délais.";
else:
$errors = "Un problème est survenu. Veuillez réessayer plus tard ou nous contacter par téléphone.";
endif;
else :
$errors = "Veuillez remplir tous les champs correctement";
endif;
endif;
endif; ?>
<?php get_header() ?>
<!-- Main Content -->
<main class="container">
<!-- Content -->
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div class="py-5 px-3 px-md-5 bg-body shadow-sm">
<section class="row">
<article class="col-lg-12 col-xl-8 mb-5 mb-md-0" id="post-<?php the_ID(); ?>">
<?php the_content(); ?>
<h3>Nous contacter par email</h3>
<?php if(isset($success)) : ?>
<div class="toast text-white bg-success w-auto fade hide" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false">
<div class="d-flex">
<div class="toast-body">Votre message a bien été envoyé. Nous vous recontacterons dans les plus brefs délais.</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
<?php elseif(isset($errors)) : ?>
<div class="toast text-white bg-warning w-auto fade hide" role="status" aria-live="polite" aria-atomic="true" data-bs-autohide="false">
<div class="d-flex">
<div class="toast-body"><?= $errors ?></div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
<?php endif; ?>
<form method="post" autocomplete="off" action="<?= the_permalink() ?>" id="test">
<div class="row g-2">
<div class="col-12 col-md-6">
<label class="form-label" for="firstname">Prénom</label>
<input type="text" name="firstname" class="form-control" id="firstname" required="required" />
</div>
<div class="col-12 col-md-6">
<label class="form-label" for="lastname">Nom</label>
<input type="text" name="lastname" class="form-control" id="lastname" required="required" />
</div>
<div class="col-12 col-md-6">
<label class="form-label" for="email">Email</label>
<input type="email" name="email" class="form-control" id="email" required="required" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}$" maxlength="255" />
</div>
<div class="col-12 col-md-12">
<label class="form-label" for="subject">Sujet</label>
<input type="text" name="subject" class="form-control" id="subject" required="required" />
</div>
<div class="col-12 mb-3">
<label class="form-label" for="message">Message</label>
<textarea name="message" class="form-control" id="message" rows="5" required="required" minlength="100"></textarea>
<input type="text" name="foo" id="foo" class="foo" />
</div>
<div class="submit">
<input type="submit" class="btn btn-primary" value="Envoyer">
</div>
</div>
</form>
</article>
<aside class="col-xl-3 offset-xl-1 mt-0">
<?php get_sidebar() ?>
</aside>
</section>
</div>
<?php endwhile;
endif; ?>
</main>
<!-- End of Main Content -->
<?php get_footer() ?>
You can use Javascript here just as a tip. The problem is that the request has already been sent to the server and you are using header again. I faced the same problem using header() function.
Try this:
$location = site_url();
<!-- Redirecting to Homepage -->
<script>
window.location.replace('<?php echo $location; ?>');
</script>
I'm developing a plugin to wordpress using WordPress Plugin Boilerplate.
Therefore, I need to create a shortcode to show up my data in one page for the user.
Is it correct to concatenate the HTML as below?
function custom_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'type' => 'type'
),
$atts
);
//get all entries (page 1 with 999999 entries per page should do it:)
$data = Data::get_entries( $atts['type']);
ob_start();
?>
<div class="row">
<header><h3>Expenses</h3></header>
<p>
<span class="cf-entries-header app-name">Expense Origin</span>
<span class="cf-entries-header page-name">Expense Value</span>
</p>
<?php
foreach ( (array)$data['entries'] as $entry ) {
echo '<p class="cf-entry">’ .
‘<span class="exp-origin">' . $entry['data']['origin'] . '</span>’ .
‘<span class="exp-value">' . $entry['data']['value'] . '</span>’;
};
?>
</div>
<?php
$output = ob_get_clean();
return $output;
};
}
add_shortcode( 'cf_app_entries', 'cf_app_entries' );
No this is not right way..
You can't echo all content in shortcode..
You need to bind all content (HTML) in one variable and return this variable
Example
function loginform() {
$html = '<form class="custom_login_form" rel="" method="POST" role="form" action="" autocomplete="off">
<div class="form-group">
<div class="input-container">
<input id="useremail" type="text" class="form-control input-field" name="email" placeholder="Email">
</div>
</div>
<div class="twice-box">
<div class="form-group">
<div class="input-container">
<input id="userpassword" type="password" class="form-control input-field" name="userpassword" placeholder="Password">
</div>
</div>
<div class="form-group text-center">
Login
</div>
</div>
</form>';
return $html;
}
add_shortcode('login-form', 'loginform');
Please check this ShortCode..
If any issue please let me know..
I will be happy to help you..
I use this hook:
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_field' );
function add_custom_field( $checkout ) {
echo '<div id="custom_field_wrapper">';
woocommerce_form_field( 'custom_field', array(
'type' => 'text',
'class' => array('custom_field_class'),
'label' => __('Custom Field'),
'placeholder' => __('Please input value ...'),
'required' => false, ),
$checkout->get_value( 'custom_field' ) );
echo '</div>';
}
and i get this markup:
<div id="custom_field_wrapper">
<p class="form-row custom_field_class" id="custom_field_field" data-priority="">
<label for="custom_field" class="">Custom Field</label>
<input type="text" class="input-text " name="custom_field" id="custom_field" placeholder="Please input value ..." value="">
</p>
</div>
But the required markup looks like this:
<div class="row order-form__row">
<div class="col-lg-3 col-md-4">
<label class="label" for="index-field">Индекс:</label>
</div>
<div class="col-lg-6 col-md-6">
<input class="input" type="text" name="index" id="index-field"/>
</div>
<div class="col-lg-3 col-md-2"><a class="field-after" href="javascript:void(0);" target="_blank">Забыли индекс?</a></div>
</div>
Kind smart people help solve the dilemma ... How to change the html markup according to the requirements?
I want my wordpress plugin to redirect to custom page, please refer below page. please refer coding given below
login page after login i want them to move to speific url.
I already try few plugin, all never done the job. I used : https://wordpress.org/plugins/redirect-after-login/.
So please help me to edit some coding in wordpress.
Please refer Login/Register Page Template (template-register.php)
<?php
/* Template Name: Login/Register */
get_header();
/* Site Showcase */
imic_page_banner($pageID = get_the_ID());
/* End Site Showcase */
/* Login/Register Page Design Layout
============================================*/
$pageLayout = get_post_meta(get_the_ID(),'imic_register_layout',true);
$contentClass = 4;
if ($pageLayout != 1) { $contentClass = 6; }?>
<!-- Start Content -->
<div class="main" role="main"><div id="content" class="content full"><div class="container">
<div class="page"><div class="row">
<?php
echo '<div class="col-md-'.$contentClass.' col-sm-'.$contentClass.'">';
/* Page Content
======================*/
while (have_posts()):the_post();
the_content();
endwhile;
echo '</div>';
/* Manage Login Form
========================*/
if ($pageLayout == 1 || $pageLayout == 2) { ?>
<div class="col-md-4 col-sm-4 login-form">
<h3><?php _e('Login','framework'); ?></h3>
<form id="login" action="login" method="post">
<?php
$redirect_login= get_post_meta(get_the_ID(),'imic_login_redirect_options',true);
$redirect_login=!empty($redirect_login)?$redirect_login: home_url();
?>
<input type ="hidden" class ="redirect_login" name ="redirect_login" value ="<?php echo $redirect_login ?>"/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control input1" id="loginname" type="text" name="loginname">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input class="form-control input1" id="password" type="password" name="password">
</div>
<div class="checkbox">
<input type="checkbox" checked="checked" value="true" name="rememberme" id="rememberme" class="checkbox"> <?php _e('Remember Me!','framework'); ?>
<?php echo ''.__('Forget Password','framework').''; ?>
</div>
<input class="submit_button btn btn-primary button2" type="submit" value="<?php _e('Login Now','framework'); ?>" name="submit">
<?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?><p class="status"></p>
</form>
</div>
<?php } ?>
</div></div></div></div></div>
<?php get_footer(); ?>
No need to create form in WordPress for login. WordPress provide functionality for this.
You can use wp_login_form($args) function for login.
Put this code in your template file.
$redirect_url = get_site_url();
$args = array(
'echo' => true,
'redirect' => $redirect_url,
'form_id' => 'loginform',
'label_username' => __('Email Address'),
'label_password' => __('Password'),
'label_remember' => __('Remember Me'),
'label_log_in' => __('Log 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($args);
I hope this will work for yor.
There is a function in wordpress for redirection. Follow this link https://codex.wordpress.org/Function_Reference/wp_redirect.
User this function before get_header() statement.
Hope it will help.