Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Override CPT setting in child theme WordPress

  • SOLVED

I have a custom post type that I'm registering in my main theme. Here's the code:



add_action( 'init', 'register_cpt_team_member' );

function register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'yl'),

//custom cpt capabilites
'capability_type' => 'team_member',
'map_meta_cap' => true,
);

register_post_type( 'team-member', $args );
}

function wpq_activate_roles() {

if(class_exists('WP_Roles')){

if(!isset($wp_roles)) $wp_roles = new WP_Roles();

if(is_object($wp_roles)) {

//team_member

$team_caps = $GLOBALS['wp_post_types']['team-member']->cap ;

foreach($team_caps as $key=>$value){

$wp_roles->add_cap('administrator', $value);

//$wp_roles->add_cap('editor', $value);

}

$wp_roles->add_role('team_member', 'Team Member', array('read' => true));

//$wp_roles->add_cap('team_member', 'upload_files');
$wp_roles->add_cap('team_member', 'read_team_member');
$wp_roles->add_cap('team_member', 'edit_team_member');
$wp_roles->add_cap('team_member', 'edit_team_members');
$wp_roles->add_cap('team_member', 'publish_team_members');
$wp_roles->add_cap('team_member', 'edit_published_team_members');
}

}

}



It's working fine, but now I need to override the rewrite array slug in a child theme. What's the proper way to do that? I tried placing the entire CPT code above in the child's functions, but that just makes the site white screen.

Answers (4)

2015-12-08

Rempty answers:

You need to add this code to your functions.php


function change_slug_of_post_type() {
$args = get_post_type_object("team-member");
//Now yout new slug
$args->rewrite["slug"] = "mynewslug";
register_post_type($args->name, $args);
}
add_action('init', 'change_slug_of_post_type', 20);


After that go to Settings -> Permalinks and put default permalinks, save it, now you can change again to your custom permalinks and the new slug must be working.


Kyler Boudreau comments:

Thanks Rempty - It didn't change it. No white screen or anything, just no change.

2015-12-07

Romel Apuya answers:

add a priority to add_action( 'init', 'register_cpt_team_member' );

in child theme

add_action( 'init', 'register_cpt_team_member', 0);


Romel Apuya comments:

or load it later

add_action( 'wp_loaded', 'register_cpt_team_member' ,12);


Romel Apuya comments:

add_action( 'wp_loaded', 'register_cpt_team_member' ,0);

2015-12-07

Reigel Gallarde answers:

There is a global array $wp_post_types. You can change $wp_post_types[$post_type]->rewrite after the parent theme has set the CPT.

try this


add_action( 'init', 'child_register_cpt_team_member', 20 );

function child_register_cpt_team_member() {
global $wp_post_types;
$p = 'team-member'; // this is post type

// Someone has changed this post type, always check for that!
if ( empty ( $wp_post_types[ $p ] )
or ! is_object( $wp_post_types[ $p ] )
or empty ( $wp_post_types[ $p ]->rewrite )
)
return;


$wp_post_types[ $p ]->rewrite = array('slug' => 'team');

}


Reigel Gallarde comments:

you can also try something like this if my first suggestion does not work... (thinking there would be flush problem)

add_action('registered_post_type', 'my_registered_post_type_handler', 10, 2);
function my_registered_post_type_handler($post_type, $args) {
do_action("team_member_post_type", $post_type, $args);
}

add_action('team_member_post_type', 'team_member_post_type_handler', 10, 2);
function team_member_post_type_handler($post_type, $args) {

if ($post_type != 'team-member') {
return;
}

remove_action(current_filter(), __FUNCTION__, 10, 2); // only run once
// change your args here
$args['rewrite'] = array('slug' => 'team');

// re-register
register_post_type($post_type, $args);
}


Reigel Gallarde comments:

[[LINK href="https://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook"]]source[[/LINK]]


Reigel Gallarde comments:

I made an error on my code... try this again...

add_action('registered_post_type', 'my_registered_post_type_handler', 10, 2);

function my_registered_post_type_handler($post_type, $args) {

do_action("team_member_post_type", $post_type, $args);

}


add_action('team_member_post_type', 'team_member_post_type_handler', 10, 2);

function team_member_post_type_handler($post_type, $args) {


if ($post_type != 'team-member') {

return;

}

remove_action(current_filter(), __FUNCTION__, 10, 2); // only run once

// change your args here

$rewrite = &$args->rewrite;
$rewrite['slug'] = 'team';

// re-register
register_post_type($post_type, $args);
flush_rewrite_rules(); // or remove this line and visit dashboard's Settings>Permalinks
}


Kyler Boudreau comments:

BOOM. That worked. Thank you!

2015-12-07

timDesain Nanang answers:

put the following code in child theme's function.php
switch theme to another one, then switch back.

<?php
function child_theme_setup() {
//remove register cpt in parent theme
remove_action('init', 'register_cpt_team_member');

//register cpt in child theme
child_register_cpt_team_member();
child_activate_roles();

flush_rewrite_rules();
}
add_action('after_setup_theme', 'child_theme_setup');


add_action( 'init', 'child_register_cpt_team_member' );
function child_register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'new-slug'), //

//custom cpt capabilites
'capability_type' => 'team_member',
'map_meta_cap' => true,
);


register_post_type( 'team-member', $args );
}


function child_activate_roles() {
if(class_exists('WP_Roles')){
if(!isset($wp_roles)) $wp_roles = new WP_Roles();
if(is_object($wp_roles)) {

//team_member
$team_caps = $GLOBALS['wp_post_types']['team-member']->cap ;
foreach($team_caps as $key=>$value){
$wp_roles->add_cap('administrator', $value);
//$wp_roles->add_cap('editor', $value);
}

$wp_roles->add_role('team_member', 'Team Member', array('read' => true));
//$wp_roles->add_cap('team_member', 'upload_files');
$wp_roles->add_cap('team_member', 'read_team_member');
$wp_roles->add_cap('team_member', 'edit_team_member');
$wp_roles->add_cap('team_member', 'edit_team_members');
$wp_roles->add_cap('team_member', 'publish_team_members');
$wp_roles->add_cap('team_member', 'edit_published_team_members');

}
}
}


Kyler Boudreau comments:

Thanks everyone. I tried all of these suggestions, but nothing works. I'll keep looking.


timDesain Nanang comments:

Hi Kyler, I have tried the code and working properly.
Please check the hooking's priority in the parent theme, then remove if exists.


[[LINK href="https://codex.wordpress.org/Function_Reference/remove_action"]]https://codex.wordpress.org/Function_Reference/remove_action
[[/LINK]]<blockquote>
$priority
(int) (optional) The priority of the function (as defined when the function was originally hooked).
Default: 10
</blockquote>

Thank you