WordPress.org

Ready to get started?Download WordPress

Forums

Modifying post type - using the registered_post_type hook (5 posts)

  1. c_cav
    Member
    Posted 1 year ago #

    Actually this turned out to be a lot easier than I thought it would be, but since I could find NO documentation on this hook anywhere, figured I'd add something here.

    In my case, there is a post type from another plugin that I simply do not want to display as it only confuses the end user...

    Accomplishing this cleanly was rather easy using 2 action hooks. Code sample follows:

    We'll call the target post_type "foo"

    add_action('registered_post_type', 'my_registered_post_type_handler', 10, 2);
    function my_registered_post_type_handler($post_type, $args) {
      do_action("my_registered_{$post_type}_post_type", $post_type, $args);
    }
    
    add_action('my_registered_foo_post_type', 'my_registered_foo_post_type_handler', 10, 2);
    function my_registered_foo_post_type_handler($post_type, $args) {
      remove_action(current_filter(), __FUNCTION__, 10, 2);  // only run once
      // change your args here
      $args['show_ui'] = false;
      // re-register
      register_post_type($post_type, $args); // will call this hook again if it exists, so we removed it above.
    }

    And we are done.

  2. webg
    Member
    Posted 1 year ago #

    In which file we should include this action hooks?

  3. c_cav
    Member
    Posted 1 year ago #

    functions.php in your theme will work. Mine is in my plugin code.

  4. webg
    Member
    Posted 1 year ago #

    OK, thanks I'll give it a try.

  5. ronshe
    Member
    Posted 9 months ago #

    It did not work for me this way, but it did work after removing line:

    // re-register
      register_post_type($post_type, $args); // will call this hook again if it exists, so we removed it above.

Topic Closed

This topic has been closed to new replies.

About this Topic