Version Description
- Added plug-in
Download this release
Release Info
Developer | stephenh1988 |
Plugin | Post Type Archive Link |
Version | 1.0 |
Comparing to | |
See all releases |
Version 1.0
- metabox.js +22 -0
- post-type-archive-links.php +189 -0
- readme.txt +34 -0
metabox.js
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
jQuery(document).ready(function($) {
|
2 |
+
$('#submit-post-type-archives').click(function(event){
|
3 |
+
event.preventDefault();
|
4 |
+
|
5 |
+
/* Get checked boxes */
|
6 |
+
var postTypes = [];
|
7 |
+
$('#post-type-archive-checklist li :checked').each(function() {
|
8 |
+
postTypes.push($(this).val());
|
9 |
+
});
|
10 |
+
|
11 |
+
/* Send checked post types with our action, and nonce */
|
12 |
+
$.post( ajaxurl, {
|
13 |
+
action: "my-add-post-type-archive-links",
|
14 |
+
posttypearchive_nonce: MyPostTypeArchiveLinks.nonce,
|
15 |
+
post_types: postTypes,
|
16 |
+
},
|
17 |
+
|
18 |
+
/* AJAX returns html to add to the menu */
|
19 |
+
function( response ) {$('#menu-to-edit').append(response);}
|
20 |
+
);
|
21 |
+
})
|
22 |
+
});
|
post-type-archive-links.php
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Post Type Archive Links
|
4 |
+
Version: 1.0
|
5 |
+
Description: Adds a metabox to the Appearance > Menu page to add post type archive links
|
6 |
+
Author: Stephen Harris
|
7 |
+
Author URI: http://profiles.wordpress.org/users/stephenh1988/
|
8 |
+
*/
|
9 |
+
|
10 |
+
class Harris_Post_Type_Archive_Link{
|
11 |
+
|
12 |
+
//Everything will go here
|
13 |
+
public function load(){
|
14 |
+
//Hook function to add the metabox to the Menu page
|
15 |
+
add_action( 'admin_init', array(__CLASS__,'add_meta_box'));
|
16 |
+
|
17 |
+
// Javascript for the meta box
|
18 |
+
add_action( 'admin_enqueue_scripts', array(__CLASS__,'metabox_script') );
|
19 |
+
|
20 |
+
//Ajax callback to create menu item and add it to menu
|
21 |
+
add_action('wp_ajax_my-add-post-type-archive-links', array( __CLASS__, 'ajax_add_post_type'));
|
22 |
+
|
23 |
+
//Assign menu item the appropriate url
|
24 |
+
add_filter( 'wp_setup_nav_menu_item', array(__CLASS__,'setup_archive_item') );
|
25 |
+
|
26 |
+
//Make post type archive link 'current'
|
27 |
+
add_filter( 'wp_nav_menu_objects', array(__CLASS__,'maybe_make_current'));
|
28 |
+
}
|
29 |
+
|
30 |
+
public function add_meta_box() {
|
31 |
+
add_meta_box( 'post-type-archives', __('Post Types','my-post-type-archive-links'),array(__CLASS__,'metabox'),'nav-menus' ,'side','low');
|
32 |
+
}
|
33 |
+
|
34 |
+
public function metabox( ) {
|
35 |
+
global $nav_menu_selected_id;
|
36 |
+
|
37 |
+
//Get post types
|
38 |
+
$post_types = get_post_types(array('public'=>true,'_builtin'=>false), 'object');?>
|
39 |
+
|
40 |
+
<!-- Post type checkbox list -->
|
41 |
+
<ul id="post-type-archive-checklist">
|
42 |
+
<?php foreach ($post_types as $type):?>
|
43 |
+
<li><label><input type="checkbox" value ="<?php echo esc_attr($type->name); ?>" /> <?php echo esc_attr($type->labels->name); ?> </label></li>
|
44 |
+
<?php endforeach;?>
|
45 |
+
</ul><!-- /#post-type-archive-checklist -->
|
46 |
+
|
47 |
+
<!-- 'Add to Menu' button -->
|
48 |
+
<p class="button-controls" >
|
49 |
+
<span class="add-to-menu" >
|
50 |
+
<input type="submit" id="submit-post-type-archives" <?php disabled( $nav_menu_selected_id, 0 ); ?> value="<?php esc_attr_e('Add to Menu'); ?>" name="add-post-type-menu-item" class="button-secondary submit-add-to-menu" />
|
51 |
+
</span>
|
52 |
+
</p>
|
53 |
+
<?php
|
54 |
+
}
|
55 |
+
|
56 |
+
public function metabox_script($hook) {
|
57 |
+
if( 'nav-menus.php' != $hook )
|
58 |
+
return;
|
59 |
+
|
60 |
+
//On Appearance>Menu page, enqueue script:
|
61 |
+
wp_enqueue_script( 'my-post-type-archive-links_metabox', plugins_url('/metabox.js'),array('jquery'));
|
62 |
+
|
63 |
+
//Add nonce variable
|
64 |
+
wp_localize_script('my-post-type-archive-links_metabox','MyPostTypeArchiveLinks', array('nonce'=>wp_create_nonce('my-add-post-type-archive-links')));
|
65 |
+
}
|
66 |
+
|
67 |
+
public function ajax_add_post_type(){
|
68 |
+
|
69 |
+
if ( ! current_user_can( 'edit_theme_options' ) )
|
70 |
+
die('-1');
|
71 |
+
|
72 |
+
check_ajax_referer('my-add-post-type-archive-links','posttypearchive_nonce');
|
73 |
+
|
74 |
+
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
|
75 |
+
|
76 |
+
if(empty($_POST['post_types']))
|
77 |
+
exit;
|
78 |
+
|
79 |
+
//Create menu items and store IDs in array
|
80 |
+
$item_ids=array();
|
81 |
+
foreach ( (array) $_POST['post_types'] as $post_type) {
|
82 |
+
$post_type_obj = get_post_type_object($post_type);
|
83 |
+
|
84 |
+
if(!$post_type_obj)
|
85 |
+
continue;
|
86 |
+
|
87 |
+
$menu_item_data= array(
|
88 |
+
'menu-item-title' => esc_attr($post_type_obj->labels->name),
|
89 |
+
'menu-item-type' => 'post_type_archive',
|
90 |
+
'menu-item-object' => esc_attr($post_type),
|
91 |
+
'menu-item-url' => get_post_type_archive_link($post_type),
|
92 |
+
);
|
93 |
+
|
94 |
+
//Collect the items' IDs.
|
95 |
+
$item_ids[] = wp_update_nav_menu_item(0, 0, $menu_item_data );
|
96 |
+
}
|
97 |
+
|
98 |
+
//If there was an error die here
|
99 |
+
if ( is_wp_error( $item_ids ) )
|
100 |
+
die('-1');
|
101 |
+
|
102 |
+
//Set up menu items
|
103 |
+
foreach ( (array) $item_ids as $menu_item_id ) {
|
104 |
+
$menu_obj = get_post( $menu_item_id );
|
105 |
+
if ( ! empty( $menu_obj->ID ) ) {
|
106 |
+
$menu_obj = wp_setup_nav_menu_item( $menu_obj );
|
107 |
+
$menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
|
108 |
+
$menu_items[] = $menu_obj;
|
109 |
+
}
|
110 |
+
}
|
111 |
+
|
112 |
+
//This gets the HTML to returns it to the menu
|
113 |
+
if ( ! empty( $menu_items ) ) {
|
114 |
+
$args = array(
|
115 |
+
'after' => '',
|
116 |
+
'before' => '',
|
117 |
+
'link_after' => '',
|
118 |
+
'link_before' => '',
|
119 |
+
'walker' => new Walker_Nav_Menu_Edit,
|
120 |
+
);
|
121 |
+
echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
|
122 |
+
}
|
123 |
+
|
124 |
+
//Finally don't forget to exit
|
125 |
+
exit;
|
126 |
+
}
|
127 |
+
|
128 |
+
|
129 |
+
public function setup_archive_item($menu_item){
|
130 |
+
if($menu_item->type !='post_type_archive')
|
131 |
+
return $menu_item;
|
132 |
+
|
133 |
+
$post_type = $menu_item->object;
|
134 |
+
$menu_item->url =get_post_type_archive_link($post_type);
|
135 |
+
|
136 |
+
return $menu_item;
|
137 |
+
}
|
138 |
+
|
139 |
+
public function maybe_make_current($items){
|
140 |
+
foreach ($items as $item){
|
141 |
+
if('post_type_archive' != $item->type)
|
142 |
+
continue;
|
143 |
+
|
144 |
+
$post_type = $item->object;
|
145 |
+
if(!is_post_type_archive($post_type)&& !is_singular($post_type))
|
146 |
+
continue;
|
147 |
+
|
148 |
+
//Make item current
|
149 |
+
$item->current = true;
|
150 |
+
$item->classes[] = 'current-menu-item';
|
151 |
+
|
152 |
+
//Get menu item's ancestors:
|
153 |
+
$_anc_id = (int) $item->db_id;
|
154 |
+
$active_ancestor_item_ids=array();
|
155 |
+
|
156 |
+
while(( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
|
157 |
+
! in_array( $_anc_id, $active_ancestor_item_ids ) )
|
158 |
+
{
|
159 |
+
$active_ancestor_item_ids[] = $_anc_id;
|
160 |
+
}
|
161 |
+
|
162 |
+
//Loop through ancestors and give them 'ancestor' or 'parent' class
|
163 |
+
foreach ($items as $key=>$parent_item){
|
164 |
+
$classes = (array) $parent_item->classes;
|
165 |
+
|
166 |
+
//If menu item is the parent
|
167 |
+
if ($parent_item->db_id == $item->menu_item_parent ) {
|
168 |
+
$classes[] = 'current-menu-parent';
|
169 |
+
$items[$key]->current_item_parent = true;
|
170 |
+
}
|
171 |
+
|
172 |
+
//If menu item is an ancestor
|
173 |
+
if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
|
174 |
+
$classes[] = 'current-menu-ancestor';
|
175 |
+
$items[$key]->current_item_ancestor = true;
|
176 |
+
}
|
177 |
+
|
178 |
+
$items[$key]->classes = array_unique( $classes );
|
179 |
+
}
|
180 |
+
|
181 |
+
}
|
182 |
+
return $items;
|
183 |
+
}
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
}
|
188 |
+
Harris_Post_Type_Archive_Link::load();
|
189 |
+
?>
|
readme.txt
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Post Type Archive Link ===
|
2 |
+
Contributors: stephenh1988
|
3 |
+
Tags: post type archives, menu link, archives, navigation,
|
4 |
+
Requires at least: 3.3
|
5 |
+
Tested up to: 3.3.1
|
6 |
+
Stable tag: 1.0
|
7 |
+
|
8 |
+
Creates a metabox to the Appearance > Menu page to add post type archive links
|
9 |
+
|
10 |
+
== Description ==
|
11 |
+
|
12 |
+
Post Type Archive LInk creates a metabox on the Appearance > Menu admin page. This lists your custom post types and allows you to add a link to their archive page onto your menu.
|
13 |
+
|
14 |
+
|
15 |
+
== Installation ==
|
16 |
+
|
17 |
+
Installation is standard and straight forward.
|
18 |
+
|
19 |
+
1. Upload `post-type-archive-links` folder (and all it's contents!) to the `/wp-content/plugins/` directory
|
20 |
+
1. Activate the plugin through the 'Plugins' menu in WordPress
|
21 |
+
1. The metabox wll appear at the bottom of your Appearance > Menu
|
22 |
+
|
23 |
+
|
24 |
+
== Frequently Asked Questions ==
|
25 |
+
|
26 |
+
= Why are some post types missing? =
|
27 |
+
|
28 |
+
The metabox will list only public custom post types
|
29 |
+
|
30 |
+
|
31 |
+
== Changelog ==
|
32 |
+
|
33 |
+
= 1.0 =
|
34 |
+
* Added plug-in
|