WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Create Custom Posttype with no permalink (4 posts)

  1. Xeevee
    Member
    Posted 2 years ago #

    Hi all,

    I've been playing around with theme development for a little while now and have been creating a settings page to store information such as social media links, contact details etc ...

    However this requires me to have already hard coded the Social Media fields for the settings page.

    Ideally I would like to create a Social Media Post type and then the user can add as many to the list as they like, and I just run through a loop of the post type where I want to print the links.

    The problem is I don't want this post type to have a Permalink, I don't want people to be able to find a "Facebook" page on the site and all that's on it is a post title. Now I know I could set up a 301 for the custom post type eg: "/socialmedia/" in my htaccess file, but ideally I would just like to remove the Permalink from the post type all together.

    I've had a Google around for any tips but not found anything yet, If anyone could help me out I'd greatly appreciate it. =)

    I'm pretty competent with WordPress so I'm not asking for a complete solution, a pointer in the right direction or just some suggested functions would probably be enough to get me going =)

    Thanks in advanced!

    - Jack

  2. I was going to say that all posts (and post types) MUST have a permalink, except ... well, MENUS are CPTs. You could build off that functionality, in theory. Haven't a clue how, but if it's been done before, maybe you can start there.

  3. Xeevee
    Member
    Posted 2 years ago #

    Hi Ipstenu,

    Thanks for your hasty reply, as you've said Menu's are technically just custom post types, along with attachments, and a few others, and while I didn't think there were Permalink's associated with each of these types, after having a snoop around in the database they all have a guid set, however after trying to follow the links in the database for Menu items for example, it returns the default 404 template.

    After looking into this a little further I suspect the 301 redirects may indeed be the best alternative after all, just with some added code to hide the Permalink display on the edit post page.

    I'm going to continue to have a play around, and will post back any findings here for anyone that’s interested.

    Any further advice is still appreciated.

    - Jack

  4. Xeevee
    Member
    Posted 2 years ago #

    For anyone who may be interested in this problem, I've come up with a solution, it's not ideal, but it gets the job done.

    First I created my custom post type of the type "socialmedia"

    Then in my themes functions.php I've added the following function:

    /**
     * Removes the permalinks display on the socialmedia post type
     */
    add_action('admin_init', 'remove_permalink');
    
    function remove_permalink() {
    
    	if($_GET['post']) {
    
    		$post_type = get_post_type($_GET['post']);
    
    		if($post_type == 'socialmedia' && $_GET['action'] == 'edit') {
    			echo '<style>#edit-slug-box{display:none;}</style>';
    		}
    	}
    }

    This simply checks if you're on the edit page of a post of type "socialmedia" and if so, sets the Permalink's containing div to display none.

    Next, I create a file "single-socialmedia.php" which will be used by all posts of the type "socialmedia" as explained in the template hierarchy, and this file simply calls the 404.php file in my theme using the following code:

    <?php
    /**
     * The Template for displaying all posts of the "socialmedia" type.
     *
     *	- Loads the 404.php template
     *
     * @package WordPress
     * @subpackage Project Bee Racing
     * @since Project Bee Racing 1.0
     */
    ?>
    
    <?php get_template_part( '404' ) ?>

    The end result is a post type that gives the illusion of having no permalink and is inaccessible on your site as an individual post. Perfect for creating item lists of an unknown quantity / value social as social media links.

    - Jack

Topic Closed

This topic has been closed to new replies.

About this Topic