show shortcodes according to url - javascript

I'm new to programming. Let's see if you can help me. I have to create a script to show several different short codes depending on the result of each url. They gave me this code but there's no way
<script>
var pages = window.location.href;
if((url == '/?listing_region=alicante')) {
echo do_shortcode( '[template id="11570"]' );
else {
if((url == '/?listing_region=madrid')) {
} echo do_shortcode( '[template id="11571"]' );
else {
if((url == '/?listing_region=barcelona')) {
} echo do_shortcode( '[template id="11570"]' );
}</script>

Related

Trigger Js function onclick

I am using wordpress and have a function like this, how to redirect to the corresponding url only happens onclick, for now the "en" language page is automatically redirecting after the page load, but the "zh-hant" redirecting after click, anyone can help me to check the code?
Thanks.
add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
if ( in_category('teachers') ) {
if(ICL_LANGUAGE_CODE=='en'){
?>
<script>
window.beforeConfirmedBooking = function() {
window.location.href = "https://aaa.com";
};
</script>
<?php
}
if(ICL_LANGUAGE_CODE=='zh-hant'){
?>
<script>
window.beforeConfirmedBooking = function() {
window.location.href = "https://aaa.com/zh-hant";
};
</script>
<?php
}
}
}
You should be doing all of it in one function itself
add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
if ( in_category('teachers') ) {
$url_endpoint = '';
if(ICL_LANGUAGE_CODE=='en'){
} else if (ICL_LANGUAGE_CODE=='zh-hans') {
$url_endpoint = '/zh-hans';
}else if (ICL_LANGUAGE_CODE=='zh-hant') {
$url_endpoint = '/zh-hant';
}
?>
<script>
window.beforeConfirmedBooking = function() {
window.location.href = "https://aaa.com<?php echo $url_endpoint; ?>";
};
const btn = document.querySelector('.el-button .el-button--primary .redirect-link');
btn.addEventListener('click', beforeConfirmedBooking);
</script>
<?php
}
}
You can also do it just using php and no js at all
add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
if ( in_category('teachers') ) {
$url_endpoint = '';
if(ICL_LANGUAGE_CODE=='en'){
} else if (ICL_LANGUAGE_CODE=='zh-hans') {
$url_endpoint = '/zh-hans';
}else if (ICL_LANGUAGE_CODE=='zh-hant') {
$url_endpoint = '/zh-hant';
}
}
}
// The following will redirect you to where ever you want
header('Location: https://aaa.com' . $url_endpoint);
/* Make sure that code below does not get executed when we redirect. */
One method would be to do the below using a switch statement.
This allows for easy growth and in the event you end up with a LOT of languages easy to just repeat and falls back to the site URL in the event there isn't a match.
add_action( 'wp_head', 'redirect_after_booking' );
function redirect_after_booking() {
if ( in_category('teachers') ) {
switch (CL_LANGUAGE_CODE) {
case 'zh-hant':
wp_redirect( trailingslashit( get_site_url() ) . CL_LANGUAGE_CODE );
exit;
break;
default:
wp_redirect( get_site_url() );
exit;
}
}
}

I have trouble redirecting all traffic not coming from tripadvisor to another website (WordPress)

