template_redirect() doesn't seem to be working

kind of a rookie question but here goes: I'm working on a Wordpress theme from scratch on my local machine, and I'm setting up a static page for the Blog. I have my archive.php file for all the posts and then a single.php for individual posts. However, when I put these files in my root folder (themes -> theme name), along with index.php, the file always seems to default to index.php. In addition to that, I was trying to use …
Category: Web

Trying to re-direct users to specific page based on an ACF variable

I have a case where I have an ACF variable that is stored within a users profile and based on that variables state I need to redirect to one of two pages once the user logs in. This is just a true or false value that will represent if a user has logged in before. I have tried using wp_redirect to do this and it does sort of work but seems to run excruciatingly slow and sometimes will briefly get …
Category: Web

URL Rewriting for PHP script on an image URL

I built an image randomizer so that when I open a .php file in a URL that it displays the image with readfile($randomImage);. This works locally but when I upload it to the server it gets blocked by the firewall since we do not want to be able to load URLs like https://www.example.com/wp-content/themes/THEME/randomImage.php I want to load a .php script on a .jpg file. What needs to happen is that when I use the URL (for instance) https://www.example.com/image.jpg and when …
Category: Web

Serve specific "template" page at any URL that ends in its slug

Still new to WordPress, so please go easy on me. Goal: Prevent users of a specific role from viewing other custom post types made by users of same role. Intended solution: Modify the URL and redirect to a custom 403 page when the user ID doesn't match the post author. As such, if the person attempting to view example.com/%taxonomy%/%private-post-slug% would instead be redirected to example.com/%taxonomy%/403 Current redirect code: ​<?php add_action( 'template_redirect', 'private_posts_redirect'); function private_posts_redirect() { $user = wp_get_current_user(); $post = …
Category: Web

Redirect page without changing the url

I have a page running a custom shortcode. When an element on the page is clicked I am pushing a new url into the browser (using history.pushstate) and displaying the content in the same window. If this url is copied and pasted at a later date into the browser, I want to be able identify this and display the content. As the requested element is not actually a real page/post, I am having difficulty. I can use template_redirect hook to …
Category: Web

wp_redirect on base wp-admin and login

UPDATE: I've done some more research on my problem and have narrowed down the problem further. The question below has been very rewritten from the original. I'm trying set up a plugin to redirect ALL page requests - front-end, admin, login, etc - to an additional code validation page (whether that's advisable or not is moot, it's what the client wants). Based on the functions, hooks, and filters I need to use, I've determined that the earliest possible hook I …
Category: Web

How to pass a message using template_redirect

I'm using the following function to redirect a post to another using custom field. It's working just fine until I added the line#8 to pass an ID so that I can grab it in to the post (to where was redirected), and show a custom message that, you are redirected from that particular post: function project_do_redirect() { if( !is_singular( 'mycpt' ) && !is_single() ) return; global $post; $redirect_post_id = get_post_meta( $post->ID, 'redirect', true ); $redirect_url = $redirect_post_id ? get_permalink( $redirect_post_id …
Category: Web

Adding rewrite rule dynamically for search results redirecting to 404 URL

We need the search url structure like the below URL : https://siteurl.com/in/chennai/accountant/ We have already a post url with same structure for single post detail page URL : https://siteurl.com/in/chennai/accountant/ramesh/ We have added coding like below add_action( 'init', function() { add_rewrite_rule( 'in/([^/]*)/([^/]*)/?', 'index.php?pagename=on&wpdocs-pro-category-action=$matches[1]&wpdocs-product-cat-id=$matches[2]', 'top' ); } ); If one is matched another one getting 404 for example if the above code works for search url then the post detail page going to 404 page. Here are the other codes for beautifying …
Category: Web

How to make WordPress search page use a relative path

I am working with a WP blog hosted on a subdomain (eg. wp.foo.com). The primary domain (eg. foo.com) is a non-WP platform running a main app/service. For blog content, however, the main platform uses a reverse proxy to display the WP blog page and articles but with the primary domain showing (eg. foo.com/blog/ rather than wp.foo.com/blog/). That part is all well and good thanks to the Make Paths Relative plugin. Except for searching the blog... the one path it does …
Category: Web

How to correctly detect accessing wp-content/uploads?

