Add filter to safe title helper

This is better.
This commit is contained in:
Tetrakern 2023-08-31 17:56:09 +02:00
parent db8f002fa1
commit 0da8e3701a
4 changed files with 62 additions and 38 deletions

View File

@ -301,7 +301,6 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `show_admin_bar` | `__return_false`
| `the_content` | `fictioneer_embed_consent_wrappers`, `fictioneer_add_lightbox_to_post_images`, `fictioneer_add_chapter_paragraph_id`
| `the_password_form` | `fictioneer_password_form`
| `the_title` | `fictioneer_prefix_sticky_title`
| `theme_templates` | `fictioneer_disallow_page_template_select`
| `update_post_metadata` | `fictioneer_prevent_page_template_update`
| `upload_size_limit` | `fictioneer_upload_size_limit`

View File

@ -297,7 +297,7 @@ Filters the queried comments in the `comments.php` template and `fictioneer_ajax
* $comments (array) The queried comments.
* $post_id (int) Current post ID.
**Hooked filters:**
**Hooked Filters:**
* `fictioneer_shift_sticky_comments( $comments )` Shift sticky comments to the top. Priority 10.
---
@ -599,6 +599,19 @@ Filters the RSS link returned by the `fictioneer_get_rss_link( $post_type, $post
---
### `apply_filters( 'fictioneer_filter_safe_title', $title, $post_id )`
Filters the string returned by the `fictioneer_get_safe_title( $post_id )` function, after all tags and line breaks have been stripped. No further sanitization is applied here, so you can add HTML again.
**Parameters:**
* $title (string) The sanitized title of the post.
* $post_id (int) The post ID.
**Hooked Filters:**
* `fictioneer_prefix_sticky_safe_title( $comments )` Prepends icon to sticky blog posts.
* `fictioneer_prefix_draft_safe_title( $comments )` Prepends "Draft:" to drafts.
---
### `apply_filters( 'fictioneer_filter_search_title', $html, $args )`
Filters the HTML for the search title before it is rendered in the `search.php` template. The default title follows the "n Search Results" pattern.

View File

@ -231,10 +231,8 @@ if ( ! function_exists( 'fictioneer_get_safe_title' ) ) {
*/
function fictioneer_get_safe_title( $post_id ) {
// Get title and remove script/style tags and line breaks
$title = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', get_the_title( $post_id ) );
$title = preg_replace( '/[\r\n\t ]+/', ' ', $title );
$post_status = get_post_status( $post_id );
// Get title and remove HTML
$title = wp_strip_all_tags( get_the_title( $post_id ) );
// If empty, use the datetime as title
if ( empty( $title ) ) {
@ -245,17 +243,57 @@ if ( ! function_exists( 'fictioneer_get_safe_title' ) ) {
);
}
if ( $post_status === 'draft' ) {
$title = sprintf(
_x( 'Draft: %s', 'Draft: {Post Title}', 'fictioneer' ),
$title
);
}
// Apply filters
$title = apply_filters( 'fictioneer_filter_safe_title', $title, $post_id );
return $title;
}
}
/**
* Prepends icon to sanitized titles of sticky blog posts
*
* @since Fictioneer 5.7.1
*
* @param string $title The sanitized title of the post.
* @param int $id The ID of the post.
*
* @return string The modified title.
*/
function fictioneer_prefix_sticky_safe_title( $title, $id ) {
// Prepend icon to titles of sticky posts
if ( is_sticky( $id ) && get_post_type( $id ) === 'post' ) {
return '<i class="fa-solid fa-thumbtack sticky-pin"></i> ' . $title;
}
// Continue filter
return $title;
}
add_filter( 'fictioneer_filter_safe_title', 'fictioneer_prefix_sticky_safe_title', 10, 2 );
/**
* Prepends "Draft:" to sanitized titles of drafts
*
* @since Fictioneer 5.7.1
*
* @param string $title The sanitized title of the post.
* @param int $id The ID of the post.
*
* @return string The modified title.
*/
function fictioneer_prefix_draft_safe_title( $title, $id ) {
// Prepend icon to titles of drafts
if ( get_post_status( $id ) === 'draft' ) {
return 'Draft: ' . $title;
}
// Continue filter
return $title;
}
add_filter( 'fictioneer_filter_safe_title', 'fictioneer_prefix_draft_safe_title', 10, 2 );
// =============================================================================
// GET READING TIME NODES HTML
// =============================================================================

View File

@ -1171,30 +1171,4 @@ function fictioneer_prevent_track_and_ping_updates( $data ) {
}
add_filter( 'wp_insert_post_data', 'fictioneer_prevent_track_and_ping_updates', 1 );
// =============================================================================
// ADD STICKY PREFIX TO STICKY BLOG POSTS
// =============================================================================
/**
* Prepends icon to titles of sticky posts
*
* @since Fictioneer 5.7.1
*
* @param string $title The original title of the post.
* @param int $id The ID of the post.
*
* @return string The modified title.
*/
function fictioneer_prefix_sticky_title( $title, $id ) {
// Prepend icon to titles of sticky posts
if ( is_sticky( $id ) ) {
return '<i class="fa-solid fa-thumbtack sticky-pin"></i> ' . $title;
}
// Continue filter
return $title;
}
add_filter( 'the_title', 'fictioneer_prefix_sticky_title', 10, 2 );
?>