redirection for deleted pages - wordpress

Hi I need to find a way to redirect deleted pages to another pages using 301 redirection, to a specific page depending on the URL of the page.If the url is
http://example.com/projects/test-page-redirectpage/ then it should be redirect to the 'redirectpage' after page deletion.There are hundreds of pages like that which have 'redirectpage' at the end of URL,so I can not do that manually by and redirection plugin.I have wrote following code but it is only working by page name.I want such a function which can detect that the 'redirectpage' from the URL and if that url is not exit then it should be redirect to the 'redirectpage'
function get_page_by_name($pagename)
{
$pages = get_pages();
foreach ($pages as $page) if ($page->post_name == $pagename) return $page;
return false;
}
function redirect_301() {
$page = get_page_by_name('test-page-redirectpage');
if (empty($page)) {
wp_safe_redirect( home_url('http://example.com/redirectpage/'), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirect_301', 1 );

You are correct with using template_redirect but you should be checking if the URL can be translated to post ID and the current request URL (from global $wp object) for occurrence of redirectpage string (at the end of it):
function so_423_redirectpage_redirect()
{
global $wp;
if (url_to_postid(home_url($wp->request)) === 0 &&
('redirectpage' === substr(untrailingslashit($wp->request), -12) || 'redirect-page' === substr(untrailingslashit($wp->request), -13))) {
wp_safe_redirect(home_url('http://example.com/redirectpage/'), 301);
exit();
}
}
add_action('template_redirect', 'so_423_redirectpage_redirect');

I think you should use the 404.php file template here (from your theme), so you're pretty sure that the page visited doesn't exist.
Before the get_header() function you can do the following check:
$requested_uri = explode('/', $_SERVER[REQUEST_URI]);
if(strpos(array_pop($requested_uri), '-redirectpage') !== FALSE) {
wp_safe_redirect(site_url('redirectpage'), 301);
}

Related

WordPress Login Redirect: Redirect URL adding to login URL instead of replacing it

I've got a redirect that if someone is logged into https://yieldofdreams.live/login they get redirected to another page.
I used the following function:
function add_login_check()
{
if ( is_user_logged_in() && is_page(608) ) {
wp_redirect('yieldofdreams.live/redirect/');
exit;
}
}
add_action('wp', 'add_login_check');
This does work, but instead of redirecting to the URL above, it instead adds to the login URL.
So that redirect takes you instead to;
https://yieldofdreams.live/login/yieldofdreams.live/redirect/
How can I make it where it completely redirects to the proper URL I want?
Use template_redirect instead.
function bks_add_login_check()
{
if ( is_user_logged_in() && is_page(608) ) {
wp_redirect(home_url() . '/redirect');
exit;
}
}
add_action('template_redirect', 'bks_add_login_check');
Learn about the hook here : https://developer.wordpress.org/reference/hooks/template_redirect/

Override wordpress routing / url rewriting?

I would like to serve different content based on the URL.
I started with a custom page setup via a custom template but I am open to something else.
The main goal is to have one PHP page that can serve different contents programmatically based on the URL.
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc...
I have been looking at add_rewrite_rule, add_rewrite_tag, flush_rewrite_rules and WP_Rewrite API but I am getting confused about which one I should use?
I found an example here but I could not make it work, I get 404s, any idea why?:
/*
Plugin Name: Products Plugin
Plugin URI: http://clivern.com/
Description: Register URL rules for our products
Version: 1.0
Author: Clivern
Author URI: http://clivern.com
License: MIT
*/
function products_plugin_activate() {
products_plugin_rules();
flush_rewrite_rules();
}
function products_plugin_deactivate() {
flush_rewrite_rules();
}
function products_plugin_rules() {
add_rewrite_rule('products/?([^/]*)', 'index.php?pagename=products&product_id=$matches[1]', 'top');
}
function products_plugin_query_vars($vars) {
$vars[] = 'product_id';
return $vars;
}
function products_plugin_display() {
$products_page = get_query_var('pagename');
$product_id = get_query_var('product_id');
if ('products' == $products_page && '' == $product_id):
//show all products
exit;
elseif ('products' == $products_page && '' != $product_id):
//show product page
exit;
endif;
}
//register activation function
register_activation_hook(__FILE__, 'products_plugin_activate');
//register deactivation function
register_deactivation_hook(__FILE__, 'products_plugin_deactivate');
//add rewrite rules in case another plugin flushes rules
add_action('init', 'products_plugin_rules');
//add plugin query vars (product_id) to wordpress
add_filter('query_vars', 'products_plugin_query_vars');
//register plugin custom pages display
add_filter('template_redirect', 'products_plugin_display');
First of all, make sure your pretty permalinks are enabled, in this case the option "Plain" in Settings - Permalinks should be unselected:
Select one of these options to enable pretty permalinks
You can check whether pretty permalinks are enabled in the code like so:
function is_enabled_pretty_permalinks() {
return !empty( get_option( 'permalink_structure' ) );
}
if ( is_enabled_pretty_permalinks() ) {
echo 'Pretty permalinks enabled';
}
Next add a new rewrite rule:
function add_my_rewrite_rule() {
$page_slug = 'products'; // slug of the page you want to be shown to
$param = 'do'; // param name you want to handle on the page
add_rewrite_rule('my-plugin/?([^/]*)', 'index.php?pagename=' . $page_slug . '&' . $param . '=$matches[1]', 'top');
}
add_action('init', 'add_my_rewrite_rule');
Add your parameter to query vars so you will be able to handle it on the page:
function add_my_query_vars($vars) {
$vars[] = 'do'; // param name you want to handle on the page
return $vars;
}
add_filter('query_vars', 'add_my_query_vars');
Then you can access your query var do in the page template or in a shortcode, for example:
function my_plugin_shortcode_handler( $atts ){
$do = get_query_var( 'do' );
if ( $do === 'this' ) {
return 'do this';
} else {
return 'do that';
}
}
add_shortcode( 'myshortcode', 'my_plugin_shortcode_handler' );
Then place the shortcode to the content via Gutenberg.
Check out the links:
https://some-url.com/my-plugin/this/ - outputs "do this"
https://some-url.com/my-plugin/that/ - outputs "do that".
This can be solved by using query params. Like you mentioned you have set up custom page via a custom template. You can read the URL and check for the parameters and based on that you can send data from the PHP template page.
e.g,
function cleanTheInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
if (isset($_GET['page_url'])) {
$theUrl = cleanTheInput($_GET['page_url']);
}
if($theUrl == 266)){
// data for https://some-url.com/?page_url=266
}
if($theUrl == 366)){
// data for https://some-url.com/?page_url=366
}

Redirect in an url link after commenting in wordpress

I have this code but it is not working.
add_action('comment_post_redirect', 'your_redirect_function');
// A function that redirects your users after they have commented.
function your_redirect_function ($location, $comment) {
// Here all you need to do is return the url of your target page.
$page = 'http://www.stackoverflow.com';
return $page;
}
Any suggestions?

wordpress redirection from header.php not working

Nobody access my full wordpress website without login, if user is not logined than redirect it to http://example/submit-project/.
I'm trying to do this with this code:
$current_user = wp_get_current_user(); $crntusr = $current_user; if($crntusr->ID == 0){ wp_redirect( 'example.com/login'; ); }
But get this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-content/themes/freelanceengine/header.php:14)
in
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-includes/pluggable.php
on line 1195
1) wp_redirect() does not exit automatically, and should almost always be followed by a call to exit.
2) you should make an redirect before your template output something.
3) Better to refrain from using absolute links like http://example.com, you can get your WP login page via wp_login_url() function.
Remove your redirect code from your header.php file and try to add this code to your functions.php:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) ) {
wp_redirect(wp_login_url());
exit;
}
}
Update.
If your login form is on custom page (http://example/submit-project/), then you should use this code:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and $_SERVER['REQUEST_URI'] != '/submit-project/' ) {
wp_redirect('http://example/submit-project/');
exit;
}
}

WordPress URL Modification

I have a WordPress page at
http://www.mysiteurl.com/pagename/
and I want any URL of the form
http://www.mysiteurl.com/pagename/{any string here}
to redirect or display my original page: http://www.mysiteurl.com/pagename/. How do I go about doing this?
Like what Kodos Johnson suggested, the function should be something like this:
function redirect_to_page() {
//get parsed uri separated by '/'
$a_uri = explode('/', $_SERVER['REQUEST_URI']);
//check if the page is the page you want to redirect
if( $a_uri[1] === 'pagename' ) {
//redirect to siteurl/pagename
wp_redirect( home_url('pagename'));
exit();
}
}
add_action('template_redirect', 'redirect_to_page');
Hope this helps!

Resources