How to Automatically Remove Default Video Links in WordPress - wordpress

I am looking for the user to redirect to the home page when the user tries to access the video permalink.
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);
I tried the above code, but it doesn't work

Actually you need to redirect the users via template_redirect hook that you can use as per below.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
$image_set = get_option( 'image_default_link_type' );
if ( is_page($image_set) && ! is_user_logged_in() ) {
wp_redirect( 'http://www.yourhomepage.com/', 301 );
exit;
}
}
Check the $image_set is getting correct URL.

Related

Non logged in user when try to see a product page will get Login page and after login will be redirected to the Product page that user clicked before

I want to give access Woo product single page only for Logged in users.
I have a product listing on the Homepage and Shop page, What I want is: when a logged out user click on a Product, the user will get a Login form and after Logged in user will be redirected to the Product page that he wanted to View.
So the flow will be something like:
Home/Shop-> click to Product X -> Login page -> Redirect to Product X single page
Currently, I am using the regular Woo Login form created by this function
woocommerce_login_form()
I am trying bellow code snippets:
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
wp_safe_redirect($location);
exit();
}
}
add_action('init','my_login_redirect');
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
//wp_safe_redirect($location);
}
-----------------------
AND ALSO THIS ONE
-----------------------
function redirect_after_login(){
global $wp;
$protocol='http';
if (isset($_SERVER['HTTPS']))
if (strtoupper($_SERVER['HTTPS'])=='ON')
$protocol='https';
if (!is_user_logged_in() && is_product() ){
$redirect = site_url() . "/my-account.php?redirect_to= $protocol://" .
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( 'wp', 'redirect_after_login', 3 );
In both of cases, the problem is, it always find the Login page as HTTP_REFERER / REQUEST_URI
Because currently, I am using below code to redirect Non-logged-in user who is trying to see the product page to the Login page:
add_action('template_redirect', 'ethis_redirect_for_loggedin_users');
function ethis_redirect_for_loggedin_users() {
if ( !is_user_logged_in() && is_product() ) {
wp_redirect(site_url().'/default-login');
exit;
}
}
You can use the template_redirect filter hook to prevent guest users to access a single product page.
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && is_singular( 'product' ) ) {
global $post;
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')).'?redirect='.get_the_permalink( $post->ID ) );
exit();
}
}
Then after you have to use the woocommerce_login_redirect filter hook for login redirect.
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 2 );
function my_login_redirect( $redirect, $user ) {
if( isset( $_GET['redirect'] ) && $_GET['redirect'] != '' ){
return $_GET['redirect'];
}
return $redirect;
}
Code will go in your active theme functions.php Tested and Works.

wp_redirect goes to infinity loop

I need to check for logged-in users & redirects to pages with wp_redirect, but it goes through redirected many times
I have tried with template_redirect & init hooks
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( !is_user_logged_in() ) {
wp_redirect( 'http://localhost/lawyer_portal/login/' );
exit;
}
}
Try the follows code snippet -
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
// Assume that url login slug is a page
if ( !is_user_logged_in() && !is_page( 'login' ) ) {
wp_redirect( 'http://localhost/lawyer_portal/login/' );
exit;
}
}

Error with wp_redirect

