[wp-trac] [WordPress Trac] #31073: Add new filter on function register_post_type

WordPress Trac noreply at wordpress.org
Wed Aug 16 10:11:31 UTC 2023


#31073: Add new filter on function register_post_type
-------------------------+-------------------------------------------------
 Reporter:  teolaz       |       Owner:  (none)
     Type:  enhancement  |      Status:  closed
 Priority:  normal       |   Milestone:
Component:  Posts, Post  |     Version:  4.1.5
  Types                  |
 Severity:  normal       |  Resolution:  duplicate
 Keywords:               |     Focuses:  coding-standards, php-
                         |  compatibility
-------------------------+-------------------------------------------------
Changes (by terryfenner59):

 * focuses:   => coding-standards, php-compatibility
 * version:  4.1 => 4.1.5


Comment:

 Here's a general outline of how you could do this:

 1. **Create a Wrapper Function:**
    Instead of directly calling `register_post_type`, create a wrapper
 function in your plugin. This wrapper function will include your
 additional filter and then call the original `register_post_type`
 function.

    ```php
    function custom_register_post_type($post_type, $args = array()) {
        // Apply your modifications to $args here before registering
        $modified_args = apply_filters('custom_register_post_type_args',
 $args, $post_type);

        // Call the original register_post_type function
        register_post_type($post_type, $modified_args);
    }
    ```

 2. **Apply Custom Modifications:**
    Now you can add your custom modifications using the
 `custom_register_post_type_args` filter. This filter allows you to
 https://www.plentyofpaintings.com/ modify the arguments before they are
 passed to the `register_post_type` function.

    ```php
    function modify_custom_post_type_args($args, $post_type) {
        if ($post_type === 'your_custom_post_type') {
            // Modify $args here based on your requirements
            $args['rewrite'] = array('slug' => 'new-slug');
        }
        return $args;
    }
    add_filter('custom_register_post_type_args',
 'modify_custom_post_type_args', 10, 2);
    ```

 3. **Usage:**
    Instead of calling `register_post_type` directly, use your custom
 wrapper function to register post types.

    ```php
    custom_register_post_type('your_custom_post_type', $args);
    ```

 By creating this wrapper function and using the custom filter, you can
 effectively modify the post type arguments before they are processed by
 the `register_post_type` function. This approach provides a way to achieve
 your goal without directly modifying the core function or waiting for a
 core enhancement to be implemented.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/31073#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list