I try to redirect all traffic that is not coming from tripadvisor to another website using php session and javascript on a wordpress website
I need help to correct my script
here is the non working script I developed using php session:
<?php
if (!isset($_SESSION)) {
session_start();
}
if (!isset($_SESSION['referrer'])) {
$_SESSION['referrer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'n/a';
}
$referer = isset($_SESSION['referrer']) ? $_SESSION['referrer'] : null;
?>
<script type="text/javascript">
var str = "<?php echo $referer ?>";
var oktogo = str.includes("tripadvisor");
if (oktogo== false) {
window.location.href = ‘https://www.mySecondWebsite.com’;
}
</script>
Hello guys I've corrected the script myself thanks for your help
Here is the script for people having the same needs
if (!isset($_SESSION)) {
session_start();
}
if(isset($_SERVER['HTTP_REFERER']) && substr_count($_SERVER['HTTP_REFERER'],"tripadvisor") == 1){
$_SESSION['is_refered_from_tripadvisor'] = true;
}
if(!isset($_SESSION['is_refered_from_tripadvisor']) || $_SESSION['is_refered_from_tripadvisor'] == false){
header("Location: https://www.mySecondWebsite.com");
}

How to prevent those PHP variables from being cached on WordPress?

I'm using this code in functions.php on WordPress to generate an affiliate link based on the visitor location, It's working perfectly but the problem is that if page caching is turned on (W3 Total Cache), The variables get cached so if someone from the UK was the first one to open the page then the second one from Germany opened the page he will get the same link that the first visitor got.
One more thing please, I'm still very new to PHP and javascript so I would appreciate if the answer was simplified enough
add_action( 'woocommerce_before_add_to_cart_button', 'affiliate_link', 10);
function affiliate_link() {
$not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
// IP Geolocation
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
// Get Custom Fields
$de_asin = get_post_meta(get_post()->ID, "wccaf_de_asin", true );
$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );
//////////////////////////////////////////////
if ($country_code=="DE" or $country_code=="DE") {
$amazon_domain = 'https://www.amazon.de';
// $associate_id = 'bonstato-21';
$asin = $de_asin;
}
else if ($country_code=="GB" && $uk_asin!=="") {
$amazon_domain = 'https://www.amazon.co.uk';
// $associate_id = 'bonmedico-21';
$asin = $uk_asin;
}
///////////////////////////////////////////////
if( wp_is_mobile() ) {
// Amazon Link For Mobile
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';
}
</script>
<?php
} else {
// Amazon Link For PC
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';
}
</script>
<?php
}
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="amzGo();"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
Not sure if it will work, but you could try to set some initial values for the variables and a timestamp and after to use an if statement to check if current timestamp is greater than initial one and inside the {} to set the variables.

PHP condition check and AJAX calling issues

