I want to put contact form to user profile page via Contact Form 7 and Contact Form 7 Dynamic Text Extension .Form on profile page will send messages directly to user on profile page. I need to put user email to CF7 hidden field. This is only detail. Only code is important.I found a code but it's not working.
Profile Url : domain.com/member/username
I need shortcode which get user email according to username on url.
function func_print_user_email($user_email) {
global $query_string;
if($query_string != 'pagename=profile') {
$patterns = '/(\w+)=(\w+)&(\w+)=/i';
$username = preg_replace($patterns, '', $query_string);
} else {
global $current_user;
$current_user = wp_get_current_user();
$username = $current_user->user_login;
}
return trim($user_email);
return "testshortcode= { trim$user_email[testshortcode] }";
}
add_shortcode('testshortcode', 'func_print_user_email');
THANKS FOR SUPPORT
Related
I am trying to have an email send when a user is added to an ACF user field, I don't want existing users that are in the field already to be notified. Below are a few options I have tried, but am not having luck with. Any assistance would be greatly appreciated.
Option 1:
add_action( 'acf/save_post', 'my_acf_save_hook_email'); function my_acf_save_hook_email( $post_id ) {
// // Get the list of user ids that are currently in the ACF field
$current_pds_project_managers = get_field('pds_project_manager');
// Get the list of user ids that were just added to the ACF field
$added_pds_project_managers = $_POST['pds_project_manager'];
// Get the list of user ids that are newly added, and not already in the ACF field
$newly_added_pds_project_managers = array_diff($added_pds_project_managers, $current_pds_project_managers);
// Send an email to each newly added user
foreach ($newly_added_pds_project_managers as $user) {
$user_email = $user['user_email'];
wp_mail( $user_email, 'You have been added to a project', 'You can now access the project from your dashboard.' );
}
}
Option 2: (this ends up sending 2 emails to the existing users in the field and one email to the new user, I then started with option one to see if I could only get the new users to no avail)
add_action( 'acf/save_post', 'my_acf_save_hook_email', 5); function my_acf_save_hook_email( $post_id ) {
// bail early if no pds_project_manager
if( empty('pds_project_manager') ) {
return; }
// Get the value of the ACF field
$project_managers = get_field('pds_project_manager', '');
// Create an empty array to store the emails
$emails_to_send = array();
// Loop through the project managers
foreach ($project_managers as $project_manager) {
// Get the user data
$user_email = $project_manager['user_email'];
$user_id = $project_manager['ID'];
// Check if the user already exists in the array
if (!in_array($user_id, $project_managers)) {
// Add the user to the emails to send
$emails_to_send[] = $user_email; } }
// Send emails to the users
foreach ($emails_to_send as $email_to_send) {
// Send the email
wp_mail($email_to_send, 'You Have Been Added To A Project', 'You have been added to a project. Please check the project page for more information.'); } }
Hi I'm new to php and code igniter. what I tried is to get login info from view and validated user and need to send a message to user whether login details are incorrect.
Controller Code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
//$this->load->helper('url');
$this->load->view('login/login');
}
public function login_check()
{
//$this->load->view('hello');
//echo "directed";
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
//echo $user_id.' and'.$userPassword;
$var = $this->login_model->check_login($user_id);
$status = 0;
if(empty($var))
{
echo "Invalid user";
$status = 0;
}
else
{
//echo var_dump($var);
$username = $var->username;
//echo $username;
$status = 1;
}
$this->load->helper('url');
redirect('login/login');
//$this->load_>view('login\login');// at this point it does not redirect to login page and instead of that displaying error 404.page not found.
//echo $status;
}
}
Same url ,earlier loaded when it called from from index function but fi it is calling from login check()function does not directed to the view and displaying error 404 page not found.
Any assistance regarding this world be a great help.
Thanks a Lot!
You need to write redirect('welcome'); instead of redirect('login/login');. Because login/login is the view page and you are trying to redirect direct on view without using controller. So you have two option which I had written bellow.
redirect('welcome');
$this->load->view('login/login');
My suggestion is please choose 1st option because 2nd option already you have implemented in first one.
I hope this one will work on you.
public function login_check()
{
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
$var = $this->login_model->check_login($user_id, $userPassword); //pass username and password to your model to authenticate if input exists.
$data['error'] = '';
if(!empty($var)) //check if var is not empty
{
$this->session->set_userdata($var); //set user_data to var
redirect('Account/page'); //redirect it to your account or success page
}else{
$data['error'] = 'Invalid Username or Password.';
}
$this->load->view('login/login',$data); //pass the error notification to your page.
}
I gotta validate the "username" & "password".
This is my table with the users:
And here the users:
The login system must "clasify" those users who are ADMIN users (tipo = 0) and simple users (tipo = 1). Note: ADMIN and simple users have different screens after loging
I gotta validate that the guy who access is an active member (i got problems with it)
ESTATUS = 1 IS AN ACTIVE USER
ESTATUS = 0 IS NOT AN ACTIVE USER
And i do not know how should i validate if a guy did not write anything and he only click on the "login" button (i should not connect to the server to validate it)
Here is my code, it works properly.. It validates if the user is an ADMIN or a simple user and if the information written in the login system corresponds to a person in the database.
But i do not know how should i do the "ESTATUS" validation and how i should check blank fields without connecting to the server :/
<?php
Class Login extends CI_Controller{
public function index(){
$this->load->view('login_form');
}
public function do_login()
{
if(isset($_POST['login'])){
$user=$this->input->post('usuario', true);
$pass=$this->input->post('contrasena', true);
$cek = $this->m_login->proceso_login($user,$pass);
$hasil=count($cek);
if($hasil > 0){
$pelogin =$this->db->get_where('usuarios',array('usuario' => $user, 'contrasena' => $pass))->row();
if($pelogin ->tipo == 0){
redirect('login/admin');
}
else{
redirect('login/usuario');
}
}
redirect('login/index');
}
}
You need to do some validation before inserting your data into the database:
public function do_login()
{
// load the form_validation library
$this->load->library('form_validation');
$this->form_validation->set_rules('Your_username_field_name', 'Username', 'trim|required|min_length[3]|alpha_numeric');
$this->form_validation->set_rules('Your_password_field_name', 'Password', 'trim|required|min_length[6]');
// if there is errors
if ($this->form_validation->run() == FALSE) {
// this will load your form with the errors
$this->load->view('Login_main');
} else {
// if no errors we will hit the database
$user=$this->input->post('usuario', true);
$pass=$this->input->post('contrasena', true);
$cek = $this->m_login->proceso_login($user,$pass);
$hasil=count($cek);
if($hasil > 0){
$pelogin =$this->db->get_where('usuarios',array('usuario' => $user, 'contrasena' => $pass))->row();
if($pelogin ->tipo == 0){
redirect('login/admin');
}
else{
redirect('login/usuario');
}
}
redirect('login/index');
}
}
then in your Login_main view you could add this line of code to echo the errors to see what's going wrong:
<?php echo validation_errors(); ?>
One other question – in the user controller login method, after checking pressed button (login or forget password), in line : $data = $this->user_model->login(); return correct user information; "user name, family, user id" to be used in dashboard for using in log table and heAder of pages how to send this data to dashboard?
i tried to send it via redirect() but it fail
if ($this->form_validation->run() == TRUE) {
if (!empty($_POST['login'])) {
$data = $this->user_model->login();
if ($data != NULL)
{
redirect('dashboard');
} else {
redirect('user/login', 'refresh');
}
} elseif (!empty($_POST['forget'])) {
redirect('user/recover');
}
}
$this->data['subview'] = 'users/login';
$this->load->view('users/_layout_modal', $this->data);
Function redirect() doesn't save anything. You can try save $data to session by writing $this->session->set_userdata('some_name', $data);
Then you can call them with $this->session->userdata('some_name');
Note that $data can be object or array.
Assume that $data is array, then in dashboard you can use something like this:
$data = $this->session->userdata('some_name');
echo 'Username: '.$data['username'].'<br />';
echo 'Family: '.$data['family'];
...... so on ........
As per your question, you are validating the user login details and creating session for them.
Then store user information in session.
set session library auto-load in config/autoload.php. Then create session as below.
$this->session->set_userdata(
array(
"username"=>$username,
"user_id" => $user_id,
"family" => $family
)
);
In Dashboard:
You can access session with it's name:
echo $this->session->userdata("username");
I'm not a pro, but know my way around PHP, I'm new to Codeigniter.
Been going through these tutorials: http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-5-crud/
OK, so I have a page that lists users, clicking on users name will go to an edit page, the url of that page being: index.php/users/edit/1 (where 1 is the users id)
On edit page is a form, this form contains a few parts, each part is populated from different tables in the DB. So my Controller for edit is as follows:
function edit() {
//load model
$this->load->model('users_model');
//assign user data from DB
$data['data_user'] = $this->users_model->getUser($this->uri->segment(3));
//get users Password, using username from above
$data['data_user_password']= $this->users_model->getUserPassword($data['data_user'][0]->UserName);
$data['page_content'] = 'pages/users_edit';
$this->load->view('template/template', $data);
}
Notice:
$data['data_user'] contains users data like name, username, email
$data['data_user_password'] contains users password from a different table
I can then populate the form, on users_edit.php, this all works fine.
I'm accessing this data by doing the following:
if (is_array($data_user)) {
foreach($data_user as $user)
{
$userID = $user->id;
$userName = $user->Name;
$userUserName = $user->UserName;
$userMail = $user->Mail;
$userDepartment = $user->Department;
$userWorkPhone = $user->WorkPhone;
$userHomePhone = $user->HomePhone;
$userMobile = $user->Mobile;
}
}
//user password
if (is_array($data_user_password)) {
foreach($data_user_password as $user)
{
$userPassword = $user->value;
}
}
Name:
<?php echo form_input('name', set_value('name', $userName), 'id="name" class="inputLong"'); ?>
When I post, I'm sending data to: index.php/users/update
My controller for this page so far is:
function update() {
echo '<pre>';
print_r($_POST);
echo '</pre>';
//exit();
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pages/users_edit');
}
else
{
$this->index();
}
}
For now, I'm just testing validation on users "name" where form input=name id=name
I think I'm not handling the if ($this->form_validation->run() == FALSE) part of it correctly, if the form contains data, it passes and redirects to index, if I leave name blank it either not handling the edit page correctly, or I dont know, something isnt right.. I think its because the page is being reloaded using the post array, and not passing the $data like I did in function edit().
Back to the form page, where it should be showing the validation_errors, its showing:
The Name field is required.
This is correct, however, for the rest of the fields that should be pre-populated, its showing PHP error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: userUserName
Filename: pages/users_edit.php
Line Number: 50
You could do your validation inside your edit function instead of having an update function, that way, your data is still available for your view and if the validation has errors, codeigniter will take in charge to repopulate your fields. If the validation is ok, you do your next step
function edit() {
//load model
$this->load->model('users_model');
//assign user data from DB
$data['data_user'] = $this->users_model->getUser($this->uri->segment(3));
//get users Password, using username from above
$data['data_user_password']= $this->users_model->getUserPassword($data['data_user'][0]->UserName);
$data['page_content'] = 'pages/users_edit';
$this->load->view('template/template', $data);
//is the form submitted
if(form submit){
if ($this->form_validation->run() == TRUE)
{
$this->index();
}
else
{
$this->load->view('pages/users_edit', $data);
}
}
}
$this->load->view('pages/users_edit');
Inside your function update(), after your validation you load view but you don't PASS any data variables to it. So you don't have any variables which you can access at your view file..
You have to set your variables the same way as in your function edit():
$this->load->view('template/template', $data);
Currently there is not set variable $data_user so you can't loop it and use it..