Page scroll to id for WordPress
Knowledge Base – FAQ
-
No, the plugin works strictly on document's root element (html/body) in order to function properly with element highlighting, deep-linking etc.
-
A quick way is to enable Keep the current element highlighted until the next one comes into view option
Alternatively, you could use the wrapper target shortcode to create your id targets and have full control over the scrolling distance your link(s) stay highlighted.
-
Make sure your target element is at the top by checking its position via browser's developer tools (in most browser's, you can right-click the element and select "Inspect" to view it within the HTML code).
If your link's URL is#top
and no such target id exist in your page, the plugin will scroll the page automatically to the very top (determined by the position of thebody
tag in the HTML). -
The page should scroll to the top (i.e. starting) position of the target, so firstly make sure that your target(s) position is indeed correct (or what you want it to be). Note that the top position of elements does not include CSS margins, so check if you need to adjust any of your target's CSS properties.
In general, you can adjust/tweak the scroll-to position by changing your target(s) position or by using plugin's "Offset" option.
For example, if your page has a top sticky/fixed-positioned menu, you should set an offset value in order to avoid the menu covering your content when the page is scrolled. -
Add the full address as your links URL/href value instead of just the hash with the id. For example, instead of
#my-id
, usehttp://my-site.com/page/#my-id
or/page/#my-id
etc. in the link's URL. -
The plugin has the options to scroll from/to different pages enabled by default (if not, enable it), so you only need create links with the full address in their URL (e.g.
http://my-site.com/page/#my-id
). -
You should use plugin's "Offset" option and set how much you want to offset page scroll (see option for more info).
-
Yes, via Disable plugin below option. You can set a
width
orwidth,height
value (indicating pixels) below which the plugin will be disabled (see option for more info). -
It does. You can take a look at the jQuery/non-wordpress version of the plugin for available options, methods etc. The plugin script offers things like changing options on-the-fly, triggering scroll events programmatically, running callback functions at the beginning and end of each scroll etc., so contact me if you need help 🙂
-
Yes, set different vertical and horizontal offsets by adding comma separated values in plugin's "Offset" option field.
For instance, inserting100,50
will offset vertical scrolling by 100 pixels and horizontal scrolling by 50 pixels). -
Yes. If you're using plugin's "Insert/edit Page scroll to id link" button in WordPress visual editor, you can set a link-specific offset value in the "Offset" field of the "Page scroll to id" modal.
If you're using plugin's shortcode, you can use the offset attribute like so:
[ps2id url='#some-id' offset='100']link text[/ps2id]
If you create your links manually in HTML, you can use the
data-ps2id-offset
attribute like so:<a href="#some-id" rel="m_PageScroll2id" data-ps2id-offset="100">link text</a>
For WordPress menu items, you can use the class
ps2id-offset-NUMBER
in the menu item Classes field, replacingNUMBER
with the offset you want for that specific menu link. For example, for 80px offset, add the classps2id-offset-80
(feature added in plugin's version 1.7.4). -
Firstly, you need to determine if highlighting comes from "Page scroll to id" or by your WordPress theme. If you didn't use any of the plugin's highlight classes in your CSS, then the highlighting you see comes from your theme's stylesheet. It's common for themes to style current page menu items differently. If that's the case, you should edit (or overwrite) your theme's CSS.
If you did use plugin's
mPS2id-highlight
class in your CSS, then you can try any of the following:- Enable Allow only one highlighted element at a time option in plugin settings.
- Instead of
.mPS2id-highlight
, use.mPS2id-highlight-first
or.mPS2id-highlight-last
selectors in your CSS to style the first or last highlighted link accordingly.
-
- Create your menu items/links on your menu (Appearance → Menus). You should create/add "Custom links" as these type of links allow us to enter a custom URL with the id we want. Your links URL could be in the form of
#my-id
,http://my-site.com/page/#my-id
,/page/#my-id
etc. - Create the id targets that your links will scroll to, using plugin's "Insert Page scroll to id target" button in WordPress visual editor or plugin's shortcodes.
Another option is to use id values that already exist in your theme. - The plugin is enabled by default on WordPress Menu links so normally everything should work.
Some themes have their own custom function for generating menus which might prevent the "Enable on WordPress Menu links" option from working. In such case or if for any reason you need to enable the plugin on your links manually, do the following:- Go to Appearance → Menus to edit your links.
- Click "Screen Options" at the very top and make sure "CSS Classes" is checked.
- Click to edit each one of the links you've created and insert the
ps2id
value in "CSS Classes" field. - Save your menu. Any menu item/link with
ps2id
class will be handled by the plugin.
.menu-item a[href*='#']
, so try adding this value in "Selector(s)".
- Create your menu items/links on your menu (Appearance → Menus). You should create/add "Custom links" as these type of links allow us to enter a custom URL with the id we want. Your links URL could be in the form of
-
You basically need to see the generated HTML code of your page(s) and search for
id="some-id"
patterns, e.g.<div id="some-id">...</div>
The best way to do this is to use browser's developer tools (Chrome and Firefox are browsers with great developer tools), so the steps are as follows:- Open your page in say Google Chrome.
- Hit the F12 key or right-click any element a click "Inspect" to open the developer tools window.
- Switch to "Elements" panel (if necessary) where you can see the page's HTML code (in real-time).
In this panel you can toggle each HTML tag and hovering over each one, highlights it in browser's main window (where you can conveniently see its actual position within the page/document). The tags/elements you should look for are within thebody
tag.
You can also right-click any element in the main window (e.g. a title, paragraph, section, image etc.) and click "Inspect" to highlight it in the "Elements" panel. There you can see if the inspected element has an id attribute value and use it as your link(s) target.
-
It is possible only if you can add custom javascript in your template/theme. See "Page scroll to id with mousewheel and keyboard" guide.
-
You can change the scroll duration/speed of a link by giving it a special class in the form of
ps2id-speed-VALUE
(i.e.ps2id-speed-600
) withVALUE
indicating the scroll duration in milliseconds (lower duration equals faster scrolling).If you're using plugin's "Insert/edit Page scroll to id link" button in WordPress visual editor, you can add the special class in the "Custom class(es)" field of the "Page scroll to id" modal.
If you're using plugin's shortcode, you can use the class attribute
-
Yes. Add
define('PS2ID_MINIFIED_JS', false);
in yourwp-config.php
file to instruct the plugin to load the non-minified/development version of its scripts. -
Image map links (
area
elements) work the same as normal links (anchors). See this comment and this thread. -
Some themes and plugins change document's length while the page is scrolling. A common example would be when a menu becomes sticky (i.e. fixed) only when the user has scrolled down the page. There are cases where when this happens, the menu's previous state (when it was not sticky) space becomes zero, thus changing the document/page length. This means that targets position is different according to the state of the menu, that's why when clicking the link a second time, scrolling seem to become "correct".
You can deal with this either by adjusting your menu CSS (not always possible) or by enabling Verify target position and readjust scrolling (if necessary), after scrolling animation is complete option in plugin's settings.
-
You can overwrite some of plugin's option via javascript. For example, you could add the following script in your theme's header.php inside the head tag:
window.ps2id_special_params={ highlightSelector: "a.some-class" };
You can use the following option parameters:
highlightSelector
,scrollEasing
,scrollSpeed
,keepHighlightUntilNext
,forceSingleHighlight
,appendHash
,layout
,offset
You can also do the same by adding the following in your theme/child-theme functions.php:
add_action( 'wp_enqueue_scripts', 'ps2id_special_params_0', 1 ); function ps2id_special_params_0(){ if(class_exists('malihuPageScroll2id')){ $pages_arr = ps2id_mw_get_pages(); if(is_single( 23 )) : wp_register_script( 'page-scroll-to-id-special-js-params', '', array(), '0.0.1', false ); wp_enqueue_script( 'page-scroll-to-id-special-js-params' ); wp_add_inline_script( 'page-scroll-to-id-special-js-params', 'window.ps2id_special_params={ highlightSelector: "", scrollEasing: "easeOutCirc", scrollSpeed: 700, keepHighlightUntilNext: false, forceSingleHighlight: false, appendHash: false, layout: "auto", offset: 100 };'); endif; } }
The function above will add the script on the post with id 23 (see:
is_single( 23 )
). -
In plugin settings change the "Selecotr(s)" option value from
a[href*=#]:not([href=#]):not([data-vc-tabs]):not([data-vc-accordion])
to the defaulta[href*='#']:not([href='#'])
The issue is related to this topic. -
Add the class
ps2id-auto-scroll
to a target element. When the page is loaded, it'll scroll automatically to the target element with that specific class. -
It seems that TranslatePress adds a trailing slash to the translated menu links URLs and breaks all anchor links (regardless of using the plugin or not). Please see this thread for all possible solutions including the following filter in functions.php:
add_filter('trp_get_url_for_language', 'trpc_remove_end_slash', 10, 6); function trpc_remove_end_slash($new_url, $url, $language, $abs_home, $lang_from_url_string, $lang_url_slug){ return untrailingslashit($new_url); }
Pages: 1 2
Hello, i set up the delay enough long to read the title of the page, but i wanted to know if i can create a link ( back previous page ) with a different delay in the link ? to come back faster back in the id back?
Thank you
Hello,
There’s no way to add a different delay on a specific link. I don’t know exactly what you need but I’m guessing going back to some other id on a previous page (correct?) but unfortunately there’s no way to do this (at least not without developing a custom js script with a bit of PHP).
Hi mate,
I have installed your plugin on this website. Is it possible for the menu to automatically close on mobile devices when you click on an option?
The website is the below : https://e-aligners.gr/
Έλα φίλε μου,
Δες την απάντηση μου στο φόρουμ:
https://wordpress.org/support/topic/mobile-menu-263/#post-17860729
Hi
I am building a website that uses the html-anchor as an ID a CSS :target Selector.
I need your help to disable the jump to section feature so the user doesn’t get surprised when they click the button
The particular page in my website is board of directors page, when you click the image, a description text overlay appears using a fade transition.
My goal is to make the user comfortable and can see the description clearly.
The problem is because it uses the html anchor, it jumps to the section thus making jerky and sudden movements that made the user uncomfortable
I have tried to exclude the id, but still it jumps to the section
Do you have any advice or solution?
Thank you
Worm regards,
Erdafa Andikri
Hello,
I’d need to see your site/page in order to help. Can you post or send me the URL?
unfortunately i cant give you the url because the website is still at my localhost.
i will give you a youtube video that give you an understanding of my problem
https://youtu.be/abF2PiWG9Es
Have you activated “Page scroll to id” plugin? Is the “+” button an actual link with an
href
value?yes i already turned it on.
to be clear, i want to disable it.
i dont want it to jump to section.
I don’t think it’s possible to stop a link from jumping to its hash (you can test this by deactivating the plugin and see that it still jumps to the hash target), unless you add custom javascript. This is because jumping to hash is a standard browser behavior.
The thing is that unless I see your page/code I cannot provide what js code to add. At the very least you’ll need to tell me the class(es) of the “+” element (and its parent element) and maybe we can try adding some code.
Keep in mind that adding the code might still not work as it’ll depend on how your theme devs created the button functionality.
By the way, what clicking the “+” button supposed to do?
https://youtu.be/TfhuK0y9Hmw
clicking the + button shows the description of the member, i used css to achieve this
i think our conversation ends here, i will try to figure it out myself.
really appericiate your help
thank you,
Erdafa Andikri
No problem 🙂 Let me know if I can help.