Not sure if this is a stupid question or not; however, I've been trying to detect when a user accesses a PDF. I want to be able to know how often a user is accessing specific PDF files. I have been playing around with the 'template_redirect' hook, but the below code doesn't seem to be functioning. function detect_access() { $server_request = $_SERVER['REQUEST_URI']; if ( strpos($server_request, '/wp-content/uploads') !== false ) { wp_redirect( home_url('/') ); exit; } } I would really appreciate …
Category: Web

Template_redirect works, but headers aren't sent when checking via cURL

I'm creating a 307 redirect to maintenance page for when my site goes through maintenance. My PHP class is included in functions.php, and gets instantiated immediately. In my __construct, I call: add_action('template_redirect', [ $this, 'redirectToMaintenance' ] ); Then my redirectToMaintenance() method looks like this: public function redirectToMaintenance() { $pageToLoad = get_the_permalink(123); // for test page ID header( "HTTP/1.1 307 Temporary Redirect" ); header("Location: ". $pageToLoad ); die(); } This works perfectly, but if I try: curl -iL https://example.com No redirects …
Category: Web

Redirect to first child in menu

I have the following page hierarchy: Page Grandparent (This page) Page Parent 1 (Should direct here) Page Parent 2 Page Child Page Child Page Parent 3 Page Parent 4 My parent and child pages have been assigned via page attributes. The pages are then ordered via Appearance > Menus. On the front end, when you click on Page Grandparent, I'd like it to redirect to the next menu item (Page Parent 1). To achieve this, I've built the following page …
Category: Web

auth_redirect() doesn't send users to the page where they orginally tried to acces

In the site I am building, I'm using "theme my plugin" so I could make my own login, logout, registration pages. For the login form, I used the default wp_login_form input names, and the default value for the hidden type input named redirect_to which is: ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . for some pages, I want user to login to access to them, so I use the auth_redirect() function, as expected it sends users …
Category: Web

Custom Login page, redirection and restrictions

I'm having some issues with my custom login page with redirections and restrictions. Any user can browse a 'community' page but they can't access the 'distributors' pages. I have set a rule so if anyone that it's not logged in will be forwarded to the home page. Those who are logged will be redirected to the 'distributor' pages. Everything seems to be working fine except on Firefox. For the custom login page I'm using a form from Elementor. Here's custom …
Category: Web

Redirect Users who aren't logged in, aren't post authors and aren't admin. Frontend

Very new to PHP and I'm trying to do the following through a snippets plugin but keep getting critical errors: Restrict all non-logged in users to home, about and pricing pages. If attempting to access other pages, redirect to "no-permission" message page. If user is logged in, allow them to only view posts on the front end if they are the post author. If attempted, redirect to "no-permission" page. Admin has full read access on front end. I've tried with …
Category: Web

using add_filter in a template_redirect function?

I have certain pages that I would like to filter, replacing some tags, with a value. Rather than attempting to filter every page, I only want some pages, but to make it use even less resources (not even have it call a filter and copy the contents to a variable) the idea is to use template_direct to only set a the_content filter on certain pages. Is that okay or will it cause issues. For example: function my_template_redirect() { if (is_page(array(4, …
Category: Web

How to redirect to category page if page does not exists?

In my current case, the system try to find a page and show 404 if it does not find. I would like with the same url... like https://example.com/{{slug}}/ Check if a page with this slug exists : Yes - show the page No - switch to the category page (with same slug) No category page -> show page 404 What is the best recommandanted way to do this in WP?
Category: Web

Get posts on template redirect

I'm trying to validate some $_GET parameters before letting the user view a particular page. In the actual template file, this code works: $user = get_user_by('ID', $_GET['sscp']); $args = [ 'post_type' => 'appointment', 'author' => $user->ID, 'meta_query' => [ [ 'key' => 'clinic', 'value' => serialize( [ wp_get_current_user()->dr_clinic_assigned ] ) ] ] ]; $posts = get_posts($args); echo "<pre>"; print_r( $posts ); echo "</pre>"; But when I do add_action( 'template_redirect', 'my_redirect_function' ); it doesn't show any results. I want to do …
Category: Web

Page get parameter doesn't work with is_single() function

If exist the page parameter in the blog post, I have to redirect to the 404 page instead of current post URL. I tried to check the current page type (is singular) and try to get page parameter in the function.php, but I couldn't. It's doesn't work together. I need to implement it only for blog post. If possible .htaccess redirect also fine I tried with this code but it doesn't work. But it's working without the is_single function. When …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.