How to add post_type=value when editing that post type in the Wordpress admin?

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

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.

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.

Share Improve this question edited Jun 30, 2020 at 15:18 Trinity Branding asked Jun 30, 2020 at 14:36 Trinity BrandingTrinity Branding 134 bronze badges 4
  • 1 please explain, what your goal is with that additional arg.. – honk31 Commented Jun 30, 2020 at 14:54
  • @honk31ok. thank you. I updated my post with my why, goal, and an example... – Trinity Branding Commented Jun 30, 2020 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 Commented Jun 30, 2020 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? – Trinity Branding Commented Jun 30, 2020 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742279110a4414153.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信