got some kind help here recently so I thought I'd ask couple more questions, 2 to be exact.
Problem 1.
This is related to one of my previous questions can be found here: PHP condition check fail
Here's the code:
<?php
$username = (isset($_GET['username']) ? $_GET['username'] : null);
$fileName = "users.txt";
$users = file_get_contents( $fileName );
$objUsers = json_decode( $users );
// iterate through the list and find John
for( $i = 0; $i < count($objUsers); $i++ ){
if($objUsers[$i]->username == $username){
echo '<div>John is found</div>';
} else {
echo '<div>John is NOT found</div>';
break;
}
}
?>
The trouble is now it always and everytime returns "John is found" even if I typed "Charles" into the username field as it iterates through the whole list and will eventually find "John", no matter what. The way I need to change the code is to return "John is found" only when "John" is typed into username field.
I think it is a relatively easy problem for those of you experts out there but my OOP experience is not very helpful with this syntax.
Problem 2.
I would like to trigger an ajax call after a click event has done some stuff.
Code:
$("#div1").click(function(){
// if condition is true
if (...){ // working
// then if this condition is also true
if (...){ // working
var link = "do-some-stuff.php" // working
$.notify("task is done"); // working
$.getJSON( link, function( input ){});
// how to refer to div2 or should i just paste the ajax function here?
// tried this latter one, no success
}
$("#div2").click(function(){
$.ajax({
url: "other-suff.php",
type: "GET",
success: function(data){
$('#div3').html(data);
}
});
}
Thanks for all the suggestions!
Regarding your problem 1:
<?php
$username = (isset($_GET['username']) ? $_GET['username'] : null);
$fileName = "users.txt";
$users = file_get_contents( $fileName );
$objUsers = json_decode( $users );
// iterate through the list and find John
for( $i = 0; $i < count($objUsers); $i++ ){
if($username=='John'){
if($objUsers[$i]->username == $username){
echo '<div>John is found</div>';
} else {
echo '<div>John is NOT found</div>';
break;
}
}else{
echo '<div>You are not looking for John</div>';
}
}
?>
added an if to see if you tiped John -> look for John in the txt. Else alert that you didn't type John in the box

How to get div content that was echoed by PHP

I need to get a value inside a div content. After a button click and doing stuff on the server side, my PHP function does:
echo "0";
or
echo "1";
depending on what my function does. So let's say if it's 0, the AJAX response will be $("div#divResult").html(data); where I put the 0 in the div divResult.
What I am trying to do now is I want to execute a js function to read whether it's 0 or 1 in divResult.
This is how I execute it:
<div id="divResult"><script>getDivResult();</script></div>
And my js function:
function getDivResult()
{
var result = $("div#divResult").text();
if(result === "0")
{
alert("Badge Number already exists, please check again.");
}
else if(result === "1")
{
alert("Your details have been entered!")
ADD_USER_POPUP.close;
}
}
Somehow the getDivResult function is not executing. The 0 and 1 does display on in the div though. Any help on this? I've tried .html too by the way.
EDIT:
Here's the AJAX that I use for the button click and return the response from PHP which is either 1 or 0:
$.post(page, {
name : name,
badge_number : badge_number,
category : category,
priviledge : priviledge,
action : "insert"
}, function(data) {
$("div#divResult").html(data);
});
2nd EDIT:
function insertRow($name, $badge_number, $priviledge, $category)
{
$table_info = "TBL_USER_LOGIN";
$query_string = "select badge_number from $table_info where badge_number = $badge_number";
$result = #mysql_query($query_string) or die (mysql_error());
$checkBadge = mysql_num_rows($result);
if($checkBadge>0)
{
//echo "Badge Number $badge_number already exists. Please check again.";
echo "0";
}
else
{
$query_string = "insert into $table_info(name, badge_number, priviledge, category) values('$name', '$badge_number', '$priviledge', '$category')";
$result = #mysql_query($query_string) or die (mysql_error());
//echo "Your details have been entered! Please click on 'View Users' to display all users.";
echo "1";
}
?>
<?php
$action = rtrim($_REQUEST['action']);
if($action=="delete")
{
$id = rtrim($_REQUEST['id']);
$order = $_REQUEST['order'];
echo deleteRow($id);
echo selectAll($order);
}
elseif($action=="insert")
{
$name = $_REQUEST['name'];
$badge_number = $_REQUEST['badge_number'];
$priviledge = $_REQUEST['priviledge'];
$category = $_REQUEST['category'];
echo insertRow($name, $badge_number, $priviledge, $category);
}
elseif($action=="update")
{
$order = $_REQUEST['order'];
echo selectAll($order);
}
?>
You shouldn't need to append the return data to the page at all. Why don't you run your function immediately after the AJAX request completes, like so:
$.ajax({
success: function(data) {
if(data === "0") {
alert("Badge Number already exists, please check again.");
}
else if(data === "1") {
alert("Your details have been entered!")
ADD_USER_POPUP.close();
}
}
});
place getDivResult() to onclick in which button you click like
< button onclick="getDivResult()">Click me< /button>"
i think it will be work with you.
enclose the echo with a div then trying getting the value by the id.
or
try echoing via json enconde
json_encode
then fetch the value by using AJAX
i think, this script <script>getDivResult();</script> was replaced the content of #divResult by ajax code $("div#divResult").html(data);. Instead of that, place the script inside head section rather than inside #divResult to execute that.
Where is your ajax? How do you do it?
It looks like you're using jQuery. Try reading the documentation
https://api.jquery.com/jquery.get/
You can try something like this:
$.get( "ajax/test.html", function( data ) {
if(data === "0")
{
alert("Badge Number already exists, please check again.");
}
else if(data === "1")
{
alert("Your details have been entered!")
ADD_USER_POPUP.close;
}
});
data should be your 0 or 1
When you do .html(data) all the existing elements wipedoff and replaced by new content:
$("div#divResult").html(data);
I guess you should do this:
$("div#divResult").html(data);
getDivResult(); // call after it. and put the function globally.
Run your function
getDivResult();
after
$("div#divResult").html(data);
in ajax

Categories