Wordpress - how to use authentication for other code? - wordpress

I want to setup WordPress and use it's authentication for outside scripts.
For example, I'll have a script here:
http://www.domain.com/simplescript
Instead of adding custom user and password login, I thought it would be great simply to add a line at the top of the code, something like a is_user_logged_in check.
I read somewhere this can be done, the only extra being to add require_once($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php'); at the top...
BUT... the page says the code has to be in the theme folder - which I would prefer not to have this restriction. I want to have my code anywhere on the website.
My code isn't all that complex - but does need other files + may or may not need folders and sub folders
Any ideas on how I can do what I need?

You only need to require 'wp-load.php' in your seperate PHP file. wp-load.php is in the root of your WordPress installation. You should modify the path if the script is somewhere else. Like this,
<?php
require_once 'wp-load.php';
if( is_user_logged_in() ) {
// true
} else {
// false
}
?>

Related

wp_enqueue_script is not Working for Editor role

I have this code
add_action('admin_enqueue_scripts', 'my_admin_scripts_method');
function my_admin_scripts_method() {
wp_enqueue_script('admin-control2', plugin_dir_url( __FILE__ ) .'js/admin_control.js',array(), PLUGIN_VERSION, true);
}
When I'm logged in /wp-admin with an user that is administrator the js file is enqueued. However if I switch to an user that is editor role the js file is not included.
How can i fix this ? Is this the default behavior ?
If you want my 2 cents i would say that your problem reside outside that piece of code. Be 100% sure that your code is getting executed no matter what the user lvl is. For example, move it in the main plugin file outside any class scope and see if it work

wp_redirect not working in custom plugin

I am trying to create custom plugin in wordpress.
We want to create a case like if user is not logged in to the system then user should be redirected login page. I tried wp_redirect and wp_safe_redirect but it is not working. here is my code.
if (isset($_SESSION['game_login'])) {
//Do Something
}else{
wp_redirect('login');
exit():
}
I am getting this warning
Cannot modify header information - headers already sent by (output started at wp-includes/class.wp-styles.php:225) in wp-includes/pluggable.php on line 1216
can someone suggest me in this scenario?
You shouldn't just start output buffers wherever unless you're specifically delaying the final output, such as modifying content on the template_redirect, using add_shortcode, or any numerous scenarios where you intend to buffer the output.
The code in your example should be encapsulated in a function and hooked to one of WordPress' many Action Hooks. Typically this kind of function is added on the plugins_loaded or init hooks.
add_action( 'init', 'redirect_anonymous_users' );
function redirect_anonymous_users(){
if( isset( $_SESSION['game_login'] ) ){
// Do Something
} else {
wp_redirect('login');
exit();
}
}
There may be several reasons causing this issue.
Try this points and hope this may have a fix
Remove blank space from files that are showing error after php ?> tag at end of file, But in your case
it is from core file, So don't modify anything in terms of code just try to remove blank space at the
ending of those files. Also remove blank space from bottom of files
wp-config.php and functions.php
If the above point does not work add this code to your wp-config.php file
ob_start();
error_reporting(0);

Using external file with WP_USE_THEMES set to false causing 404

I know there's a dedicated WordPress StackExchange, however, I'm getting no traction over there.
I have a file in my WordPress template I want to use for a Twitter authentication callback named oauth.php that I'd like to be accessible through htp://mydomain.com/oauth.php:
<?php
define('WP_USE_THEMES', false);
echo "test";
However the file is throwing up a 404??
This actually a issue I'd like a solution for away from just this instance.
EDIT
Using the full file path works as intended, so:
http://example.com/wp-content/themes/mytheme/oauth.php
Is this bad practise?
You could try something like this in your oauth.php:
<?php
//Instead of setting to false, try comment it out
//define('WP_USE_THEMES', true);
/** Load the WordPress Environment */
require('./wp-blog-header.php');
// Fake a 200 OK status header
status_header(200);
//The rest of your code here
?>
This should allow you to access this file at http://example.com/oauth.php and run the code inside leveraging Wordpress functionality but without your template.
NOTE: This may depend on your wordpress version.
Hope it helps!

Warning: Cannot modify header information when using require or require_once

I'm WordPress newbie.
I'm creating a plugin that redirect to custom login page each unregistered user access a website, let say the custom login page : custom_login.php.
I am able to create a code to redirect it but it seems no wordpress functions work in custom_login.php. So, I think I have to load something through the file. I guess wp-load.php.
Then I add some codes below at the top of the page :
<?php
require( 'd:\xampp\htdocs\wordpress\wp-load.php' );
?>
But then I got this error :
Warning: Cannot modify header information.....
I changed to require_once but still get similar error.
Some solutions of this forum threads advice to delete any whitespace. Frankly, I don't know what does it mean but I tried to delete all whitespace anyway so that the code become :
<?php require('d:\xampp\htdocs\wordpress\wp-load.php');?>
But it does not solve anything. The error is still exist.
Please help me, the expert ones.
Thanks in advance
Try inserting a system path relative to localhost, like this:
require( '/wp-load.php' ); // or just
require( 'wp-load.php' ); //
All depends on the location you are trying to include wp-load.php from.
On the other hand, you don't have to include wp-load.php if you place the file custom_login.php. in the stylesheet directory as a template or as a custom page. The way to do it is:
.1 Rename the file to page-custom-login.php
.2 Move the file to the stylesheet directory (The theme directory)
.3 Go to admin and create a new page with the title "custom login"
That's all. Now WP will treat that file as a single custom page.
They are correct - you have a space before the opening php tag in one of your files. It can be a bit tricky to find, but look hard.
If you can't find it, try looking for ob_clean() php function to help.

PHP echo function in BuddyPress to Hyperlink to Member Profile Pages

When I first setup BuddyPress on my site, root profiles were enabled. Basically, if a registered user wanted to edit the account settings of their profile, the URL where they could do that would be: "domain.com/username". However, if you typed in "domain.com/members/username" in your browser, you would get the exact same page.
I decided I did not want root profiles enabled because I preferred the URL to be: "domain.com/members/username", so based on this guide (http://codex.buddypress.org/extending-buddypress/changing-internal-configuration-settings), I edited the code in functions.php to be like this:
define ( 'BP_ENABLE_ROOT_PROFILES', false );
However, in header.php, where my navigation is located, the PHP function is still linking to the root profile that I wanted disabled. This is what it looks like:
Profile
I am trying to figure out how I can change the function so that it links to: "domain.com/members/username" instead of "domain.com/username".
Please remove the define ( 'BP_ENABLE_ROOT_PROFILES', false ); in your function.php file.
Because the Buddypress default provide domain.com/members/username.
And also please check the wp-config.php file if above code is there just remove it
Please try this

Resources