Using WordPress ‘post_format_rewrite_base’ PHP filter

The post_format_rewrite_base WordPress PHP filter allows you to modify the post formats rewrite base.

Usage

add_filter('post_format_rewrite_base', 'custom_post_format_rewrite_base');
function custom_post_format_rewrite_base($context) {
    // your custom code here
    return $context;
}

Parameters

  • $context (string) – Context of the rewrite base. Default ‘type’.

More information

See WordPress Developer Resources: post_format_rewrite_base

Examples

Change the rewrite base to ‘category’

In this example, we change the rewrite base for post formats to ‘category’.

add_filter('post_format_rewrite_base', 'change_rewrite_base_to_category');
function change_rewrite_base_to_category($context) {
    $context = 'category';
    return $context;
}

Add a custom prefix to the rewrite base

This example adds a custom prefix ‘custom-‘ to the rewrite base.

add_filter('post_format_rewrite_base', 'add_custom_prefix_to_rewrite_base');
function add_custom_prefix_to_rewrite_base($context) {
    $context = 'custom-' . $context;
    return $context;
}

Change the rewrite base to ‘content-type’

In this example, we change the rewrite base for post formats to ‘content-type’.

add_filter('post_format_rewrite_base', 'change_rewrite_base_to_content_type');
function change_rewrite_base_to_content_type($context) {
    $context = 'content-type';
    return $context;
}

Make the rewrite base the same as the site’s language code

This example sets the rewrite base for post formats to the site’s language code.

add_filter('post_format_rewrite_base', 'set_rewrite_base_to_language_code');
function set_rewrite_base_to_language_code($context) {
    $context = get_locale();
    return $context;
}

Change the rewrite base based on user role

In this example, we change the rewrite base for post formats based on the current user’s role.

add_filter('post_format_rewrite_base', 'change_rewrite_base_based_on_user_role');
function change_rewrite_base_based_on_user_role($context) {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
        $context = 'admin-type';
    } elseif (in_array('editor', $user->roles)) {
        $context = 'editor-type';
    }
    return $context;
}