0

When using Custom Post Types within a plugin, I would like to create a way that when editing I could change the use of 'action=' in the url, to something else opening a new url which would focus in on different areas of editing my post. It would allow the user to focus on certain areas of the post they are editing in cleaner view without everything on one page. But still allowing me to tap into the post when saving. In the past I've always used Div's over the top of everything with higher indexing to hide everything else but having come across this I thought this might look neater.

What I would like to achieve is creating more options for 'action=' within the URL. I've seen it done in plugins like Elementor page builder. As standard it is written in the post.php file to have the options of edit, editattachment, editpost, trash, preview and more. They have built onto it in elementor adding elementor, elementor-library and others.

/* Link to look like */
www.example.co.uk/wp-admin/post.php?post=15&action=newaction

My thinking is somehow I need to add to the existing list seen by post.php and then tell it what to display when accessing the URL. In a test to see how this would function I temporarily made a new case within post.php using 'newaction' with the same code as the 'edit' case. I was expecting it to then show the editor when accessing the URL see above, however instead it redirects you to

www.example.co.uk/wp-admin/edit.php

I'm not sure if it would need something else adding like:

if($_POST['action' == 'example1']){
prevent default redirect and load scripts/stylesheets etc for page
} 

or if it's something you have to tell Wordpress to allow somehow.

Has anyone come across this before or would happen to be able to point me in the right direction. I've been through Elementors plugin files to see if I could work it out from that. But it seems to jump all over the place being such a big plugin its annoying to follow and thought someone may know faster on here.

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.