WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#42840 closed defect (bug) (worksforme)

New plugin / theme editor wp_ajax_ save hook not working or inexistent

Reported by: cristianuibar Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.1
Component: Editor Keywords:
Focuses: Cc:

Description

Hello,

We are trying to use a hook using: https://developer.wordpress.org/reference/hooks/wp_ajax__requestaction/
for the new Code Editor in Plugins and Theme.

Add any of these hooks using add_action(), and they will not work.

Neighter
wp_ajax_edit_theme_plugin_file, nor
wp_ajax_edit-theme-plugin-file
works.

I expected the hooks to work and they didn't.

We tried an alternative using the 'wp_doing_ajax' hook, but it executes twice per request, even though we were checking for:

if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit-theme-plugin-file')

Best,
Cristian Uibar

Change History (3)

#1 @birgire
3 years ago

@cristianuibar Welcome to trac

How did you test it? Do you have a test example?

I think you need to use priority <= 1, because the default callback for the wp_ajax_edit-theme-plugin-file action has priority 1 and calls wp_send_json_success() (or wp_send_json_error()), that implicitly calls die().

Last edited 3 years ago by birgire (previous) (diff)

#2 @dd32
3 years ago

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

Hi @cristianuibar

As @birgire alluded to, this is because your action is hooked later than when the core action runs, you'll need to hook in at priority 0 or lower: edit: I originally said 9, but that doesn't work, use 0 or -1 and it will work.

add_action( 'wp_ajax_edit-theme-plugin-file', function() {
	// Returns a failure for all edit attempts
	die( '-1' );
}, 0 );

I'm marking this as worksforme as the above code works as expected.

Last edited 3 years ago by dd32 (previous) (diff)

#3 @cristianuibar
3 years ago

Indeed. It works with a lower priority. This should be stated as a NOTE in the docs maybe?

Note: See TracTickets for help on using tickets.