setcookie on WordPress Page Template

I am having an issue with setcookie. I need to set a cookie on a page action (form). I have tried on the template file, but not working. then find out if I set it from functions.php with init hook, it's working. then I put condition like if(is_page_template('mytemplate.php')){... then it's not working. how can I do that? here is my code. add_action( 'init', 'hello_my_cookie' ); function hello_my_cookie() { if(is_page_template('mytemplate.php')){ if(isset($_POST['GGmyCookie'])){ setcookie( 'myCookie','Hello Cookie!', time()+3600); } } }
Category: Web

How can I create network with different domains?

I have heard similar questions posed but not exactly what I am looking for. Is it possible to create a multisite with different domains and sub.doamins? For example, can I create a network that allows me to create build: SiteName1.com is a BuddyPress video community. DomainName2.net is an online shop that sells products. TheDomain3.co is a web design & hosting company that creates and manages other websites. Users must be able to create subsites of their own like (username.TheDomain3.co) Also …
Category: Web

if is_page() is not working with wp_redirect

I have a plugin installed, which is creating user profiles on frontend. The ID of this page is 301 in backend of wordpress. If a profile site is visited, i want to append the url with a parameter. The parameter value is stored in a cookie. The code for appending the URL is working, but when i add the code to only show on this page id its not working. The code is executed in my child themes functions.php NOT …
Category: Web

How to login a user with wp_set_auth_cookie on a specific blog within a multisite environment

How can you leverage wp_set_auth_cookie to programmatically login a user on a specific blog in a multisite environment? wp_set_auth_cookie doesn't allow you to set the target blog to authenticate the user under. I have a multisite/buddypress setup where activations happen on the primary domain. I want to then automatically authenticate that user not for the main site, but for one his site that was created on registration.
Category: Web

Login problems on multisite installation with different domains

I have a subdomain multisite installation in site.com, with subsites such as sub1.site.com, sub2.site.com etc. I needed to transfer one of these sites to an own domain, sub1.com. So I changed the domain value from sub1.site.com to sub1.com on the network admin panel and at first it seemed to be working. However, I was not able to log in to the admin panel on the transferred site at sub1.com/wp-login.php. No error messages, but returned on the same page every time. …
Category: Web

Getting an ERROR: Cookies are blocked error when logging in to a site on a different domain?

I am getting the following error: ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress. When trying to login to a network site on a different domain with version 5.0.2WordPress. I can login to the primary site, but when going to the dashbaord of the secondary site it redirects to the login screen on the different domain and gives the cookie error. Relevant wp-config.php settings: /* Multisite */ define('WP_ALLOW_MULTISITE', true); define('MULTISITE', true); …
Category: Web

Log in user using Wordpress REST API

I am having problems authenticating a user after logging in using a custom endpoint using the WordPress REST API. I'm aware that this is not the perfect approach but I am just trying to make a few custom routes so that I can prototype a mobile app. I have searched countless forums and I have looked at a number of other answers but I can't seem to find out what the problem is in my code. Code to create a …
Category: Web

$_SESSION variables lost during OAuth callback

When I attempt an OAuth handshake, the first step is a GET call to obtain an access code like so. # Redirect to request authorization code $url = $this->get_authorization_url(); header("Location: $url"); exit; Just before I send this I store the current URL in a $_SESSION['last_url'] variable so that after authentication is complete I can redirect it back to the page the user originally attempted. This authorization url uses the client id and secret to obtain an access code which is …
Category: Web

Why sometimes wp_redirect() reset cookie?

I've been trying to set cookie to mark users that get redirected before checking out because of incompleted profile. The problem is when wp_redirect is executing and new page is opened, the set cookie dissapears. Here's my code: add_action('woocommerce_before_checkout_form',function(){ if (is_user_logged_in()){ $user = wp_get_current_user(); if ( bpprocn_has_incomplete_profile($user->id) ) { setcookie('wsk_redirected_from_cart',true,time()+3600); wp_redirect(bp_core_get_user_domain( $user->id ) . bp_get_profile_slug() . '/edit/group/2'); exit; } } }); And you can see how the cookie was set for just a short while and disappears after the new …
Category: Web

