0

I use a plugin that creates a custom post type with a slug that I am wanting to change. I don't want to overwrite the plugin files, so I'm wondering how I can change the slug using a separate function? Thanks.

// Register post type
add_action( 'init', 'ctc_register_post_type_item' );
function ctc_register_post_type_item() {
    // Arguments
    $args = array(
        'labels' => array(
                'name'                  => _x( 'items', 'post type general name', 'church-theme-content' ),
                'singular_name'         => _x( 'item', 'post type singular name', 'church-theme-content' ),
                'add_new'               => _x( 'Add New', 'item', 'church-theme-content' ),
                'add_new_item'          => __( 'Add Item', 'church-theme-content' ),
                'edit_item'             => __( 'Edit Item', 'church-theme-content' ),
                'new_item'              => __( 'New Item', 'church-theme-content' ),
                'all_items'             => __( 'All Items', 'church-theme-content' ),
                'view_item'             => __( 'View Item', 'church-theme-content' ),
                'search_items'          => __( 'Search Items', 'church-theme-content' ),
                'not_found'             => __( 'No items found', 'church-theme-content' ),
                'not_found_in_trash'    => __( 'No items found in Trash', 'church-theme-content' )
        ),
        'public'        => ctc_feature_supported( 'items' ),
        'has_archive'   => ctc_feature_supported( 'items' ),
        'rewrite'       => array(
                'slug'          => 'items',
                'with_front'    => false,
                'feeds'         => ctc_feature_supported( 'items' )
        ),
        'supports'      => array( 'title', 'editor', 'excerpt', 'publicize', 'thumbnail', 'comments', 'author', 'revisions' ), // 'editor' required for media upload button (see Meta Boxes note below about hiding)
        'taxonomies'    => array( 'ctc_item_topic', 'ctc_item_book', 'ctc_item_series', 'ctc_item_speaker', 'ctc_item_tag' ),
        'menu_icon'     => 'dashicons-video-alt3'
    );

    $args = apply_filters( 'ctc_post_type_item_args', $args ); // allow filtering

    // Registration
    register_post_type(
        'ctc_item',
        $args
    );
}

2 Answers 2

2

In the code you posted, the filter ctc_post_type_item_args gets applied to the $args array before it is passed into register_post_type().

Putting a function into your themes functions.php and hooking it to the filter should do the trick:

function wpse_191003_ctc_post_type_item_args( $args ) {
    $args['rewrite']['slug'] = "your_new_slug";
    return $args;
}
add_filter( 'ctc_post_type_item_args', 'wpse_191003_ctc_post_type_item_args' );
0

Take a look at Tom's answer on a similar question. He hooks into an action that adds a rewrite rule to the database so the post is accessible through both slugs. He was not actually able to redeclare the slug, but he shows how you can hide the old slug so that it appears the new one is the right one.

Another option suggested by Dustin is to call register_post_type with the same $post_type but set the slug to the one you want. It sees to work for him, but no one else has confirmed if it throws problems down the line.

1
  • Thanks for sharing! Tom's answer does work to add an additional slug but the links on my site still point to the original slug, so essentially I do need to find a way to overwrite the slug. The code I mentioned above does have a filter in it: $args = apply_filters( 'ctc_post_type_item_args', $args ); // allow filtering Does this help me at all?
    – Ryan
    Jun 13, 2015 at 2:46

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.