Skip to content

[New block] bookmark and bookmark count #69660

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

Conversation

sarthaknagoshe2002
Copy link
Contributor

What?

This PR adds a bookmarking/favorites system for WordPress posts, allowing users to save posts for later reading. The feature includes:

  • A Bookmark Block for single posts and Query Loops.
  • A Counter Block to display the number of saved posts.
  • A Bookmarks Archive Template to list bookmarked posts.
  • Support for non-logged-in users via Local storage.

Closes #59524

Why?

Currently, WordPress lacks a built-in way for users to bookmark or favorite posts for later reading. This is an essential feature for content-heavy sites, similar to YouTube’s Watch Later or social media Saved Posts features.

How?

  • Bookmark Block
    • Adds a two-state button (selected/deselected).
    • Currently supports 3 icons - heart, bookmark & star ( Icons are aimed to be used as wish list, bookmark or like posts).
    • Works inside single posts and Query Loops.
  • Counter Block
    • Displays the number of bookmarked posts.
    • Can be placed in menus or elsewhere for quick access.
    • Redirects to "/favorites" on click. ( Can be made customisable )
  • Bookmarks Archive Template
    • A dedicated template to list all saved posts.
  • Support for Non-Logged-In Users
    • Uses local storage store bookmarks.
    • Doesn't use object storage as a fallback since the interactivity API's state serves the same purpose.
    • Ensures visitors can bookmark posts without needing an account.

Testing Instructions

  1. Add the Bookmark Block to a post or Query Loop.
  2. Click the bookmark icon to save/un-save posts.
  3. Add the Counter Block to display the total number of saved posts.
  4. Visit the Bookmarks Archive to view saved posts.
  5. Test functionality for logged-in users and guests.

Note

  1. The choice of icons is subjective & open to all kinds of discussions.
  2. The value of argument $src in wp_register_script() is supposed to be like @wordpress/script_path which didn't seemed to work for me, it will be rectified as per feedback & guidance.
  3. A lot of customisation can be added like the archive URL, More Icons, Post_per_page for the template, Customisable text, Template customisation, etc. But to keep things simple I feel that these customisations can be added later.

Screenshots or screencast

Editor side

editor.mov

Logged out users

Screen.Recording.2025-03-22.at.10.31.41.PM.mov

Logged in users

logged.in.mov

Copy link

github-actions bot commented Mar 22, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @rawdolphe.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: rawdolphe.

Co-authored-by: sarthaknagoshe2002 <sarthaknagoshe2002@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: dhanson-wp <dhansondesigns@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@t-hamano t-hamano added the New Block Suggestion for a new block label Mar 23, 2025
@sarthaknagoshe2002
Copy link
Contributor Author

@Mamaduka, @adamsilverstein & @gziolo I’d love to get your feedback on this PR!

@Mamaduka
Copy link
Member

As far as I'm aware, all the new blocks are on hold for the time being. Please take a look at the discussion in #58773.

@Mamaduka Mamaduka added the [Type] Enhancement A suggestion for improvement. label Mar 24, 2025
@sarthaknagoshe2002
Copy link
Contributor Author

In looking through past issues and PRs for new blocks added to the default block library, I see that each block is considered individually based on perceived need, functionality, quality, and accessibility. So we're planning to build some of these new blocks first.

@Mamaduka, as I understand it, new blocks—other than those listed in #58773—are not considered a priority since they don’t focus on individual needs. However, I believe these blocks should be included in the list, as they serve important functions and have broad use cases, such as bookmarks or wish lists, which could benefit a wide range of bloggers and large entities like news agencies.

@Mamaduka
Copy link
Member

@sarthaknagoshe2002, the new block guidelines need to be written/updated. Unfortunately, currently, everyone is busy with the release.

Until such guidelines are published, the referenced issue is all we have, and I would be hesitant to approve new blocks. This doesn't mean that work here should stop; I'm just trying to set the right expectations.

@sarthaknagoshe2002
Copy link
Contributor Author

@Mamaduka Okay then, in the meantime, we can refine this further and discuss any shortcomings. 😇🙌🏻

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I've built anything similar, but I've left some surface notes regarding the code.

I think the best example of a Likes plugin I can remember in the WP ecosystem is this one - https://github.com/wearerequired/rest-likes. I'm sure we can learn a lot from it. cc @ocean90

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't really understand what the point of this page is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This basically acts like an archive page which would display all the posts bookmarked/liked by the user at a single place

Comment on lines +132 to +134
add_rewrite_rule( '^favorites/?$', 'index.php?pagename=favorites', 'top' );
add_rewrite_rule( '^favorites/page/([0-9]+)/?$', 'index.php?pagename=favorites&paged=$matches[1]', 'top' );
flush_rewrite_rules();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block shouldn't register any rewrite rules or flush them. Calling flush_rewrite_rules on init is a bad practice.

Comment on lines +144 to +156
function block_core_bookmark_load_favorites_template( $template ) {
global $wp;
if ( isset( $wp->query_vars['pagename'] ) && 'favorites' === $wp->query_vars['pagename'] ) { // Ensure it's set
status_header( 200 );
if ( ! is_user_logged_in() ) {
wp_register_script( 'core-bookmarks-logged-out-user', '/wp-content/plugins/gutenberg/packages/block-library/src/bookmark/bookmarks-logged-out-user.js', array(), filemtime( '/wp-content/plugins/gutenberg/packages/block-library/src/bookmark/bookmarks-logged-out-user.js' ), true );
wp_enqueue_script( 'core-bookmarks-logged-out-user' );
}
return 'wp-content/plugins/gutenberg/packages/block-library/src/bookmark/all-bookmarked.php';
}
return $template;
}
add_filter( 'template_include', 'block_core_bookmark_load_favorites_template' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes against Site Editor principles, which require a PHP template to display the data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue mentions that this feature should also work for logged out users.
In that case the data will be stored on local storage, which would require a semi-ready template & a JS to read the local storage & add it to the page.

If the Site Editor principles doesn't allow that then what could be a better work around to achieve the aforementioned?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Query Block or some other template can be used for logged-in users. If the decision is to introduce a new template, then that should be handled in the core.

Currently, PR seems to be trying to do too much at once. Maybe it's better to start with minimal requirements for the block(s), see if it gets approved and then work on enhancements, like logged-out user support.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, PR seems to be trying to do too much at once.

@sarthaknagoshe2002 maybe you can reduce this PR to just the Bookmark block itself, then have separate PRs for each "feature" on top of that, eg counter block, archive template and non logged in user support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamsilverstein On it! 😇🙌🏻

Comment on lines +158 to +170
/**
* Sets the page name.
*
* @since 6.8.0
*/
function block_core_bookmark_set_favorites_page_title( $title ) {
global $wp;
if ( isset( $wp->query_vars['pagename'] ) && 'favorites' === $wp->query_vars['pagename'] ) {
$title['title'] = 'Favorite Posts';
}
return $title;
}
add_filter( 'document_title_parts', 'block_core_bookmark_set_favorites_page_title' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this updates the document title tag. All human-readable user-facing strings need to be translated.

@sarthaknagoshe2002
Copy link
Contributor Author

As per the feedbacks I have created 3 different PRs for each feature & also addressed a few feedbacks 😇

@Mamaduka Mamaduka closed this Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Block Suggestion for a new block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Block: Bookmark / Favorite Posts
4 participants