0

I have searched plugins, tutorials and forums but I can't find a solution to what I need.

For example, I have 2 posts with different content, which are within url-1 and url-2. What I need is that url-1 shows the content that url-2 has at the permanent link level, I don't want internal functions that have to do with the $post variable or the get_post function Since I use plugin cache, these functions would not work, it would have to be at the permanent link URL level or url-rewriting.

the content of url-2 should be shown in url-1.

Graphically:

url-1 = content 1

url-2 = content 2

Now what I want is that when they enter url-1, content 2 is displayed without affecting the url

2
  • cache systems shouldn't interfere in your site functionalities. may be there is a configuration issue, edit your question to give us more details about the cache you use on your site.
    – mmm
    Commented Sep 19 at 16:13
  • @mmm I'm going to edit my question, but what I mean is that displaying the content of another page should not depend on whether the page is static by cache, what I want is to display the page without moving or affecting the url-1
    – LΞИIИ
    Commented Sep 21 at 12:33

1 Answer 1

0

In order to achieve this URL-level content display in WordPress without using internal functions that depend on the $post variable or get_post, we can make use of the template_redirect action along with URL rewriting.

  1. Add Rewrite Rules.
  2. Handle the Redirect.

Here’s an example of how to implement this:

// Here we have added rewrite rules.
function custom_rewrite_rules() {
    add_rewrite_rule( '^url-1/?$', 'index.php?pagename=url-2', 'top' );
}
add_action( 'init', 'custom_rewrite_rules' );

// This is to Serve content of url-2 when accessing url-1.
function load_template_for_url_1( $template ) {
    if ( is_page( 'url-1' ) ) {
        // Here we are getting the template for url-2.
        $new_template = locate_template( 'page-url-2.php' ); // We need to adjust based on your template file.
        if ( $new_template ) {
            return $new_template;
        }
    }
    return $template;
}
add_filter( 'template_include', 'load_template_for_url_1' );

// This is to flush rewrite rules on activation
function flush_rewrite_on_activation() {
    custom_rewrite_rules();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__ , 'flush_rewrite_on_activation' );

Once we have done the above steps we need to replace url-1 and url-2 with the actual slugs of your posts. After adding this code, we may need to visit the WordPress Settings > Permalinks page to flush the rewrite rules manually. After this we need to ensure that your caching plugin is configured to allow for this type of redirection.

5
  • The detail is that I don't need a 301 redirect, that is, it should not be seen as a redirect because this is detectable. I need it to be natural without a redirect.
    – LΞИIИ
    Commented Sep 21 at 12:25
  • For this we need to modify the the code, I have done the same could you please try with updated code. This time we will use template_include hook instead of template_redirect which will tackle redirect issue. Commented Sep 21 at 12:29
  • the custom_rewrite_rules(); and flush_rewrite_rules(); function can be used in the admin_init hook or in an init? since I see that this will work if this is activated as a plugin because register_activation_hook is being used. What happens is that I would like that with an option any URL can be changed the content but without activating or deactivating the plugin with register_activation_hook
    – LΞИIИ
    Commented Sep 21 at 12:42
  • Yes, you can use this with init or admin_init hook. Commented Sep 21 at 12:45
  • It works but the top URL is affected, as I mentioned in the post, content 2 should be displayed but without affecting the URL. I made a video for you to see ▶️ ibb.co/9sTGRWy
    – LΞИIИ
    Commented Sep 21 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.