I need to check if there is a session on every WordPress page. I used add_action Init with the following code:
function check_session() {
$se;
if ( session_status() == PHP_SESSION_NONE ) {
$se = false;
} elseif ( session_status() == PHP_SESSION_ACTIVE ) {
session_start();
if ( isset( $_SESSION['mysession'] ) ) {
$se = true;
}
}
}
add_action( 'init', 'check_session');
Now, I want to show prices or others depending on whether or not that session exists. I tried to set a boolean variable after checking the session and programming the code based on that variable, but I can not read it in the file of my template, neither in the index.php or head.php, I guess it will not be visible. What is the way to read the boolean variable in the files of the wordpress theme, or is there a better way to do this? I have read about global variables but everyone advises against using them.
To see if anyone can take my doubts away, I greet you!
Related
On Wordpress, I'm using an e-comerce plugin which starts a php session on every page, but my caching solution will not cache a page or post once a php session starts.
I've successfully stopped the session from starting on the homepage with:
function no_start_session( $start_session ) {
if ( '/' == $_SERVER['REQUEST_URI'] ) {
$start_session = false;
}
}
add_filter( 'plugin_start_session', 'no_start_session', 10, 1 );
Now I want the same behavior to happen on every Wordpress post on my blog (not pages, just posts) but there's no way to easily target all of these at once via the URL because my URL scheme is www.thesitename.com/the-post-name
I tried adding this to the function:
if ( is_singular() ) {
global $post;
if ( 'post' == $post->post_type ) {
$start_session = false;
}
}
But it does not work. Is there a way to make sure that this PHP session doesn't start on all posts without changing the URL scheme?
I've the following folder structure:
root/wordpress/* // all wordpress related files
root/myApp/test.php
Under test.php, I've the following code
<?php
require('../wordpress/wp-blog-header.php');
$userid = get_current_user_id();
echo $userid; // prints 0 on the page
?>
So to play around and experiment, I went ahead and did the following in root/wordpress/wp-includes/user.php
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) )
return 1;
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 2 );
}
But the output on test.php still prints 0. What am I missing here?
Also
I'm very well aware one should not directly include wp-blog-header.php in an external app and rather should use the recommended approach while passing the user session
One should not edit the core files
I'll undo them as soon as I figure out why the update in the core file (user.php) isn't reflecting the change
FWIW, WP version 4.9.3 is what is being used.
Update
I'm not really looking for a solution where the user session will be passed onto the external app. Rather, I'm trying to figure out why isn't the edit to user.php not taking effect?
require('../wordpress/wp-load.php');
global $current_user;
echo $current_user->ID;
You need to include wp-load.php file, Then you need to use global
$current_user which is for getting the current user data. This is
sufficient to print the current user ID.
Ok. I figured it out. It was very silly of me to miss that.
get_current_user_id() internally calls wp_get_current_user() which has the following line:
$user_id = apply_filters( 'determine_current_user', false );
if ( ! $user_id ) {
wp_set_current_user( 0 ); // <--- this is the culprit which was setting it to 0
return $current_user;
}
You have to call wordpress proccess of user authentication. Try adding the following code before get_current_user_id();, if it doesn't solve your problem it'll at least point you the right direction:
require('../wordpress/wp-blog-header.php');
wp_set_current_user(apply_filters('determine_current_user', false));
$userid = get_current_user_id();
echo $userid;
EDIT 1:
Get the user ID from $_SESSION.
Run print_r($_SESSION); to see what your field is.
I am creating user specific content using ACF User field to determine which user can see what. My code below is working perfectly to show the correct content to the user selected and something different to everyone else -
<?php
$client = get_field('client');
$userID = $client['ID'];
$user_id = get_current_user_id();
if ($user_id == $userID ) {
echo 'YOUR OWN PAGE';
} else {
echo 'NOT YOUR PAGE';
}
?>
However I need to simply add an 'or' statement so any user that is an admin can see all content regardless of who they are, something like below (which doesn't work) -
if ($user_id == $userID ) || is_admin() {
All help is greatly appreciated.
Your brackets are wrong that is all.
if ($user_id == $userID || is_admin() ) {
This assumes the is_admin() function works though :)
While Christian's answer fixes the typo in the question, it doesn't provide a working solution.
The documentation for is_admin() states the following:
This Conditional Tag checks if the Dashboard or the administration panel is attempting to be displayed. It should not be used as a means to verify whether the current user has permission to view the Dashboard or the administration panel (try current_user_can() instead). This is a boolean function that will return true if the URL being accessed is in the admin section, or false for a front-end page.
And further down in the notes section:
is_admin() is not intended to be used for security checks. It will
return true whenever the current URL is for a page on the admin side
of WordPress. It does not check if the user is logged in, nor if the
user even has access to the page being requested. It is a convenience
function for plugins and themes to use for various purposes, but it is
not suitable for validating secured requests.
I do understand the logic that could lead one to think that is_admin() checks if the current user is an administrator. However, checking with the WordPress documentation(and I have to say it's really good for commonly used functions) is always a good idea.
Now that we've got that out of the way, there are two simple options that you can use in order to figure out if the current user has administrator privileges:
1. If you're not going to use WordPress Multisite, or if you will want to only allow Super Administrators to see the content
is_super_admin():
Determine if user is a network (super) admin. Will also check if user is admin if network mode is disabled.
You can pass the ID of a given user to the function, or in your case just do this:
if ( $user_id == $userID || is_super_admin() ) {
2. If you're going to use WordPress Multisite and you will want to allow Super Administrators and site Administrators to see the content
We can create our own function that works very similar to is_super_admin(), except for in a Multisite environment it also checks if the current user is an administrator(in the is_super_admin() function, this is defined by the user having the delete_users capability). Here's the code:
function my_is_user_admin( $user_id = false ) {
if ( ! $user_id || $user_id == get_current_user_id() ) {
$user = wp_get_current_user();
} else {
$user = get_userdata( $user_id );
}
if ( ! $user || ! $user->exists() ) {
return false;
}
if ( is_multisite() ) {
$super_admins = get_super_admins();
if ( ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) ) || $user->has_cap( 'delete_users' ) ) {
return true;
}
} else {
if ( $user->has_cap( 'delete_users' ) ) {
return true;
}
}
return false;
}
There is also an alternative solution, which could be more extendable - you can create your own capability and assign it to the Administrator role. That way if for some reason I want to give someone access to all content, but don't want to make them an Administrator on my site, I can just add that particular capability to that particular user(can't find a free plugin at the moment, but there are ways to do it).
This question has decent answers on how to add a custom capability - https://wordpress.stackexchange.com/questions/35165/how-do-i-create-a-custom-role-capability - should you choose to go that way.
I would like to apply my own logic in WordPress that'll determine if the current page is the front page or not and then I want is_front_page() to return that result. Is there anyway to hook into that function or use the 'pre_get_posts' action to change the necessary values so that is_front_page() is affected? is_front_page() needs to use my custom logic because I'm using other 3rd party plugins that uses that function and I don't want to modify those plugins so that when I update them in the future, my site won't break. Has someone needed to do this before and/or could show me what variables of the WP_Query object I need to change? Thanks!
There is no way to "hook" into is_front_page(), you're going to have to create your own function and use that instead.
The function itself is very simple, I've included it for you below - use this as a starting point for something new and put it in your functions.php file.
public function is_front_page() {
// most likely case
if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
return true;
elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
return true;
else
return false;
}
I am trying to run a Javascript file via conditions based on the request, (A.K.A. the previous URL) that the current page was accessed from. For example, if the user was redirected to the login page, logged in, and then returned to the previous page, that would trigger the JavaScript (whereas simply visiting the page would not).
Does WordPress or PHP have a way to check the request source?
Update:
WOOOOAAAH, WE'RE HALFWAY THEEERE!
Based on user1091949's suggestion, I have gone ahead and added code to run my JavaScript file based on the presence of a cookie:
if ( !isset( $_COOKIE['mmmcookies'] ) ) {
setcookie( 'mmmcookies', 1, 5 );
}
function media_kit_script() {
$page_id = get_queried_object_id();
if ( isset( $_COOKIE['mmmcookies'] ) ) {
if ($page_id === 1947) {
wp_enqueue_script( 'media-kit-init' );
}
}
}
add_action( 'wp_enqueue_scripts', 'media_kit_script' );
The script is registered in a previous function, and everything works! Now, here's my next question: I need to implement setcookie() based on the fact that the user has come from logging in, i.e.:
if ( user_came_from_login() ) {
if ( !isset( $_COOKIE['mmmcookies'] ) ) {
setcookie( 'mmmcookies', 1, 5 );
}
And so on. How would I do this?
You could use a cookie. The start page:
if (!isset($_COOKIE['iwashere'])) {
setcookie('iwashere', 1, time()+315360000);
}
The page where Javascript is needed if the user came from the page above:
if (isset($_COOKIE['iwashere'])) {
echo '<script src="someJavascript.js"></script>';
}
Of course, you may need to change the expire time for the cookie.
That would be the referer (and it's not very reliable):
$_SERVER['HTTP_REFERER'];
Wordpress has it's own version:
wp_get_referer()
that accesses $_REQUEST['_wp_http_referer']) internally