The first problem I see is here:
if( get_page_by_slug($slug) ){
}wp_redirect('https://example.com/private-page/'.$slug."https://wordpress.stackexchange.com/");
exit;
The if()
statement will do nothing; your wp_redirect()
and exit
statements are outside the {}
braces.
Try this instead:
if( get_page_by_slug($slug) ){
wp_redirect('https://example.com/private-page/'.$slug."https://wordpress.stackexchange.com/");
exit;
}
Aside from that, a couple other things to check:
- You say the code is in
function.php
; if you’re referring to a theme file, it needs to befunctions.php
(note thes
) in the currently-active theme. - Where are these functions being called from? What action/filter hook are you adding them to?
Related Posts:
- Logout and redirect using the WP Menu
- Mega Menu Walker
- Displaying Logged-In User Name in WordPress Menu
- Second Navigation inside header
- How can I add a custom class to only nav sub-menu > li > a items in WordPress?
- items_wrap not working
- Style wp_page_menu() items
- Change Navigation Bar based on logged in or not
- Multiple WordPress Menus that will only display all pages
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- Menu Limitations
- Custom Walker_Nav_Menu issue with variables on PHP 7.3
- Detecting classes, adding widgets, and adding divs in with a Nav Walker
- How to style one item from main navigation?
- Add login hyperlink to secondary navigation menu
- Menu Custom Data Attributes
- How to modify mobile nav menu text in theme
- Nav-Menu not showing up
- Custom navigation menu with awsAccordion
- Why can’t I add a custom image in my navigation?
- Remove the Tag from wp_nav_menu
- How to set Nav as a default menu in wordpress
- How to create a sub-page of sub-page?
- Add the “.current-menu-item” class to tag?
- Nav Menu Display
- Open WordPress Page from selected option dropdown
- Custom Navigation Bar JSON Syntax Error in JSON at Position 0
- Nav menù doesn’t display selected pages
- how can i redirect dynamic URL in worpress base on ID in htaccess or wp-config or function.php
- Replace jQuery with PHP or CSS for Customized WordPress wp_nav_menu
- Implementing a multilingual WordPress site by installing several instances of WordPress and redirecting
- WordPress on Ubuntu – “File not Found” for home.php – Trying to Redirect to Root Folder
- dynamic php menu with hidden sub-categories only showing when the category name is clicked
- I want to show cart items number if any or nothing, simple CART text
- Remove all feeds and return custom 404 page
- How to put 2 php codes in functions.php without site crashing
- Infinite loop when logging out using custom login form
- Menu not updating for logged in users after redirect
- wp_get_nav_menu_items() with ACF
- How to switch between two primary menus (programatically0
- How to let mobile navigation menu close when link is clicked?
- Used a code to revoke some dashboard menus for my contributors but for some reason it’s revoked me access to the editor [closed]
- Get Child of Child Pages in custom Menu
- WordPress: Redirect Main Site to Subsite in Multisite Where user is NOT logged in
- How to give custom roles the capability to edit one Menu instead of every Menu
- Primary navigation menu with timed items from custom post type
- PHP warning with code for nav menu walker [duplicate]
- Add element after navigation element title don’t works like I expect
- How to disable sub menu items from being created?
- String replace for Login/Logout concatenation problem in menu
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- Bring Font Awesome icons inside menu A tags
- Difference between “Walker_Nav_menu” and “walker”
- Add parent menu item in sub-menu in custom nav_walker
- wp-login.php?redirect_to=https problem
- ERR_TOO_MANY_REDIRECTS / To Force SSL Logins and SSL Admin Access
- WordPress Redirect / Add_Rewrite_Rule – Non Index.php Page
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Vertical Menu for one page only
- Display specific main Sub Nav on Woocommerce product pages
- PHP drop down menu for my get_category child of
- Override 404 when navigating to single php file
- Widget Logic – display on page and all child
- Submit to itself don’t work
- How limit the width of a fixed menu-bar on scroll?
- Pulling a variable into the wp_nav_menu function
- index.php navigation
- Beyond widget side menu editing, with the php page, custom template
- How to list categories by page id in wordpress
- Adding button to wordpress menu
- redirect 301 with special character like WIX site “#!”
- 301 redirect from webpage to wordpress page in the same root
- Editing existing pre-created menus in PHP
- Two theme locations for two menus, but only one is showing up
- Submitting my form to the database and then redirecting to payment site
- wp_nav_menu doesn’t work in a duplicate server
- How I can add div to menu?
- wp_nav_menu prints children with parent name
- wp nav menu without label
- Contact form field in wordpress menu
- WP Admin Bar – Get current theme name as custom menu title
- Add extra markup to WP menus
- How to call a certain object/menubar in a PHP file
- Active class not working on custom menu link using add_menu_page
- Sidebar current menu item for parent
- Using ‘array’ method in wp_nav_menu causes it to disappear
- Remove all nav menu classes except those which begin with certain letters
- How to add a code for lastpostdate after/inside a specific menu link?
- How to get the count for each taxonomy term
- 3 domains, 1 wordpress install, redirecting and changing domains on live site
- WP_Nav Highlight Parent Issue
- WordPress adding in site URL to header links
- How to change the menu color
- Can’t remove menu items added by plugin
- Custom Links in Walker Class
- Submenu opened problem
- if is_home(), change class of menu-item-225 of wp_nav_menu
- Adding a unique class to wp_nav_menu
- Get permalink for a post from inside WordPress and route to a related site
- Change order that the menu is generated with wp_list_pages