Hook a function on the entire backend in wordpress - php

I am trying to hook a function on the entire wordpress backend for a user custom role, just that when the user is accessing the edit posts page post.php?post=xxxx0&action=edit, the function is no more available, printed message disappears.
if ( is_user_logged_in() ) {
echo 'here';
function contributor_posts() {
echo 'here2';
}
add_action( 'admin_init', 'contributor_posts' );
}
echo here - disappears - though it doesn't go on the else
echo 'here2- disappears also

admin_init action is triggered when a logged in user access the admin area, there is no need for is_user_logged_in() check here.
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init
edit:
Put the code below inside functions.php, admin_init action must be triggered always in every part of the admin area. If that is not the case then i really do not know where the problem is. Visit Wordpress Action Reference to see the list of action hooks available and the order of execution.
function contributor_posts() {
echo 'here';
}
add_action( 'admin_init', 'contributor_posts' );

Related

Non logged in users Woocommerce, redirection

I'm doing a shop online with Woocommerce, where my client ask me he don't want people to be able to register, he will create a user and password for each one of his clients. That way he can control who buys on his shop.
So I went into Woocommerce and disable the registration at checkout and everywhere, and the option to allow guests to place orders. Everything works fine, except that when someone tries to place an order, when logged out, when he tries to go to the checkout page, it just shows an unformatted message saying "You must be logged in to place an order". Is there a way where I can redirect not logged in customers to login page, when trying to access checkout?
May be this code could be more compact, simple and convenient:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in() {
if(!is_user_logged_in() && is_checkout())
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
Reference: WooCommerce login redirect based on cart
Use the following code in functions.php
add_action( 'template_redirect', 'redirect_user_to_login_page' );
function redirect_user_to_login_page(){
// Make sure your checkout page slug is correct
if( is_page('checkout') ) {
if( !is_user_logged_in() ) {
// Make sure your login page slug is correct in below line
wp_redirect('/my-account/');
}
}
}
I manage to do it my self! But still if someone has a cleaner code to do so, I'm open to suggestions.
What I did was to paste the following code on the functions.php file of the child theme:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
if(!is_user_logged_in() && is_checkout())
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/')
);
wp_redirect($url);
exit;
}
}
Thanks
kind regards

auth_redirect() or wp_redirect() from a specific page in wordpress

I want to redirect a user to login, when he clicks on a certain menu item of a page. Only that item requires login and then it redirects back to the page. All other pages are open for public viewing.
I searched a lot on how i can use auth_redirect() for a specific page, but to no avail.
Please help.
This can do the trick.. just take care of condition, i put both, removed the not needed..
function my_page_template_redirect()
{
if( is_page( 'your-page-slug' ) || is_singular('your-post-type' ) /** just removed not need condition if its a page keep page and if its a post keep singular one **/
{
if(!is_user_logged_in())
{
auth_redirect();
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );

wordpress redirect to login form

I know that are some answers to my question but i don't know if my problem exists in the other persons who asked for help.
I have this function that helps me to redirect users to login before access any contetnt of my website:
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url(get_permalink()));
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
That function works fine but i have a problem. When somebody is navigated to my home page and click to a link that goes here: 'example.com/about' then my function works great. When somebody he trying to go direct to 'example.com/about' by entering this into browser url then my function doesn't work and he is able to access my website. What i have do wrong? I have write my function in my templates function.php file.
I just created a page template called restriction_page.php:
<?php
/*
Template Name: My Restricted Page
*/
?>
<h1>Hello There</h1>
<?php
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url());
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
?>
i set this template as the fefault template in my about page. No solution.
Any ideas?
You can use the built in auth_redirect() function for this:
function restrict_access_if_logged_out(){
if ( !is_user_logged_in() ){
auth_redirect();
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
Note: This should go in your themes functions.php file
Method :1
you can add it to the top of page.php
If you want to add more pages, just add the ||(or) operator like so:
<?php
$pageTitle == get_the_title();
if($pageTitle == 'Submit' || $pageTitle =='NewPage') {
if (!is_user_logged_in()) {
redirect code
} else {
page code
}
} else {
page code
}
?>
Method:2
Write this into a plugin:
add_action( 'template_redirect', 'auth_redirect' );
This will force all visitors login if they aren’t already.
In some cases, this is asking for a log-in every time. This might work better:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
You should create a template page for restricted page like we create full width page tamplate, right sidebar page template, left sidebar template, etc. And in restricted page template call your function. When going to create pages, choose restricted template from the listed of the page template in right side.
If it doesn't help you, we'll be able to help you, when you paste your more code with more detail.

Is there a way to only authorise logged user to see my whole WordPress site without plugin?

is there a way to only authorise logged user to see my whole WordPress site without plugin?
My goal is to see the login page if your are not logged-in or registered.
I found that on another post:
<?php if(!is_user_logged_in()){wp_redirect( 'http://www.your-blog.com/wp-login' ); exit;}?>
Whit this solution I have to put on top from all my pages. Is there a way to put just some line of code in the functions.php to get the same results?
Adding this to the end of my functions.php doesn't do the trick:
add_action( 'muplugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
if(!is_user_logged_in()){
wp_redirect( wp_login_url() );
exit;
}
}
Don't know why, any Idea?
I have found a plugin who made the trick, but I want to try to understand how to get it work without plugin with a simple solution.
Thank you
Put below code into your current theme's functions.php file.
add_action( 'muplugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
if(!is_user_logged_in()){
wp_redirect( 'http://www.your-blog.com/wp-login' );
exit;
}
}
muplugins_loaded is the earliest action hook.
For more dfetails check - https://wordpress.stackexchange.com/questions/162862/how-to-get-wordpress-hook-run-sequence

Custom url redirect the page say You do not have sufficient permissions to access this page

i have an error message whe i tring to set the page in custom url
can anybody to tell that any action or hook to set the page or function. here is my code
<a href='<?php echo admin_url("admin.php?page=wp_crm_asign&user_id={$user_id}"); ?>'>Asign to Consultant</a>
add_action( 'admin_init', 'wp_crm_asign_admin_init' );
function wp_crm_asign_admin_init() {
if ($_GET['page'] == 'wp_crm_asign') {
echo "hello";
}
}
Can Anybody to tell the right way to do this.
thanks
You will have to create sub menu page with parent slug null, check here
add_submenu_page(null,$page_title,$menu_title,8,'wp_crm_asign','wp_crm_asign_admin_init');
Remove the admin_init hook and the function will automatically be called through this menu.
click Here to check the wordpress function.

Categories