Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Add shortcode option to allow widgets to be excluded from the generat…
Browse files Browse the repository at this point in the history
…ed anchor links list on the maintenance page, update documentation
  • Loading branch information
dotherightthing committed Jun 20, 2021
1 parent faf320e commit 2815d17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ In a PHP template, as a template tag:
?>
```

### Inject an HTML string below the list

Add the following parameter to the shortcode:

```php
# HTML to appear in .wpdtrt-anchorlinks__additions
additional_html='<div>Some HTML</div>'
```

### Inject widget titles into the list, from a widget sidebar which resides outside of the_content

Add the following parameters to the shortcode:

```php
# Sidebar ID to source widgets from
additional_from_sidebar_id_1='content-top'

# DOM order of sidebar relative to the_content (0)
# A negative value means that the sidebar appears before the_content.
additional_from_sidebar_order_1='-1'

# Exclude widgets titles from e.g. the maintenance page
# Comma separated list of page IDs
exclude_widgets_on_pages='12345, 67890'
```

### Control the dynamic pinning of the anchor links

Pinning keeps the navigation in view, while the rest of the page content is scrolled.
Expand All @@ -42,13 +68,12 @@ Dynamic content includes:

* replacement of the anchor links list title with the summary heading
* highlighting of the anchor link corresponding to the content section currently in view
* injection of theme elements below the anchor links list

Dynamic content is implemented using Intersection Observers (requires JavaScript / modern browser or MS Edge 15+).

Pinning toggles a class of `.wpdtrt-anchorlinks--sticky` on `.wpdtrt-anchorlinks`.

To control dynamic content, add the following data attribute to an element:
To control dynamic content, add the following attribute to the shortcode element:

```html
data-anchorlinks-controls="pinning"
Expand All @@ -70,26 +95,6 @@ data-anchorlinks-controls="highlighting"
* the previous matching anchor link will be unhighlighted
* the new matching anchor link will be highlighted

### Inject a theme element after the anchor list

Add the following data attribute to the element:

```html
data-anchorlinks-list-addition-clone="false"
data-anchorlinks-list-addition="1"
```

* This element will be removed from its current location and injected after the list
* If there are multiple elements to inject, this element will be injected first, as it has an id of `1`

```html
data-anchorlinks-list-addition-clone="true"
data-anchorlinks-list-addition="2"
```

* This element will stay at its current location and a copy (clone) will be injected after the list
* If there are multiple elements to inject, this element will be injected second, as it has an id of `2`

### Styling

Core CSS properties may be overwritten by changing the variable values in your theme stylesheet.
Expand Down
20 changes: 14 additions & 6 deletions template-parts/wpdtrt-anchorlinks/content-anchorlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$after_widget = null; // register_sidebar.

// shortcode options.
$exclude_widgets_on_pages = null;
$post_id = null; // $post->ID stand-in for unit tests
$title_text = null;
$additional_html = null;
Expand All @@ -38,6 +39,10 @@
$additional_from_sidebar_id_1 = null;
}

if ( isset( $exclude_widgets_on_pages ) && ( '' !== $exclude_widgets_on_pages ) ) {
$exclude_widgets_on_page_ids = explode( ',', $exclude_widgets_on_pages );
}

global $post;

if ( isset( $post ) && is_object( $post ) ) {
Expand All @@ -56,12 +61,15 @@

foreach ( $sidebars_widgets as $sidebars_widget ) {
if ( isset( $wp_registered_widgets[ $sidebars_widget ]['name'] ) ) {
$name = $wp_registered_widgets[ $sidebars_widget ]['name'];

array_push( $new_anchors, array(
$name . '#',
sanitize_title( $name ),
) );
$name = $wp_registered_widgets[ $sidebars_widget ]['name'];
$post_id = strval( $post->ID );

if ( ! in_array( $post_id, $exclude_widgets_on_page_ids, true ) ) {
array_push( $new_anchors, array(
$name . '#',
sanitize_title( $name ),
) );
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion wpdtrt-anchorlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,17 @@ function wpdtrt_anchorlinks_plugin_init() {
'tip' => __( 'e.g. Outline', 'wpdtrt-anchorlinks' ),
'default' => __( 'Outline', 'wpdtrt-anchorlinks' ),
),
'exclude_widgets_on_pages' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Exclude widgets from anchor links list on these pages', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Comma separated list of page IDs', 'wpdtrt-anchorlinks' ),
),
'additional_html' => array(
'type' => 'text',
'size' => 30,
'label' => __( 'Additional HTML', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Output in .wpdtrt-anchorlinks__additions', 'wpdtrt-anchorlinks' ),
'tip' => __( 'Output an HTML string below the list', 'wpdtrt-anchorlinks' ),
),
'additional_from_sidebar_id_1' => array(
'type' => 'text',
Expand Down Expand Up @@ -336,6 +342,7 @@ function wpdtrt_anchorlinks_shortcode_init() {
'selected_instance_options' => array(
'post_id',
'title_text',
'exclude_widgets_on_pages',
'additional_html',
'additional_from_sidebar_id_1',
'additional_from_sidebar_order_1',
Expand Down

0 comments on commit 2815d17

Please sign in to comment.