S
S
Soniked2022-01-27 09:59:03
WordPress
Soniked, 2022-01-27 09:59:03

How to create a simple link handling rule in Wordpress?

Good afternoon
I have a simple one page landing page in Wordpress.
Landing page navigation is done using anchors #
I need to make Wordpress treat the transition to a specific page as a transition to an anchor.
That is, if the user enters the site.ru/contact page, then the site.ru/index.php#contact page is shown to him
Without redirects and without changing the URL - so that the user thinks that he is on the site.ru/contact page
From the documentation According to Wordpress, it's not at all clear to me how this can be achieved.
Options I have tried:

add_action( 'init', 'custom_rewrite_basic' );
function custom_rewrite_basic() {
  add_rewrite_rule( '^(contact)', 'index.php#$matches[1]', 'top' );
}

add_action( 'init', 'custom_rewrite_basic' );
function custom_rewrite_basic() {
  add_rewrite_rule( '^(/contact)', 'index.php#$matches[1]', 'top' );
}

add_action( 'init', 'custom_rewrite_basic' );
function custom_rewrite_basic() {
  add_rewrite_rule( '^(index.php)/([^&]+)', 'index.php#$matches[2]', 'top' );
}

None gave results. The second option throws you to the main page when you go to the / contact page.

I would be grateful if you could help with advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2022-01-27
@init0

You cannot receive on the server side the uri fragment (the hash symbol and everything after it) and rewrite it accordingly. This can only be done on the client side, in JS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question