I am currently trying to write a couple of pages into my website that are not part of the wordpress site but I would like to be able to use the wordpress users. I have this working using the following code
require_once("../../../wp-load.php");
$current_user = wp_get_current_user();
Now I am able to use the $current_user variable for everything I need. However because I am includeing the wp-load.php file there is a lot of overhead that I really don't need.
My question is how can I get the current wordpress user without including wp-load?
I don't mind having to include a few extra files myself but I really don't need or want the entire wp enviroment to be set-up each time this page is called just so I can get the user.
What are you doing in those pages? I'm asking because, if you want to send some POST via ajax, you should check this.
If you don't use ajax, but you just need some pages where you can acces Wordpress functions, well somehow you must include wp-load because this file is loading Wordpress.
I suggest creating a file called page-custom-name.php in your theme folder, and publish an empty page with the exact title "Custom-name". Now when you'll be visiting www.yoursite/custom-name you will see that page, and you can get the current user info, or access other Wordpress functions.
Later edit:
This idea a partial solution: How about using hooks?
In your functions.php put these lines:
add_action('wp_login', 'aboutuser');
function aboutuser($username, $user)
{
$userObject = $user;
//find a solution to send this data to your url of your application
}
Basically, when users are logging in Wordpress, the aboutuser() function is executed, and it's get 2 params(in Wordpress 3.3; in earlier versions it gets only the username).
Now, when users are logging in we have acces to the wp user object. Maybe there is a way pass this data(a POST request) to your application and store it in a session.
I don't sure if it's possible though.
Related
i have a website in wordpress, now i have created 1 more website in core php which is similar to wordpress website. I have linked wordpress database to normal website.
Now what i want is i want to login in my 2nd website with the help of wordpress user table.
i don;t know what kind of encryption method they have use. that's why i'm not able to login from my 2nd website.
i saw some article which tell that wordpress use md5 in older version, now they use password_hash
i tried both method
but unable to get result.
Can anyone tell me what kind of encryption method they use in latest version.
or how can i make that method in php
This should be possible by loading the wp-load.php file into your custom application and using the Wordpress build-in function
wp_authenticate( $username, $password );
You can find further documentation here:
https://developer.wordpress.org/reference/functions/wp_authenticate/
Update 23-03-2022
In my testing lab I created a file called alt-login.php in de document root that also contains the wp-config.php. In the file I put the following code
<?php
include("wp-load.php");
wp_authenticate("user","password");
To test just load the file and enter a valid set of credentials. You can var_dump the return value of wp_authenticate to verify if the authentication was succesful, if not you will get an error message. There are no other dependencies to load other than wp-load.php, unlike mentioned in comments.
I currently have a MySQL database from my old site that used to have a basic PHP log-in form and it would query a table containing the user information to log them in. Basic stuff. However I've recently moved to WordPress and it appears to be quite overwhelming at first, so I'm struggling to understand how to integrate my old log-in form with a WordPress site.
I have tried looking at this post:
Wordpress login system and I am still confused. I understand the 'loop' is responsible for displaying multiple 'Posts' on a single page. But to integrate my own PHP code or more specifically; a log-in page, would I make a logged-in 'Post' and a not-logged-in 'Post', and perhaps use a template to display one or the other for a single page via a 'Loop' that uses an if statement to check if the user is logged in?
Or is there some easier method that I'm completely missing?
Thanks!
EDIT: I've thought about creating two pages, both using a template to check if the user is logged in at the top, and display relevant content if they are, or redirect to the log-in page if they aren't. Would this work?
The other possible method I could think of is creating two Posts and displaying the relevant one on a single Page via The Loop. Would this be possible? Are there any security concerns?
EDIT2: I'm going to try the Members plugin and see if I can modify the WordPress MySQL tables to allow room for my previous data.
EDIT3: I need to sync my user logins to our external database as it contains flags to check whether my members are able to access our 'support' pages. I'm not sure how this would be viable while relying on the normal WordPress login system and tables.
Keep it simple stupid.
The point of WordPress is to make things easier. So if you want to go with WP, you should adopt the WP way of doing things.
As comments has suggested, import your current user table into WP. Should be simple enough to change the columns around to get a working import directly into the WP users table.
If there is extra information in your current user table, simple create new fields in the User Profile section of WP. There should be plugins for this (although I have not tried any), but it is quite simple to add these fields yourself, just google for it, there are many tutorials.
For a frontend page to be accessed by only logged in users, simple create a page template and check if user is logged in with is_user_logged_in().
For the access part (Support Pages), either create different user roles and check that the currently logged in user is of the correct role. Or you could also add a custom user capability and validate it on your support pages.
Based on the very general details you have given there is no 'easier method' you are missing. We can't help you form the requirements of your application, but we can help with code if you paste examples of the code you are using, what you are trying to achieve, and what is going wrong with it. Pick one thing, try it out, and if it doesn't work and you get stuck then post it here.
I am building a Wordpress plugin that allows admins to edit store hours from the backend of Wordpress. When a user clicks a department for which to edit the hours, a separate view ('hours.php') loads via jQuery
.load()
from this view, the list of hours for that department is supposed to be called up and displayed to the user. The functions that complete these tasks are in a separate class file. The problem is that in order for this class to be called I have to include the wp-load.php file as well as the class file. I do not want to do this as my hours.php file looks like this:
include('../../../../wp-load.php');
include('../class.libhours-database.php');
I know that is bad practice and do not intend on keeping it that way.
I read this article on query_vars and parse_request but I don't think this is exactly what I am looking for as I am not passing a URL at any point.
Remember: this plugin is ONLY accessible by admins and is only done on the backend.
Wordpress always loads wp-load.php, so you don't have to worry about that.
If your PHP script that handles yor Ajax call from the load() needs a separate class to create the HTML to return, then having it include the necessary PHP file that contains the class is perfectly appropriate.
I am trying to create a php website on WordPress for the first time.
When I create a page, it creates a permalink which is of the form http://localhost/?p=123. I don't know if there is a corresponding file.
I've installed insert-php plugin to read php code. It works fine on a static page. But how do I include another php file? I want to include my_utilities which contains all the back-end functions, in login. It has a permalink 'http://localhost/?page_id=45'.
What to do?
include 'http://localhost/?page_id=45' doesn't work.
Pages you define in WordPress's backend are pages mostly setup for your users to view / read. They, by default, have this URL structure of http://localhost/?p=123 (though this can be changed, but that's a whole different lesson).
To include a script file, upload the file to your folder structure where you have your website then refer to it in your include statement as follows:
include('path/to/folder/my_script.php');
EDIT: You may also want to have a look into WordPress Page Templates:
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
For exapmle your wordpress file is at
/var/www/html/wp-content/myphpfiles/test.php
To include the test.php file in your wordpress page you have to following.
[insert_php] include('wp-content/myphpfiles/test.php'); [/insert_php]
Thats it.
This issue has been driving me up a wall.
I've been working on a mobile website for a friend who uses wordpress. I'm attempting to modify a wordpress plugin that "rethemes" each page for mobile users. I want the plugin to do it's normal thing ("retheme" the page) iff the user is on the blog page, otherwise (if the user visits a different page, he will be redirected to a different address (the address of the mobile website).
In order to do this, I have to be able to detect whether I'm on the blog page, or a different page when the functions in the plugin are run.
I tried condtionals like is_home, is_page(id), ect., but none of them work. I also tried parsing the url and attempting to determine the page that way, but I can't.
I'm just getting my feet wet with PHP, so I'm really not sure what to try next.
To restate my question: How can I determine whether I'm on a certain page within a wordpress plugin function?
The plugin, for the record, is "Wordpress PDA & iPhoned" (http://imthi.com/wp-pda/)
Here's the relevant code:
function PDAPlugin() {
if(true) {
header("Location: http://adavidcreation.com/mobile");
exit;
}
...
}
This works, but I want to change the if statement to only be true if it's NOT the blog.
Any help would be greatly appreciated!
Try using this, which should retrieve the page id: $id = get_option('page_for_posts');
If this doesnt work -> https://wordpress.stackexchange.com/