Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hardcoded navigation menus to use the correct site_url() rather than original value #22

Closed
bobbingwide opened this issue Nov 5, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@bobbingwide
Copy link
Owner

bobbingwide commented Nov 5, 2020

I've been creating the theme in s.b/wp56 so the url attribute in the navigation-link blocks is of the form https://s.b/wp56/permalink/

Here's the current footer menu.

<!-- wp:navigation {"orientation":"horizontal","itemsJustification":"center"} -->
<!-- wp:navigation-link {"label":"Blog page","url":"https://s.b/wp56/blog-page/"} /-->

<!-- wp:navigation-link {"label":"home-page","url":"https://s.b/wp56/"} /-->

<!-- wp:navigation-link {"label":"Trying to build Fizzie","url":"https://s.b/wp56/2020/10/trying-to-build-fizzie/"} /-->
<!-- /wp:navigation -->

When I implement the theme on blocks.wp-a2z, blocks.wp-a2z.org and other subdomains I'll want the menu items to point to the current site. I want the correct site url = as provided by site_url().

Proposed solution

In the absence of a solution delivered by Gutenberg, intercept the server side rendering using the same method as documented in #18.

eg

function fizzie_render_block_core_navigation_link( $attributes, $content, $block ) {
    $attributes = fizzie_nav_atts( $attributes );
    $html = gutenberg_render_block_core_navigation_link($attributes, $content, $block);
    return $html;
}

function fizzie_nav_atts( $attributes ) {
    $attributes['url'] = str_replace( 'https://s.b/wp56', site_url(), $attributes['url'] );
    return $attributes;
}

And while we're at it....
Apparently the logic already supports current-menu-item - see
WordPress/gutenberg#25575
so let's see if we can ensure that the current-menu-item class is set.

Looking at the current code, we need to set $attributes['id'] to the value of get_the_ID()
when the current URL is the same as the link's URL.

$is_active   = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] );
```
@bobbingwide bobbingwide added the enhancement New feature or request label Nov 5, 2020
@bobbingwide bobbingwide self-assigned this Nov 5, 2020
@bobbingwide
Copy link
Owner Author

The logic that uses get_the_ID() only works for singular content.

In wp_nav_menus(), WordPress uses a far more complicated function called _wp_menu_item_classes_by_context().
We can implement a much simpler function, currently called fizzie_fiddle_nav_atts().

In order to support setting the CSS class names for current menu item's ancestors I'll have to implement this logic for core/navigation rather than core/navigation-link.

@bobbingwide
Copy link
Owner Author

Fizzie v1.0.0 continues to apply overrides to the core/navigation-link block to replace hardcoded https://s.b/wp56 with site_url()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant