How to get the user ID in Wordpress - php

I have Wordpress installed in my website and I want to upload a single .php file that has to retrieve the user ID which is now logged.
This is my code of the id.php file (in the root of the website), which works:
<?php
require( dirname( __FILE__ ) . '/wp-load.php' );
$user_ID = get_current_user_id();
echo $user_ID;
?>
But I am afraid to load wp-load.php in this way. Can it cause problems in the normal work of the blog?
How can I improve my script?

If you're concerned about loading wp-load.php, instead load wp-includes/wp-user.php and use the function (shown below) defined on the WordPress developer site.
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
return 0;
}
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
courtesy of WordPress

Related

I want to disable all WordPress plugins except for ones I pass in an array with a file placed in mu-plugins folder

I use the following code to turn off all plugins without losing auth-keys or settings:
<?php add_filter('pre_option_active_plugins', '__return_empty_array'); add_filter('pre_site_option_active_sitewide_plugins', '__return_empty_array');
But now I want to do the same but de-activate all plugins not returned in my array, but first I need to check if they are even installed on the WP site first so as to not cause problems. I thought the following would work but I get a 502 error:
add_filter('pre_option_active_plugins', 'atlas_default_plugins');
add_filter('pre_site_option_active_sitewide_plugins', 'atlas_default_plugins');
function atlas_default_plugins() {
$default_plugins = array();
$plugins_to_check = array(
"example-plugin1",
"example-plugin2",
"example-plugin3",
);
$active_plugins = get_option( 'active_plugins' );
foreach( $plugins_to_check as $plugin ) {
if( in_array( $plugin, $active_plugins ) ) {
$default_plugins[] = $plugin;
}
}
return $default_plugins;
}
I thought it might be because the line $active_plugins = get_option( 'active_plugins' ); is being called too soon but I am not sure. I need it so that when the file is deleted from the folder, the plugins are active as before. Thanks
I initially thought it was just taking too long so I disable Long-Process Killer in WP but that did not solve it. I also switched PHP versions being used on WP but that also did not work.
It works if I do not the following lines of code:
$active_plugins = get_option( 'active_plugins' );
foreach( $plugins_to_check as $plugin ) {
if( in_array( $plugin, $active_plugins ) ) {
$default_plugins[] = $plugin;
}
}
But I need that check or else it breaks the site if I run it without checking first.

WordPress is_page_template not working right init action

I have the code above into functions file of my theme. I did this code to logout the user "messenger" when another page that not use the template page_forms is requested.
But, when I access some page that use this template, I'm being redirected to the homepage and the session is closed. Which makes me believe that the is_page_template statement is not working properly.
Does anyone have any tips?
function messenger_session(){
$current = wp_get_current_user();
if( isset($current->user_login) && $current->user_login == 'messenger' && !is_page_template('page-forms.php') ):
$redirect_to = get_home_url();
wp_logout();
wp_safe_redirect( $redirect_to );
exit;
else:
return;
endif;
}
add_action('init','messenger_session');
UPDATE
After research, I solve the problem changing 'init' from add_action to 'template_redirect'. This change make the user messenger stay logged in on page-forms.php.
But, the second step is make logout of this user if any other template is required. So, to make this happen, I make some changes in my code:
function messenger_login(){
$current = wp_get_current_user();
if( is_page_template('page-forms.php') && !isset( $current->user_login ) ) :
$username = 'messenger';
$user = get_user_by( 'login', $username );
wp_clear_auth_cookie();
wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID );
return;
elseif( !is_page_template('page-forms.php') && isset( $current->user_login ) && $current->user_login == 'messenger' ):
$redirect_to = get_home_url();
wp_clear_auth_cookie();
wp_safe_redirect( $redirect_to );
die;
else:
// do nothing
endif;
}
add_action('template_redirect','messenger_login');
And the problem now is that after messenger login on page-forms.php, the logout happens automatically withou respect the second statement of the function where the template needs not be page-forms.php to fire the logout (without redirect to home -- is crazy!).
When I comment the function wp_clear_auth_cookie, the problem stop. But the function lose your porpouse.
That function is_page_template is working correctly just make sure you're using correct path of your page_form template if that template is inside any folder like folder/page-form.php then you must need to use that full path after your theme folder suppose if your template is inside templates folder then you must need to pass templates/page-forms.php within is_page_template function
Let me know if this helps you

How to redirect after login to BuddyPress profile in WordPress

