Coder Social home page Coder Social logo

sarutole / modern-primary-category Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 2.01 MB

Gutenberg-friendly primary category plugin: choose a category to use in post's permalink. Works with CPTs and taxonomies.

License: MIT License

JavaScript 49.16% CSS 2.01% PHP 48.83%
post-permalink gutenberg custom-taxonomies wordpress-plugin primary-category

modern-primary-category's Introduction

Modern Primary Category

Gutenberg-friendly primary category plugin -- the easiest way to choose a category to be used in post's permalink.

Features

  • Enables taxonomy placeholders in permalinks;
  • Supports custom post types and custom taxonomies;
  • Allows multiple taxonomies in post's permalink.

The underlined term that has a red flag by its side is the primary permalink

Limitations

  • Works only on hierarchical taxonomies (i.e. does not work on tags);
  • No user interface for Classical Editor.

Instructions

Support for Custom Post Types

To have Gutenberg/Block Editor enabled for your CPTs, provide the following values when calling register_post_type():

	...
	'supports'              => [ 'custom-fields' ],
	'show_in_rest'          => true,
	'rewrite' => array(
		'slug' => '/%category%/%custom_taxonomy%',
		'with_front' => true
	)
	...

Support for Custom Taxonomies

Your custom taxonomy must be hierarchical and also must be enabled for REST. Pass the following values when calling register_taxonomy():

	...
	'hierarchical'               => true,
	'show_in_rest'               => true,
	...

Customizing permalinks

To change the structure of your permalinks for regular posts, you will have to go to your /wp-admin/options-permalink.php.

If you switch to Custom structure, you can have a permalink like this: /%category%/%custom_taxonomy%/%postname%/

The structure of your permalinks should include %category% or (any other taxonomy name)

To change the permalink for your CPTs, set the slug attribute as described in the snippet above.

Substitute for default term

When taxonomy term is used as a part of permalink, missing values might result in conflicts. To avoid that, when there is no term set for a post, but the permalink structure includes a %taxonomy% placeholder, a generic substitute will be used:

no-<taxonomy-name>

Please note that for category taxonomy, the default_category option from the site settings will be used instead.

It might be worth mentioning, that when a post has several terms from a single taxonomy assigned to it, by default WordPress uses the term with the lowest ID as primary.

Extensibility options

The plugin comes with several filters that can be used to hook in whatever custom logic is necessary.

stmpc/current_user_can

A hooked callable should return a boolean value indicating whether the current user should be allowed to modify primary terms.

stmpc/get_default_term_id

A hooked callable should return an integer referring to term ID to use as a primary term for the specified taxonomy.

stmpc/supported_post_types

A hooked callable should return an array of WP_Post_Type objects.

stmpc/supported_taxonomies

A hooked callable should return an array of WP_Taxonomy objects.

stmpc/supported_taxonomy_names

A hooked callable should return an string array of taxonomy names.

Rebuilding React component

Note, you will not need any additional setup besides placing the files of the plugin under your /plugins directory and activating the plugin.

However, if you decide to make modifications to the taxonomies metabox, you will have to rebuild the component which renders the metabox. For that, you will need node, npm and wp-scripts installed on your machine.

To set up your development environment, please follow these instructions: JavaScript Build Setup documentation

If you already have npm set up, all you will need will be to run these commands in /modern-primary-category/assets dir:

npm install
npm run build

To have the file watcher running, call this command:

npm run start

For general principles of how to build a Gutenberg-friendly post metabox please refer to this tutorial: Managing WordPress Metadata in Gutenberg Using a Sidebar Plugin


modern-primary-category's People

Contributors

dependabot[bot] avatar sarutole avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.