Created
November 15, 2013 19:55
-
-
Save trepmal/7490578 to your computer and use it in GitHub Desktop.
Nav Menu section for adding links to other blogs on network
This file contains 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: Site Home Links | |
* Plugin URI: | |
* Description: | |
* Version: | |
* Author: Kailey Lampert | |
* Author URI: kaileylampert.com | |
* License: GPLv2 or later | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* TextDomain: site-home-links | |
* DomainPath: | |
* Network: | |
*/ | |
$site_home_links = new Site_Home_Links(); | |
class Site_Home_Links { | |
function __construct() { | |
if ( ! is_multisite() ) return; | |
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( 'site_home_links', __( 'Site Links', 'site-home-links' ), array( &$this, 'add_meta_box' ), 'nav-menus', 'side', 'low' ); | |
} | |
function add_meta_box() { | |
echo '<ul id="site_home_links">'; | |
$list_key = 'site_list_cache'; | |
if ( false === ( $list = get_site_transient( $list_key ) ) ) { | |
$list = ''; | |
foreach( wp_get_sites() as $site ) { | |
switch_to_blog( $site['blog_id'] ); | |
$home = home_url(); | |
$title = get_bloginfo(); | |
$list .= "<li><label><input class='site-home-link' type='checkbox' value='$home' /><input type='hidden' value='{$title}' /> {$title}</label></li>"; | |
restore_current_blog(); | |
} | |
set_site_transient( $list_key, $list, 60*60*24*7 ); | |
} | |
echo $list; | |
echo '</ul>'; | |
?><p class="button-controls"> | |
<span class="list-controls"> | |
<a href="#" class="select-all-site-home-links"><?php _e('Select All', 'site-home-links' ); ?></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', 'site-home-links' ); ?>" id="submit-site-home-links" /> | |
<span class="spinner"></span> | |
</span> | |
</p><?php | |
} | |
function admin_footer() { | |
?><script> | |
jQuery('.select-all-site-home-links').click( function(ev) { | |
ev.preventDefault(); | |
checked = jQuery('.site-home-link:checked'); | |
all = jQuery('.site-home-link'); | |
//if all are checked, uncheck | |
if ( checked.length == all.length ) | |
jQuery('.site-home-link').click(); | |
//otherwise just check the unchecked | |
else | |
jQuery('.site-home-link:not(:checked)').click(); | |
}); | |
jQuery('#submit-site-home-links').click( function(ev) { | |
ev.preventDefault(); | |
jQuery('.site-home-link: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