0

Please excuse the noob question, but I am fairly new at php. I am using the plugin WP Hashed ID, https://wordpress.org/plugins/wp-hashed-ids/, works well with a regular post, but my site uses a custom video post type and the plugin does not work with a custom post type. The developer of the plugin said he has no plans on updating the plugin. Does anybody know how I would make this plugin work with custom post types, I really need this to work. Your help is greatly appreciated. My goal is to encrypt my custom video url like youtube, bitly, etc.Transforming the post_id (1234) to a hashed id/or equivalent (dx49Ph) permalink structure.

PS: I also use the custom post type permalink plugin (https://wordpress.org/plugins/custom-post-type-permalinks/)

I added some code to the Hashed-ID plugin as recommended by a user on its support forum, but it did not work for me. Here is the edited plugin code:

require_once('hashids/lib/hashids.php-5-3.php');

define ('HASHED_IDS_MIN_LENGTH', 6);

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('post_type_link', '_hashed_id_custom_link', 1, 2);
        add_filter('parse_request', '_hashed_id_parse_request');
    }
}

function _hashed_id_post_link($permalink, $post) {
    $hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
    $permalink = str_replace('%hashed_id%', $hashids->encrypt((int)$post->ID), $permalink);
    return $permalink;
}

function _hashed_id_custom_link($permalink, $post) {
    $hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
    $permalink = str_replace('%hashed_id%', $hashids->encrypt((int)$post->ID), $permalink);
    return $permalink;
}

function _hashed_id_parse_request($qv) {
    $hashed_id = $qv->query_vars['hashed_id'];
    if (strlen($hashed_id) > 0) {
        $hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
        $id = $hashids->decrypt($hashed_id);
        if (isset($id[0]) && is_numeric($id[0])) {
            $qv->query_vars['p'] = $id[0];
        } else {
            $qv->query_vars['pagename'] = $hashed_id;
        }
    }
    return $qv;
}
add_action('init', 'hashed_id');

function hashed_ids_activate_plugin() {
    global $wp_rewrite;
    if ($wp_rewrite->using_permalinks()) {
        $wp_rewrite->set_permalink_structure(
                str_replace('%post_id%', '%hashed_id%', $wp_rewrite->permalink_structure)
        );
    }
    flush_rewrite_rules(false);
}
register_activation_hook( __FILE__, 'hashed_ids_activate_plugin' );

function hashed_ids_deactivate_plugin() {
    global $wp_rewrite;
    if ($wp_rewrite->using_permalinks()) {
        $wp_rewrite->set_permalink_structure(
                str_replace('%hashed_id%', '%post_id%', $wp_rewrite->permalink_structure)
        );
    }
    flush_rewrite_rules(false);
}
register_deactivation_hook( __FILE__, 'hashed_ids_deactivate_plugin' );


?>

1 Answers1

0

After some Googling, I only found this article written by Francis Yaconiello.

He says:

For every custom post type you want to add to your plugin you need to define a post type class.

I'm not sure if this is in general or specifically for his plugin tutorial. This is fairly advanced so I'm not sure how much use this is for you.

It has too much code to paste it in here, but take a look.

Steven
  • 19,224
  • 47
  • 152
  • 257
  • Thank you for your response. I went through the tutorial, it focuses more on creating custom post types and creating plugin. I am more interested in integrating one plugin with another or maybe bypassing one plugin all together. – user3693369 May 31 '14 at 22:34