1

When we set permalink as Post name and go to wordpress any default post Like "Testing 123" single page its link looks like this

localhost/foo_articles/testing-123

Now i when we change our permalink to Custom Structure and set value like %category%/%postname%, the link looks like this

http://localhost/foo_articles/testing/testing-123/

testing is my category slug

Now the main part of my question is

I make a plugin where i create a post type foo_articles and custom taxonomy foo_categories

Its work perfectly. When i click on a category its link looks like this

http://localhost/foo_articles/foo_category/junk-food/

and when i click on an article for a single page, its link looks like this

http://localhost/foo_articles/foo_articles/how-to-reduce-the-intake-of-junk-food-in-children/

foo_articles is my post type and its a change able

Now my question is how can i set links that when a user set permalinks Custom Structure and set value like %category%/%postname% my link also change like above default post single page.

http://localhost/foo_articles/article cat slug/how-to-reduce-the-intake-of-junk-food-in-children/

Here is custom post type code:

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

    $foo_slug = 'foo_articles';
    $foo_slug = get_option('foo_plugin_slug');

    $labels = array(
        'name'                  =>  __('Foo', 'fff'),
        'singular_name'         =>  __('Foo', 'fff'),
        'all_items'             =>  __('Articles', 'fff'),
        'add_new'               =>  __('New Article', 'fff'),
        'add_new_item'          =>  __('Add New Article', 'fff'),
        'edit_item'             =>  __('Edit Article', 'fff'),
        'new_item'              =>  __('New Article', 'fff'),
        'view_item'             =>  __('View Articles', 'fff'),
        'search_items'          =>  __('Search Articles', 'fff'),
        'not_found'             =>  __('Nothing found', 'fff'),
        'not_found_in_trash'    =>  __('Nothing found in Trash', 'fff'),
        'parent_item_colon'     =>  ''
    );

    $foo_rewrite = array(
        'slug'          =>  FOO_PLUGIN_SLUG, // i define this in plugin index file
        'with_front'    =>  true,
        'pages'         =>  false,
        'feeds'         =>  true,
    );

    $args = array(
        'labels'                =>  $labels,
        'public'                =>  true,
        'publicly_queryable'    =>  true,
        'show_ui'               =>  true,
        'query_var'             =>  true,
        'menu_icon'             =>  plugin directory.'images/icon-foo.png',
        'capability_type'       =>  'post',
        'hierarchical'          =>  false,
        'menu_position'         =>  3,
        'supports'              =>  array('title','editor','thumbnail','comments','tags'),
        'rewrite'               =>  $foo_rewrite,
        'show_in_menu'          =>  true,
        'show_in_nav_menus'     =>  true,
        'show_in_admin_bar'     =>  true,
        'can_export'            =>  true,
        'has_archive'           =>  true,
        'exclude_from_search'   =>  true
    );

    register_post_type( 'foo_articles' , $args );
    flush_rewrite_rules();
}
add_action( 'init', 'foo_taxonomies', 0 );

// Article taxonamy
function foo_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              =>  __( 'Article Category', 'fff'),
        'singular_name'     =>  __( 'Article Category', 'fff' ),
        'search_items'      =>  __( 'Search Article Category', 'fff' ),
        'all_items'         =>  __( 'All Article Categories', 'fff' ),
        'parent_item'       =>  __( 'Parent Article Category', 'fff' ),
        'parent_item_colon' =>  __( 'Parent Article Category:', 'fff' ),
        'edit_item'         =>  __( 'Edit Article Category', 'fff' ),
        'update_item'       =>  __( 'Update Article Category', 'fff' ),
        'add_new_item'      =>  __( 'Add New Article Category', 'fff' ),
        'new_item_name'     =>  __( 'New Article Category Name', 'fff' ),
    'menu_name'         =>  __( 'Categories', 'fff' )
    );  

    register_taxonomy( 'foo_categories', array( 'foo_articles' ), array(
        'hierarchical'      =>  true,
        "labels"            =>  $labels,
        "singular_label"    =>  __( 'Foo Category', 'foo'),
        'show_ui'           =>  true,
        'query_var'         =>  true,
        'rewrite'           =>  array( 'slug' => 'foo_category', 'with_front' => true )
    ));
    flush_rewrite_rules();
}

Note: i change my post type slug by plugin settings and its option_name is foo_plugin_slug (its a client idea)

So please tell me how can i do this. Is there any hook or filter or htaccess code

1

Have you tried this plugin?

WP Permastructure

Adds the ability to configure permalinks for custom post types using rewrite tags like %post_id% and %author%.

enter image description here

Download this plugin https://wordpress.org/plugins/wp-permastructure/screenshots/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.