• Great plugin! I love Hash IDs, and appreciate your taking the time to add them to permalinks.

    I also wanted to use this plugin with a Custom Post Type, so I added the necessary code. I couldn’t find a Github repo, so the modification is shown below.

    I’ve found this especially helpful when combined with Custom Post Type Permalinks.

    Original: wp-hashed-ids.php lines 18-26 at version 1.0

    function hashed_id() {
        global $wp_rewrite;
        add_rewrite_tag('%hashed_id%','([^/]+)');
        $permalink = $wp_rewrite->permalink_structure;
        if (!empty($permalink) && false !== strpos( $permalink, '%hashed_id%' )) {
            add_filter('pre_post_link', '_hashed_id_post_link', 10, 2);
            add_filter('parse_request', '_hashed_id_parse_request');
        }
    }

    Modification:

    // Add after line 23, 'pre_post_link'
    add_filter('post_type_link', '_hashed_id_post_link', 10, 2); // CPTs

    Result:

    function hashed_id() {
        global $wp_rewrite;
        add_rewrite_tag('%hashed_id%','([^/]+)');
        $permalink = $wp_rewrite->permalink_structure;
        if (!empty($permalink) && false !== strpos( $permalink, '%hashed_id%' )) {
            add_filter('pre_post_link', '_hashed_id_post_link', 10, 2); // Posts
            add_filter('post_type_link', '_hashed_id_post_link', 10, 2); // CPTs
            add_filter('parse_request', '_hashed_id_parse_request');
        }
    }

    https://wordpress.org/plugins/wp-hashed-ids/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    First of all, thank you for sharing with us.

    I would like to know if its possible to have more information. I installed the plugin, past your code, set permalink Settings for Custom Post Types on /%hashed_id%/ but unfornutally it does not work.

    When i create a CPT,i dont get an hashed ID, i got “http://mysite/slug/%hashed_id%/” so the /%hashed_id%/ isnt remplaced and of course, i got a bad Request telling me that my browser sent a request that this server could not understand.

    Can you help me please ?

    Thanks alot

    If you want that your plugin supports Custom Post Types, replace your functions in wp-hashed-ids.php with following codes:

    function hashed_id() {
        global $wp_rewrite;
        add_rewrite_tag('%hashed_id%','([^/]+)');
        
        foreach ( get_post_types( array('_builtin'=>false),'names') as $post_type ) {
            $permalink =  get_option( $post_type.'_structure' );       
            if (!empty($permalink) && false !== strpos( $permalink, '%hashed_id%' )) {
                add_filter('post_type_link', '_hashed_id_custom_link', 10, 2); 
                add_filter('parse_request', '_hashed_id_parse_request');
            }
        }
    }
    
    function _hashed_id_custom_link($permalink, $post) {
       $hashids = _hashed_id_get_instance();
        return str_replace('%hashed_id%', $hashids->encode((int)$post->ID), $permalink);
    }
    • This reply was modified 7 years, 5 months ago by mdayan. Reason: missing code
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Type Support’ is closed to new replies.