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 do I make a redirect in PHP?
- Adding first / last CSS classes to menus
- How can I limit the length of the previous/next posts in my WordPress Theme?
- how to put the logo in the center of other items in the navigation bar
- renaming an admin menu item with decimal array index number
- How to remove a metabox from menu editor page?
- How to redirect users to custom lostpassword page?
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Can we have a php “page” without a WordPress “page”?
- wp_delete_auto_drafts() deletes links in menus
- Replacing the NavWalker dropdown element
- How to add specific menu on a specific page or pages
- Unusable menus and “Illegal widget setting ID: nav_menu_item[]” error
- WordPress menu deletes when trying to add a hook
- Creating new menu item
- Programmatically create menu with custom link
- Custom Loop through category menu to include sub categories
- selected menu item
- Add itemprop Schema.org Markup to li Elements in wp_nav_menu
- Show comments menu in dashboard only if the site has comment
- Trouble creating conditional PHP for nav menu items with children for custom Walker
- Woocommerce redirect thankyou page based on product ID doesn’t empty Cart
- Count top level menu items
- WordPress does not load page.php, return 404.php
- create a select input with menus created on a custom options page
- How to add an arrow to menu items has submenus
- wp_nav_menu returns menu list in ascending order. How can I arrange the menu same as dashboard menu
- Secondary navigation menu on one page
- Remove class in nav_menu_link_attibutes filter
- WordPress Quick Question . How to Get Parent Link in Submenu in My Code
- Add item to top of menu using a filter in functions.php
- Adding PHP in the menu
- Having Issue on Redirecting With Session in WordPress
- Automatically add images to a menu
- Login redirect. Check user meta and redirect accordingly
- Where to insert redirect code based on http_referer?
- Is this a correct usage of ob_start() in my WordPress project?
- How to allow download url redirection only if user logged in WordPress site?
- How can I use custom menus with a Bootstrap WordPress theme?
- Adding Additional Variables on Menus Page
- Dynamic Menu drops pages?
- Class for Selected Menu using wp_nav_menu
- How to create Loop code for Menu using WordPress?
- WP insert post Redirect after function has executed
- How to redirect non logged in mobile users to page on same site?
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- Download file and redirect to page or vice versa
- In PHP how do I make my navigation bar show certain links to admins only?
- Featured image in menu wordpres
- how can i redirect dynamic URL in worpress base on ID in htaccess or wp-config or function.php
- WordPress on Ubuntu – “File not Found” for home.php – Trying to Redirect to Root Folder
- I want to show cart items number if any or nothing, simple CART text
- How to let mobile navigation menu close when link is clicked?
- PHP warning with code for nav menu walker [duplicate]
- Add element after navigation element title don’t works like I expect
- 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)
- Widget Logic – display on page and all child
- Submit to itself don’t work
- Adding button to wordpress menu
- redirect 301 with special character like WIX site “#!”
- How I can add div to menu?
- wp_nav_menu prints children with parent name
- WP Admin Bar – Get current theme name as custom menu title
- Using ‘array’ method in wp_nav_menu causes it to disappear
- How to get the count for each taxonomy term
- 3 domains, 1 wordpress install, redirecting and changing domains on live site
- Submenu opened problem
- How to create a WordPress PAGE in another folder?
- Redirect loop upon installation of my plugin
- wp_login_url always redirects me to wp-admin