Using a Menu Button to Redirect to Another Website in WordPress
In case you’re wondering, the green button with the white button on it is not a glitch. It is, in fact, a feature. The WordPress developers decided that instead of forcing you to look at each post’s full content when you hit the “read more” button, they would rather you’d get redirected to another site. That way, you’ll be able to read the news story, learn about the featured product or service, or check out some juicy blog posts without worrying about running out of content.
You’ve got two options when it comes to redirecting from your WordPress site. You can use a WordPress plugin, or you can build a quick and dirty custom solution using a couple of lines of code. Let’s take a look at how you might choose to implement the latter.
The Basic Approach
This method is pretty much self-explanatory. You simply need to add a couple of lines to your WordPress functions.php file. This is the file that controls most of the site’s functionality. The lines of code you add will tell WordPress to redirect any page it thinks is an eligible redirect to a given URL. Here’s what you need to do:
Find the wp_redirect() function in the file and scroll down until you see this function is not documented. (this is referring to the wp_redirect() function.)
Add a couple of lines of code (just a couple) to the bottom of this function. The code you type will tell WordPress what to do when a user tries to visit a page that it thinks is an eligible redirect. Here’s what the code should look like:
add_action( ‘wp_redirect’,’redirect_to_another_site’ );
The first thing you’ll need to do is register the action in a WordPress plugin named redirect_to_another_site. Next, tell WordPress where to redirect your users. You do this using the wp_redirect() function. To make things a little easier to follow, let’s first set up a couple of constants. Create a new constants file in your /wp-content/plugin/redirect-to-another-site/constants directory and inside that file, add the following constants:
/** * REDIRECT_URL: This is the URL you want your users to be redirected to when they click a link, visit a page, or run a search and end up on a page you think is eligible for redirect. (note: The forward slash is important.)* / * REQUEST_FILENAME: This is the name of the file your users will be redirected to when they click a link, visit a page, or run a search and end up on a page you think is eligible for redirect. (note: Ensure this is the correct directory and filename)
Next, you’ll want to add a couple of lines of code to the wp_redirect() function in the file. Add the following code:
if (! defined( ‘REDIRECT_URL’ ) ) define( ‘REDIRECT_URL’, $_SERVER[ ‘REDIRECT_URL’ ] );
if (! defined( ‘REQUEST_FILENAME’ ) ) define( ‘REQUEST_FILENAME’, $_SERVER[ ‘REQUEST_FILENAME’ ] );
The code above simply checks to see if the defined constants are set. If they are not, it will use the values from the environment variables instead. In either case, the wp_redirect() function will now check to see if the user is trying to reach an eligible page. If so, WordPress will do the rest and redirect the user to the given URL.
A Better Approach
This method removes the necessity of hardcoding URLs in your functions.php file. With this approach, you’ll create a new file named redirect.php in your /wp-content/plugins/redirect-to-another-site directory. In this file, you’ll define three constants that will act as placeholders for the values you need. Once those three constants are set, you can simply call a single function to perform the actual redirection. Here’s what the file should look like:
/** * REDIRECT_URL: This is the URL you want your users to be redirected to when they click a link, visit a page, or run a search and end up on a page you think is eligible for redirect. (note: The forward slash is important.)* / * REQUEST_FILENAME: This is the name of the file your users will be redirected to when they click a link, visit a page, or run a search and end up on a page you think is eligible for redirect. (note: Ensure this is the correct directory and filename)
* THREE: This is the placeholder for the third constant you’ll need to set before you can call the function.
if (! defined( ‘REDIRECT_URL’ ) ) define( ‘REDIRECT_URL’, $_SERVER[ ‘REDIRECT_URL’ ] );
if (! defined( ‘REQUEST_FILENAME’ ) ) define( ‘REQUEST_FILENAME’, $_SERVER[ ‘REQUEST_FILENAME’ ] );
if (! defined( ‘THREE’ ) ) define( ‘THREE’, 3 );
The above code simply checks to see if the defined constants are set. If they are not, it will use the values from the environment variables instead. Next, you’ll want to add a function to the /wp-content/plugins/redirect-to-another-site/functions directory named redirect_to_placeholder(). You can simply copy and paste the code below into that file:
function redirect_to_placeholder() { global $wpdb; $query = $wpdb->get_results( “SELECT * FROM wp_postmeta WHERE meta_key = ‘_wplink’ AND meta_value!= ” ” ); if ( $query ) { while ( $row = $wpdb->fetch_row( $query ) ) { if (!is_page( $row[0] ) ) { $link = home_url( $row[0] ); break; } } } else { $link = “http://google.com”; break; } wp_redirect( $link ); }
The code above retrieves all of the placeholders and uses them to build a redirect URL based on the user’s input. In our earlier example, the URL would look like http://mysite.com/wp-content/plugins/redirect-to-another-site/functions/redirect_to_placeholder?Redirect_URL=&THREE=3. Once built, you can redirect users anywhere you like by simply calling the function.
As you can see, there’s a lot that can be said about redirecting from your WordPress site. With a bit of research, planning, and testing, you can do a lot to make your site more usable and accessable to your users. Whether you choose to redirect to another site or have a different method in mind, take a little bit of this and a little bit of that, and you’ve got yourself a working solution.