I want to redirect admin users to a maintenance page (https://mysite/maintenance/), but firefox tells me the redirection is not correctly made
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
Have you an idea ?
You should be using the template_include filter for this:
add_filter('template_include', 'wpse_44239_template_include', 1, 1);
function wpse_44239_template_include($template){
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
return $template;
}
template_redirect is the action called directly before headers are sent for the output of the rendered template. It's a convenient hook to do 404 redirects, etc... but shouldn't be used for including other templates paths as WordPress does this innately with the 'template_include' filter.
template_include and single_template hooks deal ONLY with the path of the template used for rendering the content. This is the proper place to adjust a template path.
What is the specific error Firefox is reporting? Sounds like it could be an infinite redirect loop. I would suggest adding a check to make sure you're not already on the maintenance page, ie:
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator'))
{
global $wp;
$current_url = home_url( $wp->request );
$position = strpos( $current_url , '/maintenance/' );
if ($position===FALSE) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
}

How to change WordPress Login URL without Plugin

I am trying to change the wordpress Admin Login URL (wp-admin) by plugin and it will work good. But can't change wordpress theme option. So, any one Please give good solution. and without Plugin. Also want easy way.
You can try this code.
This code redirects to the homepage whenever the default /wp-admin or /wp-login is accessed. You can set a passcode as the login URL and only allow wp-admin and wp-login access via this URL: https://www.yourdomain.com/?you-set-your-passcode
Add this code to the functions.php of your theme.
// define and set passcode that serves as login url.
define('PASSCODE','make-you-own-passcode');
function mask_login_url(){
// redirect to login page when passcode is verified
if( !is_user_logged_in() && parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY) == PASSCODE ){
wp_safe_redirect( home_url('wp-login.php?'. PASSCODE .'&redirect=false') );
exit();
}
// redirect to dashboard if user has already logged in
if( is_user_logged_in() && parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY) == PASSCODE ){
wp_safe_redirect( home_url("wp-admin") );
exit();
} }
add_action( 'init', 'mask_login_url');
function mask_login_redirects(){
if( isset($_POST['passcode']) && $_POST['passcode'] == PASSCODE) return false;
// redirects to dashboard when /wp-admin is accessed and user is logged in
if ( (is_user_logged_in()) && (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false)) {
wp_safe_redirect( home_url("wp-admin"), 302 );
exit();
}
// redirects to homepage when /wp-admin or /wp-login is accessed and user is not logged in
if ( (!is_user_logged_in()) && ((strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) || (strpos($_SERVER['REQUEST_URI'], 'wp-login') !== false)) && ( strpos($_SERVER['REQUEST_URI'], PASSCODE) === false ) ) {
wp_safe_redirect( home_url(), 302 );
exit();
}
// redirect to homepage after logout
if( strpos($_SERVER['REQUEST_URI'], 'action=logout') !== false ){
check_admin_referer( 'log-out' );
wp_logout();
wp_safe_redirect( home_url('?logged-out'), 302 );
exit();
}
}
add_action( 'login_init', 'mask_login_redirects', 1);
// Add a passcode hidden field to login form
function custom_login_hidden_field(){
echo '<input type="hidden" name="passcode" value="'. PASSCODE .'" />';
}
add_action('login_form', 'custom_login_hidden_field');
There are several url's to get to the WP login/admin screen, so you need to change all of them. The below could be DRY-er for sure, but it's broken down into individual solutions. Everything redirects to home_url() except for desired login url:
// Disable Password Reset URL & Redirect.
function disable_register_and_lost_pwd_actions() {
if (isset( $_GET['action'] )){
if ( in_array( $_GET['action'], array(
'lostpassword',
'retrievepassword',
'register'
) ) ) {
wp_safe_redirect( esc_url(home_url(), 302 ));
exit;
}
}
if (isset( $_GET['registration'] )){
if ( in_array( $_GET['registration'], array(
'enabled',
'disabled'
) ) ) {
wp_safe_redirect( esc_url(home_url(), 302 ));
exit;
}
}
}
add_action( "login_init", "disable_register_and_lost_pwd_actions" );
Below based on this solution:
https://wordpress.stackexchange.com/questions/160098/change-login-url-without-plugin
Whatever you define as the variable $new_login is will be the new login slug:
www.your-website.com/
or
www.your-website.com/wp-login.php?
function redirect_to_new_login() {
$new_login = '<your-new-login-here>';
if (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == '/'.$GLOBALS['new_login']) {
wp_safe_redirect(esc_url(home_url("wp-login.php?{$GLOBALS['new_login']}")));
exit();
}
}
add_action('init', 'redirect_to_new_login');
function redirect_old_login_to_home() {
$new_login = '<your-new-login-here>';
if (strpos($_SERVER['REQUEST_URI'], $new_login) === false) {
wp_safe_redirect(home_url(), 302);
exit();
}
}
add_action('login_head', 'redirect_old_login_to_home');
I have two recomendations for you.
1 - https://www.elegantthemes.com/blog/tips-tricks/how-to-create-a-custom-wordpress-login-url
2 - This plugin has the option to rename the admin url.
https://www.icontrolwp.com/blog/wordpress-shield-security-plugin/
Regards,
Ed.

Wordpress redirect if URL parameter is empty for each post

So there are people who access my website directly (not thru the tracking link I got with Voluum), thus they are not able to click the links and I can't see them as a part of my stats.
How can I redirect users who don't have a /?voluumdata=BASE64... URL parameter to a tracked URL, and to have a different redirect for each blog post?
I was testing and looking for a plugin / .htaccess trick for hours but nothing seemed to help.
Edit: I found a solution that I was certain is going to work but for some reason it didn't:
[insert_php]
if(empty($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Also tried:
[insert_php]
if(!isset($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Both just break the page loading proccess.
Unfortunately, I cannot understand what is the purpose of the code you have entered in your question. I mean that is not clear the reason you use the tags [insert_php].
A solution to your problem it can be the following.
function redirect_direct_access( ) {
// You may use the code:
//
// global $wp_query
//
// in order to determine in which pages you should run your
// redirection code. If you only check for the token existence
// then you will be faced with redirection loop. I don't explain in
// depth how to use the $wp_query as it is not part of your question
// but you always have the opportunity to check what is the contents
// of this variable using the code:
//
// echo "<pre>";
// print_r( $wp_query );
// echo "</pre>";
//
// This way you will be able to build your specific if statement
// for the page you like to test.
if (
! isset( $_GET[ 'voluumdata' ] ) ||
empty( $_GET[ 'voluumdata' ] )
) {
wp_redirect( home_url( '/page/to/redirect/' ) );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );
You can find more information in the WordPress documentation related to the template_redirect hook.
Just in case anyone would face the same issue, #Merianos Nikos has given a half-answer and I mastered it into this:
function redirect_direct_access( ) {
$post_id = get_the_ID();
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );

Resources