Language switcher for subdomains

We are running a wordpress site with Greek content (political & economical analysis) and we want to set up an English site of our site. The English site will be running on its own wordpress installation in a subdomain of the main site. If the primary site is http://www.analyst.gr then English site will http://en.analyst.gr. My problem that I face is that I can not find a proper solution for the language switcher. Ideally I want a language switcher that recognizes …
Category: Web

WordPress load javascript file if something... (after the page is loaded)

I'm having an issue with making a website GDPR ready. It is loading a javascript file for InstraGram that is setting a cookie. I was able to locate where this file is loaded in the plug-in directory but I dont want to edit this file because after that I cant update anymore. So I was able to dequeue the script so it wont be loaded. After that I tried everything to load the file conditionaly after the page is loaded. …
Category: Web

$_SESSION variable not saving on page refresh or new page

So I have a form that allows the user to select a city. Once selected, this city is then saved to a $_SESSION variable as such : $_SESSION['city_selected'] = $_POST['location_select']; This works fine once selected. When I var_dump($_SESSION); I see my variable perfectly on after selecting the city and the page refreshing. Then, if I move to a different page or refresh the same page - the var_dump(); returns NULL I can't seem to figure this out. My PHP session …
Category: Web

Why WordPress not logout after I have close my browser?

The default login cookie has expiry time as "session", so suppose when I close my browser, I need to login again when I visit wp-admin. But I've found that serveral time I've closed my browser (kill the chrome manually), and when I come back I still able to login without login. What would be the reason? Update: Only chrome has this issue, Firefox / Safrai are okay. I tested under Mac, close the browser fist and additional Quit the app …
Category: Web

Allow user to select location and then set cookie for location in WordPress

I am needing to allow a user to select a location from a dropdown on the front-end, that then sets a session cookie for that chosen location... any help here? Ok so this is the outlined task: Location selector: 1st step is to simply have the location option slide down and then save the PHP cookie for later use I am getting the cookie to set ' add_filter( 'query_vars', 'addnew_query_vars', 10, 1 ); function addnew_query_vars($vars) { $vars[] = 'loc'; // …
Category: Web

After logout browser's back button into twenty sixteen theme profile

I have tried numerous ways to prevent the browser's back button from allowing someone from using it to go back into a visitors logged out profile. The codes I used were supposed to prevent the browser from caching data from the last page visited after logout. They don't work. Wordpress logs the visitor out once they click the logged out button, yes this portion wors. Unfortunately, you can see the last page visited by the person who was logged on. …
Category: Web

Triggering a timewall after X days

I'm looking to implement a sort of timewall for a Wordpress website. The process for this would be the next: User accesses the site for first time (non-logged user). After 3 days, user is asked to register in order to see the content. User registers (logged user) and has access to content. After a month user membership ends and becomes non-logged user again. User has 3 days again to see content before being asked to register Is there a way …
Category: Web

Favicon not loading until cookies are accepted?

I'm seeing a weird issue where my site's favicon is not loaded until I accept cookies on a cookie plugin. I wrote the plugin and as far as I know there is no tie of a favicon to cookies and the plugin doesn't explicitly block anything by default. I don't think the favicons are tied to a cookie anyways. Anyone seen this type of behavior before and know how to allow the favicon to load. Plugin Github
Category: Web

How to verify which WordPress user requested the API in ASP .NET Core?

I have a use-case where I have a WordPress site and an ASP .NET Core as a Restful API backend. I use the WordPress site entire for Frontend, CMS, and User Authentication. On certain pages of my WordPress site, I've embedded React apps into it. The React app does some API calling to my ASP .NET Core server. Now, I would like to know what is the best way to verify which signed-in WordPress user requested the API in ASP …
Category: Web

How to resolve error “Cookies are blocked due to unexpected output.”?

I am facing an error as per below at admin panel login. "ERROR: Cookies are blocked due to unexpected output." I've tried several solutions like, Log checking (No logs are showing in log file) Resolve header already sent issue (Not getting this error at all). Check incomplete 'php tag', removing white spaces etc (Have already checked both). We have re-solve above 3 things but still the issue is there. Would anyone please check this and help me on this ?
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.