Wordpress disable private posts links - wordpress

I have a website of sports competitions. I can create the calendars of future matches, and then I set the post status to "private".
Users that are NOT logged in can click on the post and access it.
How can I disable the link to the post for users not logged in?

Put this code inside your function.php file.
function getPrivatePostMatch()
{
global $post;
$status = get_post_status( $post->ID );
if( $status == "private" )
{
wp_redirect( home_url() ); // you can add your page url here
exit;
}
}
add_action('template_redirect','getPrivatePostMatch');

Related

Add custom content after redirect wordpress

I've created a function redirecting all my logged-out users to the membership page.
I want them to buy a package plan before they can view the page they tried to visit before being redirected.
If logged-in -> view the download page.
If logged-out -> redirect to the membership page -> purchase a package -> view the download page.
I want to add content to the membership page, something like:
'The product that you are trying to download cannot be purchased individually, but it's part of our membership package'
The problem is that I couldn't find a way to execute any function after the wp_redirect() function. So I don't know how to display the content only to those redirected.
Is there a way to achieve it?
in your header.php:
if( !is_user_logged_in() ){
nocache_headers();
wp_safe_redirect( get_permalink( 'YOUR_MEMBERSHIP_PAGE_ID' ) . '?message=true' );
exit;
}
in your functions.php:
function add_query_vars_filter( $vars ){
$vars[] = "message";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
in your membership page:
if( get_query_var('message') && !is_user_logged_in() ){
echo 'The product that you are trying to download cannot be purchased individually, but it\'s part of our membership package';
}

Redirect User After creating a page

I want to redirect the users of my site to a custom thank you page after they create a page and send it for review.
I found this snippet, but this one is for posts, and it's for publishing, not for pending review. The role that I want to use for this snippet is Tutor Instructor.
Can somebody help me to edit this snippet? It's a WordPress site.
add_action( 'save_post', 'redirect_user_page_list', 10, 3 );
function redirect_user_page_list( $post_ID, $post, $update ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if ( 'user_role' == $role[0] ) {
$url = '';
wp_redirect($url);
exit;
}
}
}
//replace user_role with your actual role the one for which you need to implement this functionality. Also place the proper url in $url.
You can execute this code after your code
window.location.href = 'https://yoursite.com/thank-you';

wc_logout_url without confirmation AND redirect to current page in Wordpress WooCommerce

I would like to redirect the user that is logging out to the same page.
For instance: user is on category page and is logging out. After logging out I would like to have that the user is still on that specific category page (or any page the user is on). Now it will redirect the user to the homepage.
This is the code I'm using:
function iconic_bypass_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'home' ) ) ) );
exit;
}
}
add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );
I tried to change line 5 with the code below but without result.
wp_redirect( str_replace( '&', '&', wp_logout_url( get_permalink() ) ) );
According to the docs on wp_logout_url(), you can specify the redirect url. In this case, you should update the Logout link itself. So if you had a logout link in the header.php file as an example, you could update the wp_logout_url() reference to include the current page url. So, anytime a user clicked the logout link, it would also have the current page as the redirect link.
All you need is the current page url. Referencing this post you can see one method for getting the current page url. This combined with the wp_logout_url() function, and your HTML might look something like this,
<?php global $wp; ?>
Logout
Now, the user will always be redirected from the previous page. If you do not have control over the Logout link itself, your only options would be to handle the logout via AJAX, or track the previous url yourself, via a query variable, session, cookie, etc.
You can try this and set your own redirect url
<?php
//wordpress logout
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] :
"https://example.com";
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}

Woocommerce custom logout without confirmation

I am trying to implement custom logout without logout confirmation in woocommerce.
I created a page and added this code. and added link of this page to menu. But its not working.
session_start();
session_destroy();
header("Location:http://www.liberatium.com/");
Confirmation happens because you are missing the neccessary nonce in the URL, which is being checked in wp-login.php
case 'logout' :
check_admin_referer('log-out');
...
Use wp_logout_url in order to retreive the URL including the nonce. If you want to redirect to a custom URL, simply pass it as an argument.
Log out
if that's not working means, Add below function in functions.php and try above code...
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ?
$_REQUEST['redirect_to'] : '';
$location = str_replace('&', '&', wp_logout_url($redirect_to));;
header("Location: $location");
die();
}
}
I use the woocommerce endpoing logout in the menu item with link like
https://yoursite/my-account/customer-logout/?_wpnonce=2bbbac43a8&customer-logout=true
&customer-logout=true - is keypoint here to logout without confirmation. Just add it to the and of link in menu item.
One disadvantage of this method - after successfull logout user is redirecting to login page, not to the current page.
I used this code in the wordpress functions.php, to logout user after payment or close the browser
/**
* Bypass logout confirmation.
*/
function iconic_bypass_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
exit;
}
}
add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );
You can create new shortcode log out URL in function.php of theme and add everywhere you want in tag html. I tried and success.
// Custom shortcode log out
function custom_logout()
{
return wp_logout_url(bloginfo( 'url' ));
}
add_shortcode( 'custom_logout_s', 'custom_logout' );
You can use this if you prefer a logout shortcode tag for html (with optional redirect slug between tags):
/* Shortcode for no confirmation logout link with optional redirect slug between shortcode tags */
//
// E.g. [logout_link]my-account[/logout_link]
//
function logout_to_optional_redirect_slug_function( $atts, $redirect_slug = null ) {
$redirect_full_url = get_site_url(null, '/', 'https') . $redirect_slug;
$logout_url = wp_logout_url($redirect_full_url);
$logout_hyperlink = "<a href='".$logout_url."'>Logout</a>";
return do_shortcode($logout_hyperlink);
}
add_shortcode( 'logout_link', 'logout_to_optional_redirect_slug_function' );

How can I have users only see their content in Wordpress?

This goes beyond posts and media. I have several CPT's and a calendar. Is there a way to have wordpress check the user name and only show content they have created?
In the backend, to filter all post types that are shown and restrict the visualization you can use pre_get_posts.
add_action( 'pre_get_posts', 'users_own_content_so_12761756' );
/**
* Show only posts of the current user in the dashboard
* affects posts, pages, media and custom post types
*/
function users_own_content_so_12761756( $wp_query_obj )
{
// Restrict hook to the backend
if( !is_admin() )
return;
global $current_user;
get_currentuserinfo();
// http://php.net/manual/en/function.is-a.php
if( !is_a( $current_user, 'WP_User') )
return;
if( !current_user_can( 'administrator' ) )
$wp_query_obj->set( 'author', $current_user->ID );
}
After applying this code, you'll notice that the post count is not correct: it'll show the total count and not the user count. To adjust that, refer to this Q&A: Update post counts (published, draft, unattached) in admin interface.
You'll need to care about user roles and capabilities as well, blocking the rights to edit someone else's posts/pages/cpts. That's because a user can type in the browser address example.com/wp-admin/post.php?post=POST_ID&action=edit and access the post, if he/she has the rights to do so.
you can try adding this to the loop
<?php $author = get_the_author();
$current_user = wp_get_current_user();
if($author != $current_user->user_nicename) {
echo "permission denied";
break;
} ?>
I use the members plugin to create a custom-defined role for users.
http://wordpress.org/extend/plugins/members/

Resources