I am creating form for wordpress website. Currently i am getting value on same page successfully with blank action.
Now i need to get form value on data.php page.
Form
<form id="post-container" action="<?php echo home_url( '/' ); ?>data.php" method="POST">
<input name="aa" value="post_id"> ........ </input>
<input name="ab" value="post_id"> ........ </input>
<input type="submit" name="submit" value="submit">
</form>
data.php
<?php
if (isset ( $_POST['aa'] )){
$ID = $_POST['aa'] ;
echo get_post_meta( $ID, 'brand', true );
}
if (isset ( $_POST['ab'] )){
$ID = $_POST['ab'] ;
echo get_post_meta( $ID, 'brand', true );
}
?>
After submit
url = http://localhost/mobile/data.php which is correct.
but it display index.php which is wrong and also not print any value.
How to submit form to data.php and print value?
Wordpress have protection from POST, GET requests..
I have some links about it
wordpress-jquery-contact-form-without-a-plugin
create-a-submit-form-in-wordpress
add_query_arg
how-to-submit-a-form-in-wordpress
Related
I am using the following to place a search form in a slider revolution hero slide. However it searches the entire site. I would like it to only search woocommerce products. Any ideas what I need to change to make that work?
<form role="search" method="get" id="searchform" class="revtp-searchform" action="http://mywebsiteURL.com/"/>
<input type="text" value="" name="s" id="s" placeholder="What are you looking for?" />
<input type="submit" id="searchsubmit" value="Find" />
</form>
You can solve it using codex: pre_get_posts and developer: pre_get_posts
add_action( 'pre_get_posts', 'search_woocommerce_product' );
function search_woocommerce_product( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', 'product' );
}
}
You can add further condition to this hook function like include in particular page only is_page
Or with certain short code like this
$pattern = get_shortcode_regex();
$patternToken = 'masterslider';
if (preg_match_all('/' . $pattern . '/s', get_the_content(), $matches) &&
array_key_exists(2, $matches) &&
in_array($patternToken, $matches[2])) {
//Your code logic
}
I'm trying to make a simple login form for my WordPress theme. I thought I followed WP documentation correctly, but I'm seeing that my forms action is not being detected.
I can see in my error log 'got here 1' is printing. Looking at process_form.php below, this condition should indicate that my user is not logged in and that action is empty.
I believed that this line from my form processing code:
add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );
and this line from my actual form
<input type="hidden" name="action" value="streamLogForm">
would interact to bind the form action to this admin_post hook.
The showroom_login_user() function works and my GET variables actually seem to be working (at first I thought they were not), but still not sure why I'm reaching this condition.
login.php
<head>
<script type="text/javascript">
function validateLoginForm() {
var email = document.forms["streamLogForm"]["user_email"].value;
var password = document.forms["streamLogForm"]["user_password"].value;
if ( email == null || email == "" ) {
alert("User must be entered");
return false;
} else if ( password == null || password == "" ) {
alert("Password must be entered");
return false;
}
return true;
}
</script>
</head>
<div class="">
<form name="streamLogForm" id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="get" >
<input type="hidden" name="action" value="streamLogForm">
<input type="hidden" name="location" value="<?php echo $URI ?>" />
<input type="hidden" name="action_type" value="<?php echo 'login'?>" />
<div class="field">
<label for="streamUserEmail">Email:</label>
<input type="text" id="streamUserEmail" name="user_email">
</div>
<div class="field">
<label for="streamUserPassword">Password: </label>
<input type="password" id="streamUserEmail" name="user_password">
</div>
<button> Login </button>
</form>
</div>
process_form.php
<?php
if ( ! wp_validate_auth_cookie() ) {
if ( empty( $action ) ) {
error_log('got here 1', 0);
do_action( 'admin_post_nopriv' );
} else {
error_log('got here 2', 0);
do_action( "admin_post_nopriv_{$action}" );
}
} else {
if ( empty( $action ) ){
error_log('got here 3', 0);
do_action( 'admin_post' );
} else {
error_log('got here 4', 0);
do_action( "admin_post_{$action}" );
}
}
function showroom_login_user()
{
error_log("GOT HERE 6!", 0);
$location = $_GET["action_type"];
$action_type = $_GET["action_type"];
$user_email = $_GET["user_email"];
$user_password = $_GET["user_password"];
echo 'testing......' . $user_email;
}
add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );
add_action( 'admin_post_streamLogForm', 'showroom_login_user' );
?>
Update your form tag like this
<form name="streamLogForm" id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo admin_url('admin-ajax.php'); ?>" method="get" >
and place this snippet on your functions.php
function showroom_login_user() {
$creds = array();
$creds['user_login'] = $_GET["user_email"];
$creds['user_password'] = $_GET["user_password"];
$creds['remember'] = true;
$autologin_user = wp_signon( $creds, false );
if ( !is_wp_error($autologin_user) ) {
// "Logged In.!";
} else {
// "Log In Failed.!";
}
// do your redirect here
wp_safe_redirect( "URL of the page to be redirected" );
}
add_action ( 'wp_ajax_quick_streamLogForm', 'showroom_login_user' );
add_action ( 'wp_ajax_nopriv_streamLogForm', 'showroom_login_user' );
I am using wordpress and using PHPMailer class. This successfully sends emails with an attachment as intended. The issue is it is not redirecting back to the page where the form is. The page where the form is a normal wordpress template page.
global $wpdb;
require_once('../../../../wp-load.php');
get_template_part( 'email/PHPMailer-master/class.phpmailer');
$testvar = $_POST['test'];
$bodytext = 'Here is a value in the variable...'.$testvar;
$email = new PHPMailer();
$email->From = 'someone#example.com';
$email->FromName = 'Spider Man';
$email->Subject = 'This is the subject of the message';
$email->Body = $bodytext;
$email->AddAddress( 'xxxxxxxxxx#gmail.com' );
$file_to_attach = get_template_directory().'/images/test.pdf';
$email->AddAttachment( $file_to_attach , 'test.pdf' );
return $email->Send();
$Referer = $_SERVER['HTTP_REFERER'];
header('Location: '. $Referer );
HTML
<form method="post" action="Path/to/email_test.php">
<input type="hidden" name="test" value="<?php echo 'BAZINGA';?>">
<input type="submit" value="SEND EMAIL">
</form>
return $email->Send(); Aren't you returning before it even gets to the $Referer page line?
As part of a larger project, I am attempting all login activity to occur at the following address:
www.url.com/login
So, a link that should lead to a form that would allow a user to reset their password would be found at the following:
www.url.com/login?action=lostpassword
However, the wp-login.php is not reading this correctly, and when I go to the above address, I get the normal log-in form.
This is how I have it set up:
add_action( 'init', 'add_virtual_page_template' );
function add_virtual_page_template()
{
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'login', EP_PERMALINK | EP_PAGES );
add_rewrite_rule( 'login/?', 'index.php?template=login', 'top' );
add_rewrite_rule( 'login/?', 'index.php?template=login', 'top' );
$wp_rewrite->flush_rules();
}
add_action( 'template_redirect', 'add_virtual_page_redirect' );
function add_virtual_page_redirect()
{
global $wp;
$queryvar = get_query_var('template');
if ($queryvar && $queryvar == 'login')
{
include(site_url('wp-login.php'));
exit();
}
if ($queryvar == 'mylogin')
{
include( get_stylesheet_directory() . '/page-login.php' );
exit();
}
}
What am I missing?
Here's how I dealt with this when building out the WordPress for Web Apps toolkit (https://github.com/cferdinandi/web-app-starter-kit):
// LOGIN FORM SHORTCODE
function wpwebapp_login() {
// Get current page URL
$url_current = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url_current .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url_current .= $_SERVER["REQUEST_URI"];
$url_clean = array_shift( explode('?', $url_current) );
$login_failed = $url_clean . '?login=failed';
$signup_success = $url_clean . '?signup=success';
$reset_success = $url_clean . '?password-reset=success';
// Variables
$login_status = '';
// If login failed
if ( $url_current == $login_failed ) {
$login_status = '<div class="alert alert-red">Invalid username or password. Please try again.</div>';
}
// If password reset
if ( $url_current == $signup_success ) {
$login_status = '<div class="alert alert-green"><strong>Success!</strong> We just sent you an email with your password.</div>';
}
// If password reset
if ( $url_current == $reset_success ) {
$login_status = '<div class="alert alert-green">Your password was successfully reset. We just emailed you a new one.</div>';
}
// The login form
$form =
$login_status .
'<form name="login" id="wp_login_form" action="' . get_option('home') . '/wp-login.php" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="log" id="log" value="" tabindex="1" autofocus>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="pwd" id="pwd" value="" tabindex="2">
</div>
<div>
<label>
<input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" checked>
Remember Me
</label>
</div>
<div>
<button type="submit" name="wp-submit" id="wp-submit" tabindex="100" class="btn btn-blue">Log In</button><br>
Forgot your password?
<input type="hidden" name="action" value="login">
<input type="hidden" name="redirect_to" value="' . get_option('home') . '">
<input type="hidden" name="testcookie" value="1">
</div>
</form>';
// Display the form
return $form;
}
add_shortcode( 'loginform', 'wpwebapp_login' );
// FAILED LOGIN REDIRECT
add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
// URL Variables
$referrer = $_SERVER['HTTP_REFERER'];
$url_clean = array_shift( explode('?', $referrer) );
$login_failed = $url_clean . '?login=failed';
// If the post submission is a valid page that's not the backend login screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')) {
// If the password is empty...
if($user->errors['empty_password']){
wp_redirect($login_failed);
}
// If the username is empty...
else if($user->errors['empty_username']){
wp_redirect($login_failed);
}
// If the username is invalid...
else if($user->errors['invalid_username']){
wp_redirect($login_failed);
}
// If the password is incorrect...
else if($user->errors['incorrect_password']){
wp_redirect($login_failed);
}
// Catch all for all other issues
else {
wp_redirect(get_option('home'));
}
exit;
}
// Prevents page from hanging when redirected from backend
if ( !empty($referrer) && ( strstr($referrer,'wp-login') || strstr($referrer,'wp-admin')) ) {
wp_redirect(get_option('home'));
exit;
}
}
// BLOCK BACKEND ACCESS FOR NON-ADMINS
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
// If accessing the admin panel and not an admin
if ( is_admin() && !current_user_can('level_10') ) {
// Redirect to the homepage
wp_redirect( home_url() );
exit;
}
}
With this setup, you block users from ever getting redirected to the backend (the unfortunate side effect: if you're not logged in, you need to login from the front-end first). You can add custom error messages based on the situation, etc. And you can add this to a page just by using the [loginform] shortcode.
The WordPress for Web Apps toolkit has functions like this for password resets, signup forms, and more, so you might want to check it out. I went through this same learning process last year on a project I was working on.
I have created a form using wordpress which returns the title and description. I need to return a few more custom fields. I have searched the internet and found many answers which did not explain it very well and they did not end up working for me. The form would return a post with title, description, the custom fields already filled in. Thanks!
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_Blog_URL' => $URL,
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
add_post_meta($post_id, $meta_key, 'URL' , $unique);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
do_action('wp_insert_post', 'wp_insert_post');
Submit your own!
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="" class="" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="name">
<label>Name of the Article</label>
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</fieldset>
<!-- post Content -->
<fieldset class="content">
<label for="description">Description</label>
<textarea id="description" tabindex="15" name="description"></textarea>
</fieldset>
<button type="submit">Submit</button>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
If I understand you correctly, you want to be able to show Post Meta Fields in your form.
There are a few functions to set, update and retrieve these which will be helpful:
get_post_meta or get_post_custom
add_post_meta
update_post_meta
With those functions, you can create any kind of meta information for posts and display them either in the admin panel or to the end user, depending on the scenario.