Giter Site home page Giter Site logo

liquidweb / sticky-tax Goto Github PK

View Code? Open in Web Editor NEW
7.0 11.0 1.0 895 KB

Make posts sticky within the context of a single category, tag, or custom taxonomy term.

Home Page: https://wordpress.org/plugins/sticky-tax

License: MIT License

Shell 7.88% PHP 75.02% JavaScript 16.47% CSS 0.63%
wordpress-plugin sticky-posts taxonomy-terms wordpress taxonomy posts-sticky

sticky-tax's Introduction

Sticky Tax

Build Status

A WordPress plugin that enables your content to be "sticky" within the context of one or more taxonomy terms!

Why might I want this?

Let's imagine Chris, a long-time blogger with lots of traffic coming to his site. He's worked hard to categorize all of his content, but there are a few posts that he wants to make sure are the first thing people see when they hit a category landing page.

Using WordPress' default behavior, these important pieces of content will eventually get lost behind pagination, just because they're not the newest posts in the category.

With Sticky Tax, Chris can highlight the most important posts in a category, whether they were written last week or last year!

Usage

After installing and activating Sticky Tax, a new "Promote Post" meta box will appear on the post edit screen, with a list of terms that have been assigned to the current post.

The Sticky Tax meta box on a WordPress post edit screen, showing a list of categories

On the front-end

Once a post has been made sticky within a taxonomy term (category, tag, etc.), it will be forced to the front of the list when users visit the term archive. Each sticky post even gets a .sticky-tax class added to it, enabling you to apply custom styling for your sticky content!

A post, made sticky via Sticky Tax, styled separately and at the top of the category archive page

Advanced usage

Sticky Tax is built with developer flexibility in mind, and includes a number of nice features for integrating with non-standard WordPress installation.

Adding the Sticky meta box to additional post types

Out of the box, Sticky Tax only appears on the post edit screen. However, you're able to add it to any post type you'd like via the stickytax_post_types filter:

/**
 * Add Sticky Tax support to the "my-cpt" post type.
 *
 * @param array $post_types Post types that should be able to use Sticky Tax.
 * @return array The filtered $post_types array.
 */
function add_sticky_tax_to_my_cpt( $post_types ) {
    $post_types[] = 'my-cpt';

    return $post_types;
}
add_filter( 'stickytax_post_types', 'add_sticky_tax_to_my_cpt' );

Modifying the taxonomies included in Sticky Tax

Sticky Tax tries to be helpful, and will automatically add support for terms from any taxonomy that has been registered as "public" for that post type. If you need to add or remove taxonomies from the list, you may do so via the stickytax_taxonomies filter:

/**
 * Remove tags from the Sticky Tax meta box.
 *
 * @param array $taxonomies Taxonomies using Sticky Tax.
 * @return array The filtered $taxonomies array.
 */
function remove_tags_from_sticky_tax( $taxonomies ) {
  unset( $taxonomies['post_tag'] );

  return $taxonomies;
}
add_filter( 'stickytax_taxonomies', 'remove_tags_from_sticky_tax' );

Working with custom taxonomy term meta boxes

If you've replaced the default category/tag meta boxes for either built-in or custom taxonomies, Sticky Tax may not be able to automatically mirror changes to the selected terms in the Sticky Tax meta box.

Fortunately, Sticky Tax exposes a simple API for adding or removing items from its list, via the window.stickyTax.addItem() and window.stickyTax.removeItem() methods.

Example

Let's say you have a list of pre-defined genres that posts can be assigned to, and the markup for the metabox looks something like this:

<div id="my-custom-meta-box">
  <div class="inner">
    <ul>
      <li><label><input name="genre[]" type="checkbox" value="1"> Action</label></li>
      <li><label><input name="genre[]" type="checkbox" value="2"> Comedy</label></li>
      <li><label><input name="genre[]" type="checkbox" value="3"> Horror</label></li>
      <li><label><input name="genre[]" type="checkbox" value="4"> Romance</label></li>
    </ul>
  </div>
</div>

You could listen for terms to be selected or deselected using the following code snippet:

document.getElementById('my-custom-meta-box').addEventListener('change', function (e) {
  var el = e.target;

  // Return early if the change event isn't on a checkbox.
  if ('INPUT' !== el.tagName) {
    return;
  }

  if (el.checked) {
    window.stickyTax.addItem(el.value, genre.parentElement.innerText, 'genre');
  } else {
    window.stickyTax.removeItem(el.value, 'genre');
  }
});

sticky-tax's People

Contributors

bswatson avatar norcross avatar stevegrunwell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sticky-tax's Issues

Flag sticky posts when viewing post lists

