Isse with a user forwarding function in Wordpress - wordpress

I have created this code for my Wordpress Theme's function.php file. I want to redirect the user to certain URL if they are on a certain page.
Please check if this function is correct? The code is working, but it's creating an error with Woocommerce checkout and not letting customer place an order.
function user_login_check()
{
if (is_user_logged_in()) {
if (is_page(123)) {
wp_redirect('https://example.com/alpha');
exit;
};
if (is_page(456)) {
wp_redirect('https://example.com/beta');
exit;
}
}
}
add_action('wp', 'user_login_check');

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

How to disable Woocommerce login redirect if on Checkout Page

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

Session Login in wordpress

I have a site www.example.com and a wordpress blog at www.example.com/blog.
I want my user to login at www.example.com and $_SESSION['USERNAME'] is passed to www.example.com/blog.
I just want to know, how can i automatically login the end user to wordpress blog, once they login to my main site.
For now i am passing $_SESSION['USERNAME'] and using a plugin external database login, which I have linked to my main site database.
Any function that is used to login to wordpress using session username will be helpful.
Wordpress don't use $_SESSIONyou need to code a plugin or to add some code to your functions.php to do this - cf http://www.frank-verhoeven.com/using-session-in-wordpress/
You should add something like this :
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
NB: there is also a plugin to manage Sessions with Wordpress, maybe you can use/hack this http://wordpress.org/plugins/wp-session-manager/
I have been reading on this. Here is what I have found as I am in the same issue. In your wp-config.php file at the top add:
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
HOWEVER, " The data stored in the session doesn’t go away when the user logs out or logs into a different account. For that you need to destroy the session. ..." (exerpt from this nice write up). It explains you also need to:
function myEndSession() {
session_destroy ();
}
if you can get wp user_id you could do something like this:
function init_sessions() {
if (user_logged_on_site) {
#session_start();
$user_id = $_SESSION['wp_user_id']; //this has to be set when the user logs on your website
wp_set_auth_cookie( $user_id); //log in the user on wordpress
}
}
add_action('init', 'init_sessions');
Documentation on wp_set_auth_cookie http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie

Redirect after plugin activation

How do I redirect users to my plugin settings page after they activate my plugin, I tried
register_activation_hook(__FILE__,'activate_myplugin');
function activate_myplugin()
{
//create and populate DB tables
wp_redirect(plugin_setting_url);
}
but it does not work.
You should be able to do something like this:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}
This will redirect to option page only if that plugin is activated only without using bulk activation mode .
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("options-general.php?page=your-plugin-option-page");
}
}
}
thanks for your code - it´s great, but only has one downside: upon bulk activation of plugins, you also get redirected to your defined redirect page - which might confuse user when deactivating/activating all plugins at once for test/debug reason. I therefore would propose the solution, to add an option to only redirect to your page on FIRST plugin activation:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('myplugin_redirect_on_first_activation', 'true');
}
function my_plugin_redirect() {
if (get_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY) == 'true') {
update_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY, 'false');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}
Don't worry it's very simple.
Simply paste this code in you plugin.php file
function_activation_redirect( ) {
exit( wp_redirect( 'http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage' ) )
}
add_action( 'activated_plugin', 'funtion_activation_redirect' );
http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage
in my case this is the path of my page where I want to redirect my page.
Hello i have used bellows code redirect after plugin activation. You can use this code. It's working nicely.
register_activation_hook(__FILE__, 'nht_plugin_activate');
add_action('admin_init', 'nht_plugin_redirect');
function nht_plugin_activate() {
add_option('nht_plugin_do_activation_redirect', true);
}
function nht_plugin_redirect() {
if (get_option('nht_plugin_do_activation_redirect', false)) {
delete_option('nht_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("edit.php?post_type=headline&page=news-headline");
}
}
}
nht_ is my plugin prefix & "edit.php?post_type=headline&page=news-headline" is redirect page. please replace this those.

Resources