6

I have created a custom post type, but I do not want it to have a permalink. By default, after entering the post title, it creates a permalink. I do not need them to be generated.

From my reading, it is said that custom post type will have a permalink and there is no way of disabling it. Is there a way that I can prevent the ajax call that receives the permalink?

2
  • 1
    Do you want to hide the permalink under the post title on Edit Post page? Do I understand right?
    – Max Yudin
    Mar 15, 2013 at 15:02
  • yes, I can hide it with css, but I want to disable it so I dont have to use css. If i could disable the ajax call it would be best Mar 15, 2013 at 15:12

3 Answers 3

7
<?php
    add_filter('get_sample_permalink_html', 'my_hide_permalinks');
    function my_hide_permalinks($in){
        global $post;
        if($post->post_type == 'my_post_type')
            $out = preg_replace('~<div id="edit-slug-box".*</div>~Ui', '', $in);
        return $out;
    }

This will remove:

  • Permalink itself
  • View Post button
  • Get Shortlink button

If you want to remove permalink only, replace the line containing preg_replace with

$out = preg_replace('~<span id="sample-permalink".*</span>~Ui', '', $in);

UPDATE:

get_sample_permalink_html has changed in version 4.4.

Here is the updated and tested code:

add_filter('get_sample_permalink_html', 'my_hide_permalinks', 10, 5);

function my_hide_permalinks($return, $post_id, $new_title, $new_slug, $post)
{
    if($post->post_type == 'my_post_type') {
        return '';
    }
    return $return;
}
4
  • this didn't work for me - but I had it return '' and it did work. I don't know the implications of that though. Nov 21, 2014 at 22:59
  • Yeah, this doesn't work.
    – Brett
    Jan 15, 2016 at 2:13
  • That's strange, it worked for me before. Anyway, it has to be changed to meet WP 4.4 requirements. I've updated the code.
    – Max Yudin
    Jan 18, 2016 at 15:35
  • This answer seems to only hide the permalink from showing, but it is not fully disabled
    – user33056
    Apr 6, 2017 at 4:56
6

While the accepted answer seems to only hide the permalink from showing, but still being generated and accessible, you can disable the permalink from showing and being accessible by setting certain register_post_types parameters.

I've achieved what I needed by only setting the following, but depending on your specific case, you may want to adjust some of the other parameters.

'public' => false,
'show_ui' => true

More: https://wordpress.stackexchange.com/a/108658/33056

2
  • In general, you are totally right, but the answer is given according to the OP's amendment commentary.
    – Max Yudin
    Apr 7, 2017 at 12:36
  • this answers the question perfectly. creating an extra function just to hide a permalink div is the utter definition of crazy
    – Zach Smith
    Sep 5, 2017 at 16:45
1

As the WordPress documention suggests, simply turn off the public argument of the registered post type.

It will have the consequence to turn off the following arguments, only if not specified otherwise:

  • exclude_from_search
  • publicly_queryable
  • show_in_nav_menus
  • show_ui

So you will not get anymore the possibility to go on the singular and the permalink is not shown anymore on the admin and the "view" buttons are hidden too.

PS: The selected answer shows what should not be done when it comes to WordPress development, badly. There is no need to alterate the render of some HTML and/or code something since there is a WordPress builtin Post Type API for that. So please consider my answer, as it meets all the question requirements and it is simple and straight to the point with a builtin solution.

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.