Create custom actions with add_filter(post_action_{$action});
Even if it’s just to test, editing core files is bad practice. I prefer to download the source code onto my own machine and find where and how the hooks are firing.
Anyway, there is very poor documentation in this part of the source code, but I’ve been studying it for a while, and it looks like there are two major hooks inside of the post.php
file.
The first is the replace_editor
hook that fires in the switch
statement near the end of post.php
in the case
of action=edit
. This allows the classic WordPress editor to be replaced by something else, like the Gutenberg editor that is now shipped with it.
The second hook, which is the one that I use, is post_action_{$action}
. This fires in the default:
case of the switch
statement, meaning that if there is action=anything_other_than_given_cases
in the URL, this will fire.
Directly after this hook is called in the switch
statement, however, wp_redirect( admin_url( 'edit.php' ) );
is called, which after execution of the function hooked to post_action_{$action}
redirects you to edit.php
.
This is similar to what Elementor does/did where it has a custom action
used in the URL of post.php
, has a lot to do with query strings, GET, and AJAX, so if you aren’t terribly familiar with that then that’s a place to start. I apologize for the wordiness, but here’s an example of post_action_{$action}
.
<?php
add_action( 'post_action_your_action_name', 'connected_function' );
function connected_function($post_id) {
//Add your functionality here.
//This will execute when /post.php?post=$post_id&action=your_action_name
}
//After execution we return to default: case in post.php
//Where the wp_redirect() will be called.
If you need to add a link to access this page, post_row_actions
hook adds links under the posts in the edit.php
file, like “Edit”, “Trash”, “View”, etc. Looks like this:
<?php
add_filter( 'post_row_actions', 'add_links' );
function add_links($actions, $post) {
$url = add_query_arg(
[
'post' => $post->ID,
'action' => 'your_action_name',
],
admin_url( 'post.php' )
);
$actions['your_action_name'] = sprintf(
'<a href="%1$s">%2$s</a>',
$url,
'Actual Link Text'
);
return $actions;
}
This creates a link underneath your post you can click on that will redirect you to the custom action you want to add to post.php
, i.e. /post.php?post=$post_id&action=your_action_name
. Also, don’t forget to check if the current post is of the type you want to add this functionality to.
Hope this helps.