-
-
Save pareshsojitra/9a6458c07478b5c0b266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Plugin Name: Faster CPT Archive Custom Links | |
$faster_archive_links = new Faster_Archive_Links(); | |
class Faster_Archive_Links { | |
function __construct() { | |
add_action( 'admin_init', array( &$this, 'admin_init' ) ); | |
add_action( 'admin_footer-nav-menus.php', array( &$this, 'admin_footer' ) ); | |
} | |
function admin_init() { | |
add_meta_box( 'nme-test', 'Post Type Archives', array( &$this, 'add_meta_box' ), 'nav-menus', 'side', 'low' ); | |
} | |
function add_meta_box() { | |
$post_types = get_post_types(array('publicly_queryable' => true, 'has_archive' => true), 'object' ); | |
if ( ! empty( $post_types ) ) { | |
echo '<ul id="post_type_archives">'; | |
foreach ( $post_types as $slug => $object ) { | |
$archive = get_post_type_archive_link( $slug ); | |
if ( ! empty( $archive ) ) { | |
// $archive = home_url( "?post_type=$slug" ); | |
echo "<li><label><input class='post-type-archive' type='checkbox' value='$archive' /><input type='hidden' value='{$object->labels->name}' /> {$object->labels->name}</label></li>"; | |
} | |
} | |
echo '</ul>'; | |
?><p class="button-controls"> | |
<span class="list-controls"> | |
<a href="#" class="select-all-post-type-archives"><?php _e('Select All'); ?></a> | |
</span> | |
<span class="add-to-menu"> | |
<input type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to Menu'); ?>" name="add-taxonomy-menu-item" id="submit-post-type-archives" /> | |
<span class="spinner"></span> | |
</span> | |
</p><?php | |
} | |
} | |
function admin_footer() { | |
?><script> | |
jQuery('.select-all-post-type-archives').click( function(ev) { | |
ev.preventDefault(); | |
checked = jQuery('.post-type-archive:checked'); | |
all = jQuery('.post-type-archive'); | |
//if all are checked, uncheck | |
if ( checked.length == all.length ) | |
jQuery('.post-type-archive').click(); | |
//otherwise just check the unchecked | |
else | |
jQuery('.post-type-archive:not(:checked)').click(); | |
}); | |
jQuery('#submit-post-type-archives').click( function(ev) { | |
ev.preventDefault(); | |
jQuery('.post-type-archive:checked').each( function() { | |
url = jQuery(this).val(); | |
label = jQuery(this).next().val(); | |
wpNavMenu.addLinkToMenu( url, label, wpNavMenu.addMenuItemToBottom, function() {} ); | |
}); | |
}); | |
</script><?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment