Website bookmarks as a custom post type

Here’s a clean way to accomplish what you need.

  • Register custom post types (sans post_tags and categories)
  • Register taxonomies specific to your bookmarks
  • Create a post meta box for the bookmark options (sample included). You could add a nofollow checkbox option to the meta box.
  • Save post meta

Here’s the whole shabang.

add_action('init', 'bookmark_post_type');
function bookmark_post_type()
{
    $labels = array(
        'name' => _x('Bookmark', 'post type general name'),
        'singular_name' => _x('Bookmark', 'post type singular name'),
        'add_new' => __('Add New'),
        'add_new_item' => __('Add New Bookmark'),
        'edit_item' => __('Edit Bookmark'),
        'new_item' => __('New Bookmark'),
        'all_items' => __('All Bookmarks'),
        'view_item' => __('View Bookmark'),
        'search_items' => __('Search Bookmarks'),'not_found' => __('No website Bookmarks found'),
        'not_found_in_trash' => __('No Bookmarks found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Bookmarks'
        );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'bookmark',
            'with_front' => false),
        //'taxonomies' => array('post_tag', 'category'),
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'can_export' => true,
        'supports' => array(
            'post-thumbnails',
            'thumbnail',
            'editor',
            'title'
        )
    );

    register_post_type('bookmarks', $args);

    $labels = array(
        'name' => _x( 'Bookmark Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item' => __( 'Edit Category' ), 
        'update_item' => __( 'Update Category' ),
        'add_new_item' => __( 'Add New Category' ),
        'new_item_name' => __( 'New Genre Category' ),
        'menu_name' => __( 'Bookmark Categories' ),
    );  

    register_taxonomy('bookmark_categories',array('bookmarks'), array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'bookmarks_categories' ),
    ));

    $labels = array(
        'name' => _x( 'Bookmark Tags', 'taxonomy general name' ),
        'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Tags' ),
        'popular_items' => __( 'Popular Tags' ),
        'all_items' => __( 'All Tags' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Tag' ), 
        'update_item' => __( 'Update Tag' ),
        'add_new_item' => __( 'Add New Tag' ),
        'new_item_name' => __( 'New Tag Name' ),
        'separate_items_with_commas' => __( 'Separate tags with commas' ),
        'add_or_remove_items' => __( 'Add or remove tags' ),
        'choose_from_most_used' => __( 'Choose from the most used tags' ),
        'menu_name' => __( 'Bookmark Tags' ),
      ); 

    register_taxonomy('bookmark_tags','bookmarks',array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'bookmark_tags' ),
    ));

}

//Create meta box
add_action('admin_init', 'bookmark_meta_boxes');
function bookmark_meta_boxes(){
    add_meta_box('bookmarks-meta', __('Bookmark Options'), 'bookmark_options', 'bookmarks', 'normal', 'high');
}

//Meta box form data
function bookmark_options(){
    global $post;
    $url = get_post_meta($post->ID, 'bookmark-url', true);

    echo "
    <div>
        <label for="bookmark-url">URL</label>
        <input style="width:100%; padding:4px;" type="text" name="bookmark-url" value="$url" />
    </div>
    ";
}

//Save the form data
add_action('save_post', 'bookmark_save');
function bookmark_save($post_ID){
    //Do nonce checking here
    if('bookmarks' === $_REQUEST['post_type']){
        update_post_meta($post_ID, 'bookmark-url', esc_html($_REQUEST['bookmark-url']));
    }
}

To remove the bookmarks from the main loop, add this above the loop code:

query_posts('post_type=post');

If you want to control the archives and single contexts for your CPT, you will need to add two files called archive-bookmarks.php and single-bookmarks.php

By keeping ‘publicly_queryable’ as true in your post type, it allows searching of your bookmark posts.

You can use the_post_thumbnail function for your screenshot image.

I would use the content/editor area for your bookmark description.

Leave a Comment