1

So basically in The Wordpress Admin when you are editing a "post" you have a url slug like...

post.php?post=829&action=edit&classic-editor

I would like to know, is there a filter or way that I can add the post_type query arg to this url? Which would result in something like...

post.php?post=829&action=edit&classic-editor&post_type=product

I would like this to run for all post types (page, post, product, ...)

Why: I have a plugin that disables plugins from loading based on a string in the url.

My Goal: is to reveal the post_type=value in the url when editing a post, so that I can disable plugins from loading when editing post types that those plugins do not need to load on.

Example: when editing a portfolio post type, I do not need Woocommerce or its addons to load. So I would like to disable them when editing portfolio posts.

This is entirely for performance reasons of a potential mu-site I might offer publicly.

Thank you.

  • 1
    please explain, what your goal is with that additional arg.. – honk31 Jun 30 at 14:54
  • @honk31ok. thank you. I updated my post with my why, goal, and an example... – Enterprise Branding Jun 30 at 15:18
  • 1
    You should be able to extrapolate the Post Type by Post ID post. It would be cached so there shouldn't be any additional database queries. See get_post_type(). Even new posts should have a global $post object to work with. – Howdy_McGee Jun 30 at 15:24
  • @Howdy_McGee thanks. Yes, but how would I filter that into the url is my question? Or did I misunderstand something of your comment? I was hoping there might be a built in filter of Wordpress to do so? – Enterprise Branding Jun 30 at 15:31
1

there you go, i tested it in my current development and it works. it adds the post_type query parameter to all edit links, even on custom post types and also on the admin bar. this even works with preexisting query parameters and i also have other plugins currently running (f.ex. wpml).

function so370070_admin_url($url, $path)
{
    if (strpos($path, "post.php") !== false) :
        $post_type = get_post_type();
        if ($post_type) :
            $url = add_query_arg('post_type', $post_type, $url);
        endif;
    endif;

    return $url;
}

add_filter('admin_url', 'so370070_admin_url', 10, 2);

and as solved by yourself, here is the links for attachments:

function so370070_register_post_type_args($args, $post_type)
{
  if ($post_type == 'attachment') {
    //NOTE: This "_edit_link" arg is noted for Wordpress's internal use only
    //in /wp-includes/post.php around line 84
    //However, we are using a dveloper's filter to adjust it, so use accordingly
    //that you understand it may need tweaked if Wordpress itself makes changes
    //Overall it should be generally safe... no issues so far :)
    $args['_edit_link'] = add_query_arg('post_type', 'attachment', $args['_edit_link']);
  }

  return $args;
}

add_filter('register_post_type_args', 'so370070_register_post_type_args', 10, 2);
| improve this answer | |
  • 1
    You might be able to use the add_query_arg() function too which may be safer. – Howdy_McGee Jun 30 at 16:52
  • 1
    thanks @Howdy_McGee, i altered the answer to your sugggestion – honk31 Jun 30 at 17:03
  • Thanks @honk31 and @ Howdy_McGee ! I was looking through the WP Core, and was coming along to trying some stuff, but you have solved it already. Thanks! This is awesome! I can really boost the speed now and have complete control of plugin loading in the admin. – Enterprise Branding Jun 30 at 17:40
  • 1
    @honk31 I found a solution. Generally not recommended because it filters an internal Wordpress arg, however it should be generally safe I think. I added it to a gist with screenshot in comments. If you feel it is worth it, you can add my solution/answer to your answer, but none the less it is here @ gist.github.com/EnterpriseBranding/… – Enterprise Branding Jul 9 at 11:42
  • 1
    @honk31 thanks! I updated the gist as well with the full code and descriptions and credits @ gist.github.com/EnterpriseBranding/… ... really really really appreciate it, because this has let me build something that can normally be done with Wordpress without greatly having performance and loading concerns. But now I can control which plugins are loading where in the admin, so the performance is even better than more common Wordpress installs. – Enterprise Branding Jul 9 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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