Make WordPress Core

Opened 10 years ago

Last modified 4 weeks ago

#37578 new enhancement

Dashboard Recent Activity widget - new filters to manipulate output

Reported by: Jonnyauk Owned by:
Priority: normal Milestone: Future Release
Component: Administration Version: 4.6
Severity: normal Keywords: has-patch good-first-bug has-test-info
Cc: Focuses:

Description

It is currently not possible to manipulate the recent post activity dashboard widget. Output is built using wp_dashboard_recent_posts(). 2 filters could be introduced for the parameters for future and recent posts.

This would allow easy interaction with the parameters array, for instance increasing the amount of posts shown, changing order, post status, CSS ID etc - would definitely make it more useful!

I realise that there is a filter dashboard_recent_posts_query_args inside thewp_dashboard_recent_posts() function (used to build the queries, useful for switching post type etc), but there is currently no way to interact with the calls to this function.

Attachments (4)

37578-01.diff (1.7 KB ) - added by Jonnyauk 10 years ago.
New filters for Dashboard Recent Activity widget future and recent posts.
good first bug 1.txt (606 bytes ) - added by vedantsonone1234 17 months ago.
37578.diff (1.7 KB ) - added by pmbaldha 16 months ago.
Added a patch using the existing 'dashboard_recent_posts_query_args' filter with support for passing the $id to wp_dashboard_recent_posts() for better control.
37578-1.diff (1.8 KB ) - added by pmbaldha 16 months ago.
PHPDoc Comment update.

Download all attachments as: .zip

Change History (15)

@Jonnyauk
10 years ago

New filters for Dashboard Recent Activity widget future and recent posts.

#1 @Jonnyauk
10 years ago

  • Keywords has-patch added

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


5 years ago

#3 @desrosj
22 months ago

  • Focuses ui administration removed
  • Keywords changes-requested good-first-bug added
  • MilestoneFuture Release

Hi @Jonnyauk,

My apologies that this took so long to receive a response!

Instead of adding new filters, what if the $id passed to wp_dashboard_recent_posts() were also passed to the filter as an additional parameter? Then the preexisting dashboard_recent_posts_query_args filter could be used for this in any context by providing a bit more control.

@pmbaldha
16 months ago

Added a patch using the existing 'dashboard_recent_posts_query_args' filter with support for passing the $id to wp_dashboard_recent_posts() for better control.

#4 @pmbaldha
16 months ago

  • Keywords needs-testing added

#5 @pmbaldha
16 months ago

  • Keywords changes-requested removed

@pmbaldha
16 months ago

PHPDoc Comment update.

#6 @SirLouen
16 months ago

  • Keywords needs-test-info added; needs-testing removed

@pmbaldha
For this filters, please provide an use-case for testing.

#7 follow-up: @pmbaldha
15 months ago

@SirLouen

Usecases:

  1. If you want to show 10 posts instead of the default 5:


add_filter( 'dashboard_recent_posts_query_args', 'increase_dashboard_post_count', 10, 2 );
function increase_dashboard_post_count( $args, $id ) {
    $args['max'] = 10;
    return $args;
}

2.⁠ ⁠Show Only Posts from a Specific Category in "Recently Published"
If your site has multiple categories and you want the widget to only show posts from the "Announcements" category:


add_filter( 'dashboard_recent_posts_query_args', 'dashboard_recent_posts_announcements_only', 10, 2 );
function dashboard_recent_posts_announcements_only( $args, $id ) {
    if ( 'published-posts' === $id ) {
        $args['cat'] = get_cat_ID( 'Announcements' );
    }

    return $args;
} ⁠

#8 in reply to: ↑ 7 @SirLouen
15 months ago

  • Keywords needs-refresh added; needs-test-info removed

Replying to pmbaldha:

Usecases:

Did you check JD comment on a possible alternative?

Adding new hooks is the worst thing possible when there is an alternative. They should always be the last resort.

This ticket was mentioned in PR #9026 on WordPress/wordpress-develop by @sukhendu2002.


15 months ago
#9

  • Keywords needs-refresh removed

#10 @rockschtar
6 weeks ago

  • Keywords has-test-info added

Patch Testing Report

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9026

Environment

  • WordPress: 7.1-beta3 (trunk, c213aec5a6)
  • PHP: 8.3.30
  • Server: Apache
  • Database: MySQL 8.0.40
  • OS: Fedora Linux (Docker)
  • Browser: n/a (verified server-side via WP-CLI)
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: none
  • Plugins: none

Steps taken

  1. Registered a callback on dashboard_recent_posts_query_args with accepted_args = 2, logging the second parameter.
  2. Triggered both calls made by wp_dashboard_site_activity() (status future / id future-posts and status publish / id published-posts) via wp eval-file.
  3. Without the patch, the callback receives no second argument.
  4. ✅ Patch is solving the problem: with the PR applied, the callback receives 'future-posts' and 'published-posts' respectively, so the two Activity widget queries can now be distinguished.

Expected result

  • The existing dashboard_recent_posts_query_args filter receives the widget id as a second parameter, as suggested by @desrosj, instead of introducing new filters.

Additional Notes

  • The PR is based on 6.9-alpha but cherry-picks cleanly onto current trunk (7.1-beta3) and works there as well.
  • The @since 6.9.0 annotation needs to be bumped to the next release open for enhancements.

Support Content

<?php
require_once ABSPATH . 'wp-admin/includes/dashboard.php';

add_filter( 'dashboard_recent_posts_query_args', function ( $query_args, ...$extra ) {
    printf( "Filter received 2nd param: %s\n", $extra ? var_export( $extra[0], true ) : '(none)' );
    return $query_args;
}, 10, 2 );

ob_start();
wp_dashboard_recent_posts( array( 'max' => 5, 'status' => 'future', 'order' => 'ASC', 'title' => 'Publishing Soon', 'id' => 'future-posts' ) );
wp_dashboard_recent_posts( array( 'max' => 5, 'status' => 'puitle' => 'Recently Published', 'id' => 'published-posts' ) );
ob_end_clean();

This ticket was mentioned in PR #12960 on WordPress/wordpress-develop by @welcher.


4 weeks ago
#11

Opened from the WordPress Contributor Toolkit.

Note: See TracTickets for help on using tickets.