Add `authorBase` option to WP Source package

Description

There is an option to set a custom structure for permalinks in WordPress’ admin panel (Settings > Permalinks > Common Settings). This structure not only affects to posts but also categories, tags and authors. The only type of entity that is not affected are pages.

Screenshot from 2020-04-22 18-07-29

For example, when using the custom structure in the image, author URLs whould be something like /blog/author/admin, thus breaking the current implementation for the author handler.

To solve this, the idea is to add a new setting in this page like the ones we already have to change the category and tags base link.

UserStories

As a Frontity user
I want to change authors link base in the configuration
so that I can make Frontity work with custom permalinks for authors.

Possible solution

Initial proposed solution (click to open)

state.source.authorBase

This could be solve using the same implementation we did for categoryBase and tagBase. We just have to add an authorBase option in state.source:

https://github.com/frontity/frontity/blob/c5a2e15d68a1e4c2060e32808688314725001038/packages/wp-source/src/state.ts#L39-L40

and handle that option the same way we are doing for the others already mentioned:

https://github.com/frontity/frontity/blob/c5a2e15d68a1e4c2060e32808688314725001038/packages/wp-source/src/actions.ts#L177-L211

Final implementation


Final implementation

The solution was the same used for categoryBase and tagBase: to create a redirection in the init action based on a setting in state.source called authorBase.

const WpSource = {
  actions: {
    init: ({ state }) => {
      const { authorBase } = state.source;
      if (authorBase) {
        // Add new direction.
        const pattern = concatPath(subdirectory, authorBase, "/:subpath+");
        redirections.push({
          name: "author base",
          priority: 10,
          pattern,
          func: ({ subpath }) => `/author/${subpath}/`,
        });
        // Remove old direction.
        redirections.push({
          name: "author base (reverse)",
          priority: 10,
          pattern: concatPath(subdirectory, "/author/(.*)/"),
          func: () => "",
        });
      }
    },
  },
};
2 Likes