I run WordPress 5.1.1 with BuddyPress plugin.
Basically what I want to do is, redirect the user from custom login link to their profile page in BuddyPress.
I have read and check almost all codes provided on Stackoverflow for my similar query but non one has worked on my site.
The only code worked it's below but it has one problem for my site settings and setup.
function bp_help_redirect_to_profile(){
global $bp;
if( is_user_logged_in() && is_front_page() ) {
bp_core_redirect( get_option('home') . '/members/' .
bp_core_get_username( bp_loggedin_user_id() ) . '/profile' );
}
}
add_action( 'get_header', 'bp_help_redirect_to_profile',1);
The problem is, when I want to redirect on homepage of the website it's keep redirecting me on the BuddyPress profile. Categories and post sections are loading correctly.
So, what i need is, when the login flow redirect user on their BuddyPress profile page, after when user hit the homepage of the site to load the homepage of the site and not, the BuddyPress.
I hope someone can help and tweak the function in this way.
Thank you
add_filter( 'bp_login_redirect', 'bpdev_redirect_to_profile', 11, 3 );
function bpdev_redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user )
{
if( empty( $redirect_to_calculated ) )
$redirect_to_calculated = admin_url();
//if the user is not site admin,redirect to his/her profile
if( isset( $user->ID) && ! is_super_admin( $user->ID ) )
return bp_core_get_user_domain( $user->ID );
else
return $redirect_to_calculated; /*if site admin or not logged in,do not do
anything much*/
}
Tested Code put in your active theme function file
You can use wp_login action for that. Please check following example. bp_core_get_user_domain() will fetch the profile URL and after successful login user will be redirected to that profile URL.
function wpso_take_to_profile( $user_login, $user ) {
if ( function_exists( 'bp_core_get_user_domain' ) ) {
$url = bp_core_get_user_domain( $user->ID );
wp_redirect( $url );
die();
}
}
add_action('wp_login', 'wpso_take_to_profile', 10, 2);

(Wordpress Multisite) Limit function to given sites

I have a little problem with my website, sorry for my noobish question, but I really can't solve the issue:
How can I, using multisite, limit a function to only given site? I use such function to adapt homepage regarding user state:
function switch_homepage() {
if ( is_user_logged_in() ) {
$page = 4284; // for logged in users
update_option( 'page_on_front', $page );
update_option( 'show_on_front', 'page' );
} else {
$page = 4133; // for logged out users
update_option( 'page_on_front', $page );
update_option( 'show_on_front', 'page' );
}
}
I want the code to just work on a main site. Thanks for you help!
You can test if the current site is the main site with this code:
if ( is_main_site() ) {
// Do stuff only for the main site
}
Another option is to add your code in a plugin and then activate the plugin only on the main site.

The plugin generated N characters of unexpected output during activation [duplicate]

This question already has answers here:
The plugin generated X characters of unexpected output during activation (WordPress)
(25 answers)
Closed 4 years ago.
I want to create a wordpress plugin by just following the example listed here based on a class OOP architecture with an external setup object, and adapting the source code on my own way like this:
main plugin file:
<?php
/*
Plugin Name: My Plugin
Description: My Plugin
Version: 1.0
Author: drwhite
Author URL: drwhite-site
Plugin URL: drwhite-site/video-ad-overlay
*/
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
register_deactivation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_deactivation'));
register_uninstall_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_uninstall'));
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
class VAO_Setup_File{
protected static $instance;
public static function init()
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( current_filter(), array( $this, 'load_files' ));
}
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
}
In my plugin root directory i have created a subdirectory called includes within i put the setup file to be loaded on plugin load called setup.class.php:
<?php
class VAO_Setup_File_Inc
{
public static function on_activation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
}
public static function on_deactivation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
}
public static function on_uninstall()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
check_admin_referer( 'bulk-plugins' );
// Important: Check if the file is the one
// that was registered during the uninstall hook.
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
}
}
When i activate the plugin i got an error like the following:
I have read several questions posted by other users here and this may be duplicated question, but any of suggested answer worked for me including:
Remove space from start of tags and even remove the php end tag: nothing changed
in wp_config.php file i set wp_DEBUG to true , but it doesn't show errors
I have converted the file to UTF8 (without BOM) nothing changed
Have you put the eye in the issue ?
You are getting this error because your plugin is generating a PHP error that is being outputted to the page and causing the headers sent error you see... The problem with your code is that your function
public function load_files()
{
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
}
is not being called in time, so
register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
is looking for a function that doesn't exist, inside a class that doesn't exist. Move your
foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
include_once $file;
}
outside the class altogether, and it'll load just fine. It may require you to rethink your use of
add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
and the way your plugin is being created, but it's a step in the right direction. If you copy and paste the code from the link you got this code from, his code displays the same problem...

Categories