WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#32545 reopened enhancement

Adding actions before and after "Add New" button

Reported by: vivekbhusal Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.2.2
Component: Posts, Post Types Keywords: 2nd-opinion
Focuses: administration, template Cc:
PR Number:

Description

We need a proper action to add new customs options (button, link, information) before and after the "Add New" button of post pages.
Such as http://stackoverflow.com/questions/29811433/wordpress-add-custom-button-on-post-type-list-page

on #wp-admin/edit.php line 281
on #wp-admin/edit-form-advanced.php line 395

This is just a addition of actions to make wordpress more customizable and modular.

Few use-case scenarios


Solution
We can use the following sample code to solve the problem.


/**
 * Fires before the Add New button is added to screen.
 *
 * The before_add_new- hook fires in a number of contexts.
 *
 * The dynamic portion of the hook name, `$pagenow`, is a global variable
 * referring to the filename of the current page, such as 'edit.php',
 * 'post.php' etc. A complete hook for the latter would be
 * 'before_add_new-post.php'.
 */
do_action('before_add_new-'.$pagenow);
if ( current_user_can( $post_type_object->cap->create_posts ) )
        echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="add-new-h2">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
/**
 * Fires after the Add New button is added to screen.
 *
 * The after_add_new- hook fires in a number of contexts.
 *
 * The dynamic portion of the hook name, `$pagenow`, is a global variable
 * referring to the filename of the current page, such as 'edit.php',
 * 'post.php' etc. A complete hook for the latter would be
 * 'before_add_new-post.php'.
 */
do_action('after_add_new-'.$pagenow);

Attachments (2)

screenshot1.png (159.0 KB) - added by vivekbhusal 4 years ago.
screenshot2.png (110.1 KB) - added by vivekbhusal 4 years ago.

Download all attachments as: .zip

Change History (12)

#1 @helen
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I would rather not add hooks in this location - variable horizontal items do not scale well across different screen sizes and unless the implementer using such hooks takes special care to style everything, most elements will look quite bad. These are also hooks that would inherently be tied to the current look and layout, which I find to be difficult to maintain long term when things move around or styling changes.

#2 @vivekbhusal
4 years ago

Absolutely agree with you. Adding a hook in that location and assuming user to test the layout won't work. But there are other ways we can implement to make it more open to developers' and in the same time making sure that it doesn't affect the user experience of wordpress. I think it would be nice to explore other options.

Last edited 4 years ago by vivekbhusal (previous) (diff)

#3 @vivekbhusal
4 years ago

  • Keywords 2nd-opinion added
  • Resolution wontfix deleted
  • Status changed from closed to reopened

#4 @SergeyBiryukov
4 years ago

  • Component changed from Administration to Posts, Post Types
  • Milestone set to Awaiting Review

#5 @armandoduran
3 years ago

I just had a need to add a button after "New" to a custom pots type. So I found this ticket. I am building sort of a web application by white labeling WP and using custom post types with Pods.

I understand and agree on the point about responsiveness, but I think this would be good to have.

#6 @Mte90
2 years ago

Actually I used javascript to add a button like that patch do but is not a cool hack.

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


22 months ago

#8 @jeremyescott
22 months ago

I was just about to write a patch for this very feature.

This is something I've found myself wanting a couple of times for my WP-based applications. The ability to add behaviors up there, eg: Sync with External Data, etc, is something that may be more and more and common as WP becomes more of an app platform. Asking users to find a tools or settings screen to press a button is a pain.

Yes, Javascript injection will continue to work, but its not ideal and JS is harder to test around, plus we get screen flickers on slow loading connections. I know that it may cause issues with responsiveness, but that should be up to the developers to address if they're adding buttons.

+1

I'll volunteer to revise the patch if needed.

#9 @pbearne
22 months ago

+1

Maybe we could change the UI here to allow a dropdown with addition actions allow with a filter to acc them.

Last edited 22 months ago by pbearne (previous) (diff)

#10 @simonrcodrington
5 months ago

I also agree with this request. Many times I've had to resort to using JS to move elements into the right spots at the top of the admin edit list pages because there isn't any appropriate hooks.

post.php (for editing single posts) has similar filters with edit_form_after_title and such. This seems like a natural extension of this.

Note: See TracTickets for help on using tickets.