Hey ya'll I have this site here http://www.taranmarlowjewelry.com/ and in the top right corner there is a shopping cart, but it does not show up when you first visit the site, but when you refresh the page it shows up.
I have been looking into this and I think it has to do with this cookie PHPSESSID because when I delete that cookie the shopping cart goes away again.
I have also been told this is a PHP configurations issues.
I tried making some adjustments to my php5.ini file like so...
session.use_only_cookies = 1
session.use_trans_sid = 0
session.url_rewriter.tags = ""
but that didnt work....I got it from here http://wordpress.org/support/topic/frontpage-slideshow-and-phpsessid-in-general-fix?replies=1
Does anyone know which I need to adjust in my php configuration?
I tried to set a cookie in wp-content/themes/twentyeleven/header.php right after the tag like this...
<?php
setcookie("PHPSESSID","6a52e6037be6342014834c475b6c0637","/","http://www.taranmarlowjewelry.com/")
?>
and then I get this error...
Warning: setcookie() expects parameter 3 to be long, string given in /home/content/19/9468119/html/wp-content/themes/twentyeleven/header.php on line 54
and the shopping cart does not show up when you first visit my site but when you refresh it will still show up.
I even manually put in the cookie in my theme's functions.php file at the bottom like so...
function set_new_cookie() {
setcookie("PHPSESSID", '6a52e6037be6342014834c475b6c0637', time()+3600); /* expire in 1 hour */
}
add_action( 'init', 'set_new_cookie');
it sets it, it doesn't show any errors, but it does not solve my problem.
I also tried this in my theme's functions.php file...
function set_new_cookie() {
set_cookie('PHPSESSID', '6a52e6037be6342014834c475b6c0637');
$_COOKIE['PHPSESSID'] = '6a52e6037be6342014834c475b6c0637';
}
add_action( 'init', 'set_new_cookie');
and got this error
Fatal error: Call to undefined function set_cookie() in /home/content/19/9468119/html/wp-content/themes/twentyeleven/functions.php on line 5
here is the shopping cart form...
<form class="product_search" style="font-size:0px !important;" method="GET" action="<?php echo $pp_url?>/" >
<input name="product_search" id="wpsc_search_autocomplete" class="wpsc_product_search wpsc_live_search_embed .wpsc_live_search" autocomplete="off" style="padding:0px !important; height:25px !important; vertical-align:top;" />
<script type='text/javascript' > /* <![CDATA[ */
jQuery('#wpsc_search_autocomplete').keypress( function(e){
if ( e.keyCode == 13 ) {
var url = '<?php echo $pp_url ?>'+'?product_search='+jQuery(this).val();
url = encodeURI(url);
jQuery(window.location).attr('href', url);
}
});
/* ]]> */
</script>
<input type="submit" id="button" name="button" class="searchBtn" value="GO"/>
</form>
I fixed my issue with the shopping cart by going into wp-ecommerce/wpsc-includes/shopping_cart_functions.php and adjusting this line
if ( isset( $cart ) ) {
echo wpsc_shopping_basket_internals( $cart, false, true );
}
to this
//if ( isset( $cart ) ) {
echo wpsc_shopping_basket_internals( $cart, false, true );
//}
The problem is not in the cookie being set. Your session_start() will set that. However you need to understand how cookies (including the session cookie) work. The cookies are sent from the server to the browser in the response header. So the user will not have the cookie set in their browser until they start getting your response source. On their next request to the site, their browser will send the cookie with the request.
So the problem is that is you are trying to rely on something in the $_COOKIE superglobal on the first page load, it will not be there (unless the user already had it set from a previous visit). If you need to work with the $_COOKIE in the initial page load you need to do something like this
setcookie('cookiename', 'cookie value');
$_COOKIE['cookiename'] = 'cookie value';
Related
I am using a small code snipet to redirect non logged user from the shop page to the main page.
function my_redirect() {
//if you have the page id of landing. I would tell you to use if( is_page('page id here') instead
//Don't redirect if user is logged in or user is trying to sign up or sign in
if( !is_user_logged_in() && is_page('shop')){
echo 'Non logged user - You are redirected to the main page';
exit( wp_redirect( get_permalink(2604) ) );
}
}
add_action( 'template_redirect', 'my_redirect' );
This is working fine.
However I would like to prompt a message saying "You must be log to access the shop".
I don't know how to do it, my echo in the code do not display anything.
Any idea ?
Thx
You will never see the output of the echo because this action is triggered just before wordpress decides which template to load, so it's an echo in the middle of nowhere (and why the wp_redirect is inside and exit function?)
I am offering customers a repurchase on the order confirmed page. If they click the link they are sended directly to the checkout page. However, the form is then prefilled with the previous data. I would like to clear it, because the idea is one product(ticket) per person.
So far i have the following (and in the page source i can see the script is loaded):
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)){
echo '<script>
window.addEventListener("load", function(){myFunction()};
function myFunction() {
document.getElementsByClassName("woocommerce-checkout").reset();
}
</script>';
}
}
add_action('wp_print_scripts', 'print_script');
I tried other triggers too, such as window.load but no result yet.
Anyone?
You should put the script in the footer using add_action( 'wp_footer', 'print_script' ); to make sure the form gets cleared after the data was loaded into it.
Wordpress has jQuery on default, so I'm using it to look for all the forms in the document and clear them. The function is automatically called on page load, therefore no need for a event listener.
<?php
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)) { ?>
<script type="text/javascript">
( function( $ ) {
$('form').each(function() { this.reset() });
}( jQuery ) );
</script>
<?php }
}
add_action( 'wp_footer', 'print_script' ); ?>
Hi I'm trying to get into Wordpress Ajax Requests right now. Now I have finished a script that works perfectly. But since I'm not a security professional, I'd like to ask you if you can help me.
I would also like to know if I have to use a form every time or if I can start a request directly via jQuery Ajax. If so, how do I do that with dynamic requests where I can not set the nonce before? e.g. in a list where each entry has multiple buttons?
Many Thanks!
template-form.php
<form method="post" id="form">
<input type="text" name="form_name" />
<?php wp_nonce_field( 'form_action', 'form_nonce' ); ?>
<input type="submit" id="submit" value="save">
</form>
script.js
jQuery("#submit").click(function (e) {
e.preventDefault();
jQuery.post(ajaxurl + "/ajax.php", jQuery("#form").serialize()).done(function (data) {
alert(data);
});
return false;
});
ajax.php
<?php
define( 'WP_USE_THEMES', false );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
if( isset( $_POST['form_nonce'] ) && wp_verify_nonce( $_POST['form_nonce'], 'form_action') ) {
$form_name = sanitize_text_field($_POST['form_name']);
echo "is working";
}else{
echo "not working";
wp_die();
}
?>
I would also like to know if I have to use a form every time or if I
can start a request directly via jQuery Ajax. If so, how do I do that
with dynamic requests where I can not set the nonce before? e.g. in a
list where each entry has multiple buttons?
No you could also use wp_nonce_url to make ajax calls.
But as far as I know, I will not work if you have a multisite installation and send an ajax call from one blog of the network to another blog.
Apart from that, you code looks ok to me and you are also saying that it is working.
Therefore I am confused where exactly you need help.
Could you clarify your question, please?
I want to show a success message after redirect, on the new page. When I insert my record in a table, my page will be redirected to another page, and on that page I want to show a success message.
I am using the following code:
if($insert){
<script>
var url='http://www.testing.com';
window.location =url;
</script>
<div id="usp-error-message">successfully register</div>
}
Assuming $insert is returning the correct value you can pass a value through the querystring to the other pages like this:
if($insert){
wp_redirect( 'http://www.testing.com?success=yes' );
exit;
}
and then on that page use the following code to display a message somewhere. Exactly where depends on that page:
if( "yes" == $_GET['success'] ){
echo "<div id='usp-error-message'>successfully register</div>";
}
i've been looking the whole day how to setcookies in wordpress. in my way i found out (using the developer toolbar) that the cookie is set but still not working.
i have 2 files the first contains the login form redirecting to another page to set the cookie and return to another page to check if it's working. domain which is tested on is like this : blog.mydomain.com. here's the setcookie file :
<?php
setcookie("user_name","test",time()+3600);
?>
and chcking the cookie like this :
if(isset($_COOKIE["user_name"])){
echo "cookie exists";
}
else{
echo "cookie doesn't exist";
}
i've read many topics about this issue but there was no clear answer.
Thanks in advance
This typically happens when you try to set a cookie after sending output to the browser. To set a cookie in WP, you should use the 'init' hook to set the cookie on init.
function set_username_cookie() {
if (!isset($_COOKIE['user_name'])) {
setcookie("user_name","test",time()+3600);
}
}
add_action( 'init', 'set_username_cookie');
well, my best way to use cookie in wordpress is this,
function set_my_cookie() {
global $post;
$post_id = $post->ID;
$cookie_name = "my_cookie";
$cookie_value = "my_cookie_val";
if (!isset($_COOKIE['my_cookie'])) {
{
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}}add_action( 'wp', 'set_my_cookie');
i used this the function to setcookie in wp hook of wordpress. the main reason of this is that we may need sometime current page or post, that we cannot access on init hook, but we can access in wp hook.
now, in a shortcode or other plugin/theme functions we may just need to check if the cookie exists or not. thats it
Another option is to use PHP's ob_start(); and ob_end_flush();.
You can find documentation on the two functions here
The way I resolved my issues was to call the two functions before and after the opening and closing html tags like this:
<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php /* Wordpress loop and other tempate code here */ ?>
</body>
</html>
<?php ob_end_flush(); ?>
The issue I was running into was calling a global function that used PHP's setcookie(); and because WordPress processes the page progressively, the cookie couldn't be created due to the page's headers already being sent.
PHP's output buffering function forces the headers to be sent before WordPress processes the page.
Hope this helps.