I am trying to modify WordPress search form, so that after user enters the word in the search bar, that word would be translated and accordingly redirected into external link (for example google). I managed to do translation, redirection, but struggle to get search query in first place.
My code:
<form id="myform" name="myform" role="search" method="get" class="search-form" action="" target="_blank" >
<input type="text" id="SearchText" value="" name="SearchText" placeholder="<?php echo esc_attr($search_text) ;?>" >
<button onclick="go()" type="submit" id="searchsubmit" class="btnsearch"></button>
</form>
Then code for translation needs input for search query $XXXXXXXXXXXXX:
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($XXXXXXXXXXXXX) . '&source=xx&target=en';
$str = $responseDecoded['data']['translations'][0]['translatedText'];
Then I have script that would take translated search query $str:
<script type="text/javascript">
function go() {
document.myform.action = "https://www.google.com/";
document.myform.SearchText.value = "<?php echo $str ?>";
...............
}
</script>
Everything works if I put any word instead $XXXXXXXXXXXXX, but the question what here needs to be entered so that it would take original search entry.
Thank you in advance for any help.
Do you need to receive a request passed in a get parameter? You can use jquery $.get https://api.jquery.com/jquery.get/
Related
I wrote my own WordPress Plugin and I am trying to just refresh the page after form submit.
My code looks like this:
<form method="post" action="<?php echo get_admin_url();?>admin-post.php">
<input type="hidden" name="action" value="send_nl">
<input type="text" name="nl_title" placeholder="Title">
<textarea type="text" name="nl_text" placeholder="Message"></textarea>
<input type="submit" name="submit_nl">
</form>
If I submit the form everything works fine. I get an Email with the right data, but I get redirected to a white page: to https://www.myurl.com/wp-admin/admin-post.php! Now I would need a hook or something similar to redirect to my plugin page again.
I searched a lot, but I couldn't find the right solution - god knows why.
Thank you.
After you have send, save your date you should the function wp_safe_redirect() from the WP API to redirect to your page. A source example below.
wp_safe_redirect(
// Sanitize.
esc_url(
// Retrieves the site url for the current site.
site_url( '/wp-admin/admin-post.php' )
)
);
exit();
You see this usage here in this repo in a function to import data from a json string in the database in the full context.
I am developing a WordPress plugin that is inserted onto the page by adding a token to the page content.
So, on the page there is some introductory text with the contents of the plugin below. On postback, I would like to clear the introductory text and just show output from the plugin.
I know I could do this using jQuery by replacing the contents of $(".entry-content").html("plugin output"); but I wanted to ask if there was a WordPress native method of doing this instead.
UPDATE
The following is one of the files from the plugin. It is on the POST (the if condition) that I want to replace the page content, with the output of the function. On the GET (the else condition) I just want to append the output of the function to the content.
<?php
/*
The following code utilizes Heredoc syntax.
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;).
That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.
This is \n on UNIX systems, including Mac OS X.
The closing delimiter must also be followed by a newline.
*/
class WHRFContactUs {
function GenerateContactUsForm() {
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sendgrid = new SendGrid($GLOBALS['MailAPIKey']);
$email = new SendGrid\Email();
$email
->addTo($GLOBALS['MailAPISender'])
->setReplyTo($_POST['Email'])
->setFrom($GLOBALS['MailAPISender'])
->setSubject($_POST['Subject'])
->setHtml($_POST['Message'] . '<br /><hr/>' . $_POST['FullName'] . ' ' . '(' . $_POST['Email'] . ')<br/>' . '<br />')
;
try
{
$sendgrid->send($email);
$html = <<<HTML
Your message has been successfully sent. Thank you for taking the time to provide us your feedback.
<br/><br/>
In the event that your feedback requires a response, a representative will contact you as soon as possible.
HTML;
}
catch(\SendGrid\Exception $ex)
{
echo $ex->getCode();
foreach($ex->getErrors() as $er) {
echo $er;
}
}
}
else
{
$html = <<<HTML
<form method="post" id="ContactUsForm" action="{$_SERVER['REQUEST_URI']}">
<div class="form-group">
<label for="FullName" class="sr-only">Your full name</label>
<input type="text" class="form-control" id="FullName" name="FullName" placeholder="Your full name" data-validation-required="Please enter your full name.">
</div>
<div class="form-group">
<label for="Email" class="sr-only">Your email address</label>
<input type="email" class="form-control" id="Email" name="Email" placeholder="Your email address" data-validation-required="Please enter your email address." data-validation-format="Please enter a valid email address.">
</div>
<div class="form-group">
<label for="Subject" class="sr-only">Subject</label>
<input type="text" class="form-control" id="Subject" name="Subject" placeholder="Subject" data-validation-required="Please enter a subject.">
</div>
<div class="form-group">
<label for="Message" class="sr-only">Message</label>
<textarea class="form-control" id="Message" name="Message" placeholder="Your message..." data-validation-required="Please enter a message." rows="4"></textarea>
</div>
<button type="submit" id="ContactUsFormSubmit" name="ContactUsFormSubmit" class="btn btn-primary">Send message</button>
</form>
<script type="application/javascript" src="{$GLOBALS['WHRFPluginPath']}scripts/whrf-contact-us.js"></script>
HTML;
}
return $html;
}
}
add_shortcode('ContactUsForm', array('WHRFContactUs','GenerateContactUsForm'));
?>
As mentioned in the comments, without knowing how that content is being added it isn't really possible to know how to replace it.
However, there's a possibility of achieving that in a very disruptive and ill-advised way:
Chances are that content is being added by using the filter the_content.
So you could disruptively have a high-priority modification for the content and then remove that filter to stop the other content from being added. As follows:
function my_disruptive_filter($content) {
remove_all_filters('the_content');
return 'my custom content';
}
add_filter( 'the_content', 'my_disruptive_filter', -999, 1);
I'm not 100% sure if a this would work, since I've never tried it.
Also remove_all_filters takes a second parameter that's $priority which is optional. You can target all priorities that are lower that the one using with this hook, via a for loop. But I assume without providing that parameter it would just remove all of them.
Warning
The reason that this is very disruptive is that it would prevent any other code from using that filter. Another developer (or even yourself) might want to use that filter later at some point and it won't work and you have no idea why. Could be a very difficult situation to get out of.
Also this might prevent existing plugin theme from adding their content, so if you wind up using and see missing stuff -- the reason could be this.
Note: this is really a hit-or-miss solution because it depends on how that content is being added.
The function the_content() returns the page content, if you want to overwrite this using your own plugin you should remove this line in whatever page you are (usually page.php/single.php in theme dir) with your custom plugin output.
i create simple plugin wordpress , one validationform.php and rflinsertdb.php
when user click on submit form , i want got rflinsertdb.php the page validation and insert information to db , but wordpress give me Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
this 2 php page in one folder that name in public ,i see to many codes in internet but not help, how can i do that ?
thx alot
i try this codes for action form
<form method="post" action="<?php bloginfo('template_url'); ?>/rflInsertdb.php">
<p id="errorMessage"></p>
<p>name: <input type="text" class="register" name="name" id="name"></p>
<p>family: <input type="text" class="registerForm" id="family" name="family"></p>
<p>numbers :<input type="number" class="registerForm" id="numbers" name="numbers" min="1" max="200" value="1"></p>
<p>tell: <input type="text" class="registerForm" id="tell" name="tell"></p>
<p><input type="submit" value="ثبت" class="registerForm" id="submit" name="submit"></p>
</form>
This happens to you, because you are using template directory for: /rflInsertdb.php
Try to use
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/rflInsertdb.php">
If your file is under the public (what is under the plugin dir), then maybe:
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/public/rflInsertdb.php">
See here: https://codex.wordpress.org/Function_Reference/plugin_dir_url
The following code is in a wordpress page.
<form action="action_page.php" method="post">
Name:<br>
<input type="text" name="name" value="Mickey">
<input type="submit" value="Submit">
</form>
action_page.php
<?php $name=$_POST['name']; echo $name;?>
I would like to diplay the output(Mickey) in a wordpress page, instead of php page. Please help me.
You can achieve it via AJAX, see the codex
Add this to functions.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
function prefix_ajax_add_foobar() {
// Handle request then generate response using WP_Ajax_Response
// In a real world scenario, apply all the validation,
// sanitization and black magic to POSTed variables before using them
$name=$_POST['name'];
echo $name;
}
Now the following could go in footer.php inside of <script> tags or in a separate JS file.
jQuery.post(
ajaxurl,
{
'action': 'add_foobar',
'data': jQuery("form").sanitize()
},
function(response){
alert('The server responded: ' + response);
}
);
All things being equal you should see an alert with the value that was posted.
I have added within my WordPress 3.1 site, the following code at the bottom of my sidebar.php file:
<div id="search_box">
<form id="searchform" action="http://www.service.com/" method="get" role="search">
<input id="s" type="text" name="s" class="search" value="Search site" size="19" maxlength="80" id="white_box" onfocus="if (this.value=='Search site') this.value = ''"/>
<input id="searchsubmit" type="image" class="submit" value="submit" src="<?php bloginfo( 'template_url' ); ?>/images/search_btn.jpg" />
</form>
</div>
As I have coded this search process myself, when I place a some text within my search text box and press the "Search" button, it looks as if, is is calling the page search.php within my theme and displaying the results.
Questions:
1) where/how does it know when I press the "Search" button to go off and call search.php?
2) if possible only, how can I change it to call a different php file instead of the search.php
Thanks.
Use template filter
add_filter( 'template_include', 'template_include', 10 );
and change the template as
function template_include($template)
{
if(your condition here){
$template = get_template_directory().'/your-template.php';
}
return $template;
}
-1. All requests for a WP site go to a single page that routes them based upon specific criteria. Thus when a search is performed it knows that it is a search and directs to the search.php page.
Specifically it goes to index.php that loads wp-blog-header.php.
-2: Everything you need to know should be right here: http://codex.wordpress.org/Creating_a_Search_Page#Using_the_page.php
Wordpress assumes the request is a search if it sees an 's' GET variable.
You can hook into the template_redirect action (in your theme's functions.php file) and load a different template like so:
add_action('template_redirect', 'my_template_select');
function my_template_select() {
if (is_search()) {
load_template(TEMPLATEPATH . '/foobar.php');
exit;
}
}