When viewing a post list in WP admin that has been filtered by a taxonomy term (e.g. https://example.com/wp-admin/edit.php?cat=1, which shows all the posts in "Uncategorized"), it would be nice if this list would do both of the following:

  1. Apply sticky logic, so sticky posts are moved to the top of the term-filtered post list by default (unless other ordering is applied).
  2. Append "โ€” Sticky" (or similar) following the post title (like WordPress does with drafts, private posts, etc.), indicating that this post is sticky within the current context.

This will make it easier for editors to identify posts that are sticky within a given category, as well as making it more obvious when a category has 20 sticky posts.

readme update

currently has screenshots and content related to the use of Select2

Prime caches on post save

Rather than relying on the first visit to a taxonomy archive page to populate the cache, we can prime it automatically at the end of Meta\save_post(): instead of clearing the caches for any term ID that's been affected, we can call Display\get_sticky_posts_for_term() to populate the cache.

This is certainly a micro-optimization, but on large sites every little bit can help. Thanks for the suggestion @bswatson!

remove post formats by default

the vast majority of people don't use them, and of those that do, most don't use them in a traditional taxonomy way. so I'm proposing we add a filter to remove them in the get_taxonomies_for_object function.

Example:

// Check the filter for excluding post formats.
$exclude    = apply_filters( 'stickytax_exclude_post_formats', true );

if ( ! empty( $exclude ) ) {
	unset( $taxonomies['post_format'] );
}

Bundle Select2

The plugin review from Mika came in last night, and there's one issue preventing us from being listed in the WordPress.org repo: when I wrote the code, I chose to load Select2 from a CDN rather than bundling it with the plugin.

From the email:

Calling files remotely

Offloading images, js, css, cgi, and other scripts to Google (or jquery.com or anywhere else frankly) is disallowed because you're introducing an unnecessary dependency on another site. If the file you're trying to use isn't a part of WordPress Core, then you should include it -locally- in your plugin, not remotely. If the file IS included in WordPress core, please call that instead.

The one exception to this rule is if your plugin is performing a service. We will permit this on a case by case basis, however since this can be confusing, we have some examples of what are not permitted:

  • Offloading jquery CSS files to Google - You should include the CSS in your plugin.
  • Inserting an iframe with a help doc - A link, or including the docs in your plugin is preferred.
  • Calling images from your own domain - They should be included in your plugin.

Here are some examples of what we would permit:

  • Calling font families from Google or their approved CDN (if GPL compatible)
  • API calls back to your server to process possible spam comments (like Akismet)
  • Offloading comments to your own servers (like Disqus)
  • oEmbed calls to a service provider (like Twitter or YouTube)

Please remove this dependency from your plugin and, if possible, include all files within the plugin (that is not called remotely). If instead you feel you ARE providing a service, please re-write your readme.txt in a manner that explains the service, the servers being called, and if any account is needed to connect.

This is a little more complex than simply adding the files (since we attempt to do some version-matching if the Select2 JavaScript was previously registered but there wasn't a matching stylesheet); we can drop this compatibility feature, if necessary.

Post list column

Introduce a new post list column, "Sticky Terms", which lists terms for which the given post is sticky. Like #2, this feature is intended to make it easier for site owners to see at-a-glance what content is still sticky.

The result may look something like this:
WordPress post list with a new "Sticky Terms" column

Rename meta box

As pointed out in #6, the title of "Sticky" for the meta box is kinda weak. Surely there's something better.

Current "Sticky" meta box on the post edit screen

Term list is not updating when using Gutenberg

When using the block editor (Gutenberg), the list of available terms is not being updated without a full-page refresh.

Steps to reproduce

  1. Attempt to create a new post using Gutenberg and assign a category
  2. After assigning a category, check the "Promote Post" panel

Expected Behavior

The newly-assigned category should be available for selection under "Promote Post"

Observed Behavior

The newly-assigned category is missing from the list.

Reference: https://wordpress.org/support/topic/update-for-gutenberg-3

Update CI test matrix

The Travis CI test matrix should be updated to use more recent versions of WordPress and PHP.

Add custom post type support

Hi, awesome plugin here! Just one suggestion.

There is documentation in the plugin to add support for custom taxonomies, but not custom post types.

In theory, we could add a filter to allow developers to modify a 'post_type' value to be used in the WP_Query args in get_sticky_posts_for_term(). At the moment this is not possible within the plugin.

Add "sticky" class to sticky posts

When WordPress' default sticky post functionality is used, sticky posts on the homepage are given the sticky class; this class should also be included when calling Display\append_sticky_class().

Thanks for the suggestion in #6, @norcross!

Dynamically filter the taxonomy term list

As @norcross pointed out in our discussion, Sticky Tax currently allows posts to be made sticky for a given term even if the post is not assigned to that term.

This was originally to avoid users having to add a term, save the page, then be able to assign the stickiness and have to save a second time, but ideal behavior would be:

  1. The user adds or removes a term from the post
  2. That term is added/removed from the list of available terms in the Sticky meta box automatically.

Code review plugin

@norcross As mentioned on the call earlier, I'd appreciate a code review on the plugin before I push the first release to WordPress.org. We received approval on the plugin over the weekend, but you have a great habit of catching some BS, and I'd rather we see that now rather than after it's live.

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.