sectsect / wp-tag-order

↕︎ Order tags independently in each posts (not site-globally) on WordPress.
GNU General Public License v3.0
20 stars 3 forks source link
drag-and-drop order rest-api sort tags terms wordpress wordpress-plugin

WP Tag Order

Plugin Check PHP Unit Tests PHPStan PHP Coding Standards Latest Stable Version

Order tags independently in each posts (not site-globally) on WordPress with simple Drag-and-Drop ↕︎ sortable feature.

[!IMPORTANT] This plugin is NOT compatible with Gutenberg on WordPress 5.x. Consider using Classic Editor Plugin.

[!IMPORTANT] Mutation events will no longer be supported in Google Chrome and Edge in July 2024 (Google Chrome on July 23, 2024, and Microsoft Edge the week of July 25, 2024). As a result, WP Tag Order versions 3.6.0 or less will not work with Chrome v127 and later, which will be released on July 23, 2024.
You have to update to v3.7.0 or later.
If you still need PHP7 support, see Troubleshooting below.
See Chrome for Developers Blog for more details.

Get Started

  1. Clone this Repo into your wp-content/plugins directory.
    cd /path-to-your/wp-content/plugins
    git clone git@github.com:sectsect/wp-tag-order.git
  2. Activate the plugin through the Plugins menu in WordPress.
  3. Go to Settings -> WP Tag Order page to select which taxonomies to enable ordering for.

Features

Notes

Requirements

APIs

Function Description
get_the_tags_ordered() Based on get_the_tags() - Codex
get_the_terms_ordered() Based on get_the_terms() - Codex
get_the_tag_list_ordered() Based on get_the_tag_list() - Codex
get_the_term_list_ordered() Based on get_the_term_list() - Codex
the_tags_ordered() Based on the_tags() - Codex
the_terms_ordered() Based on the_terms() - Codex

Usage Example

get_the_tags_ordered()

<?php
$terms = get_the_tags_ordered();
if ( $terms && ! is_wp_error( $terms ) ) :
?>
<ul>
    <?php foreach ( $terms as $term ) : ?>
        <li>
            <a href="https://github.com/sectsect/wp-tag-order/blob/master/<?php echo get_term_link( $term->slug, 'post_tag' ); ?>">
                <?php echo $term->name; ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

get_the_terms_ordered()

<?php
$terms = get_the_terms_ordered( $post->ID, 'post_tag' );
if ( $terms && ! is_wp_error( $terms ) ) :
?>
<ul>
    <?php foreach ( $terms as $term ) : ?>
        <li>
            <a href="https://github.com/sectsect/wp-tag-order/blob/master/<?php echo get_term_link( $term->slug, 'post_tag' ); ?>">
                <?php echo $term->name; ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

get_the_tag_list_ordered()

<?php echo get_the_tag_list_ordered(); ?>

get_the_term_list_ordered()

<?php echo get_the_term_list_ordered( $post->ID, 'post_tag' ); ?>

the_tags_ordered()

<?php the_tags_ordered(); ?>

the_terms_ordered()

<?php the_terms_ordered( $post->ID, 'post_tag' ); ?>

Programmatic Tag Order Management

The plugin provides a Tag_Updater class for developers to programmatically manage tag order:

$tag_updater = new \WP_Tag_Order\Tag_Updater();

try {
    // Update tag order using an array or comma-separated string
    $result = $tag_updater->update_tag_order(
        get_the_ID(),   // Post ID
        'post_tag',     // Taxonomy that enabled in plugin settings
        [1, 2, 3]       // Tag IDs in desired order
    );
} catch ( \InvalidArgumentException $e ) {
    // Error handling
    error_log( $e->getMessage() );
}

This class allows flexible tag order updates directly from your theme or custom plugin code, supporting both array and string inputs with robust validation.

Return Value

update_tag_order() returns: int|bool

REST API

The WP Tag Order plugin provides two REST API endpoints for managing tag order:

Get Tag Order

Example Request

GET /wp-json/wp-tag-order/v1/tags/order/123
w/ cURL ```bash curl --location 'https://your-wordpress-site.com/wp-json/wp-tag-order/v1/tags/order/123' ```

Update Tag Order

Example Request

PUT /wp-json/wp-tag-order/v1/tags/order/123
{
  "taxonomy": "post_tag",
  "tags": "5,3,1,4,2"
}
w/ cURL ```bash curl --location --request PUT 'https://your-wordpress-site.com/wp-json/wp-tag-order/v1/tags/order/123' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_JWT_TOKEN' \ --data '{ "taxonomy": "post_tag", "tags": "5,3,1,4,2" }' ```

Example Response

{
  "success": true,
  "code": "tags_order_updated",
  "message": "Tag order updated successfully.",
  "data": {
    "status": 200,
    "post_id": 123,
    "taxonomy": "post_tag",
    "tags": [
      5,
      3,
      1,
      4,
      2
    ]
  }
}

Authentication

For Developers

Troubleshooting

Still need support for PHP 7.

I have a branch php7 to support PHP 7 and End-of-Life for JaveScript Mutation events.
The branch will not be maintained anymore, so I recommend you migrate to PHP 8.

Change log

See CHANGELOG file.

License

See LICENSE file.

✌️

A little project by @sectsect