All posts by acrane

About acrane

A web designer from Charlotte, NC, I enjoy working in the yard, kayaking, building stuff, making things, creating, thinking and of course WordPress.

Remove the main site from the “My Sites” dropdown in the toolbar

Remove the Main Site from the My Sites dropdown in the WordPress tool bar in a Multisite install

// remove main site from dropdown
function remove_main_blog_from_get_blogs($blogs) {
	// check that user is not Network Admin
	if ( !is_super_admin() ) {
		// remove first item from $blogs array, which should always be main blog
		array_splice($blogs, 0, 1);
	}
	return $blogs;
}
add_filter( 'get_blogs_of_user', 'remove_main_blog_from_get_blogs' );

 

Add Menus to the WordPress Toolbar

Add menu items to the WordPress Toolbar, the little black bar at the top of the page when logged in. An example would be if you wanted to add a list of custom post types to a standalone dropdown. Remember to change your “custom_post_type”.

// Add new node
add_action( 'admin_bar_menu', 'add_admin_links', 500 );

function add_admin_links( $wp_admin_bar ) {

  $args = array(
    'id' => 'admin-links',
    'title' => __( 'Add Things' ),
    'href' => false
  );

  $wp_admin_bar->add_node($args);
  
  $blogurl = get_bloginfo('url');

// Add child items
$wp_admin_bar->add_node( array(
    'parent' => 'admin-links',
    'title' => 'Add a Custom Post Type',
    'href' => $blogurl . '/wp-admin/post-new.php?post_type=custom_post_type',
    'meta' => FALSE) );
$wp_admin_bar->add_node( array(
    'parent' => 'admin-links',
    'title' => 'Add a Page',
    'href' => $blogurl . '/wp-admin/post-new.php?post_type=page',
    'meta' => FALSE) );

}

 

Remove WordPress left menu items in Admin

Removes some of the Admin Menu items on the left when in the WordPress admin area.

/*
		 hide other menu items
==========================================
*/
add_action( 'admin_menu', 'remove_menus' );
	function remove_menus(){
	 if ( !current_user_can('update_core') ) { 
	  remove_menu_page( 'index.php' );                  //Dashboard
	  remove_menu_page( 'edit.php' );                   //Posts
	  remove_menu_page( 'upload.php' );                 //Media
	  //remove_menu_page( 'edit.php?post_type=page' );    //Pages
	  //remove_menu_page( 'edit-comments.php' );          //Comments
	  remove_menu_page( 'themes.php' );                 //Appearance
	  remove_menu_page( 'plugins.php' );                //Plugins
	  remove_menu_page( 'users.php' );                  //Users
	  remove_menu_page( 'tools.php' );                  //Tools
	  remove_menu_page( 'options-general.php' );        //Settings
	 // remove_menu_page( 'edit.php?post_type=acf' );     //ACF
	  //remove_menu_page('edit.php?post_type=acf');
	  
	}
}

 

Remove WordPress Dashboard items

This function pretty much removes all Dashboard windows so you can comment out the ones you want to keep.

 /*
		 hide Dashboard items
==========================================
*/
 function remove_dashboard_meta() {
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'remove_dashboard_meta' );

 

Redirect a WordPres user to a specific location after login

This function will direct the user to the homepage not the dashboard after logging into WordPress

function my_login_redirect( $redirect_to, $request, $user ) {
	//is there a user to check?
	global $user;
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		//check for admins
		if ( in_array( 'administrator', $user->roles ) ) {
			// redirect them to the default place
			return $redirect_to;
		} else {
			return home_url();
		}
	} else {
		return $redirect_to;
	}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

 

Remove Toolbar options on WordPress Multisite

How to remove the dropdowns in your toolbar, the little black bar at the top of the page you see when you’re logged in, in a WordPress multisite install.  Many like to customize the experience a user gets when they sign up for a site so this function comes in handy.

These are most of them, but you can find more by viewing the source code and searching for wp-admin-bar- the id is the stuff that comes right after that in each case. So to remove the WP logo, you’ll see wp-admin-bar-wp-logo. “wp-logo” is the id and you use that to remove it in the function.

Slightly more involved is the foreach loop in the function that removes the new posts link that drops down from the “My Sites” Nav item. For example, I used some custom post types and not the “post” post type, so I wanted to delete that and keep the My Sites dropdown super simple. So it checks for all blogs created by the user in the foreach and adds them to their own unique id to remove them.

 

add_action( 'admin_bar_menu', 'remove_toolbar_items', PHP_INT_MAX -1 );
function remove_toolbar_items( $bar )
{
    $sites = get_blogs_of_user( get_current_user_id() );
    foreach ( $sites as $site )
    {
       $bar->remove_node( "blog-{$site->userblog_id}-n" ); // posts 
		$bar->remove_node( "blog-{$site->userblog_id}-c" ); // comments
       // $bar->remove_node( "blog-{$site->userblog_id}-d" ); // dashboard
    }
    $bar->remove_node( 'new-media' ); // +Post
    $bar->remove_node( 'wp-logo' ); // WP logo
	$bar->remove_node( 'new-user' ); // New User
	$bar->remove_node( 'new-post' ); // New Post
	$bar->remove_node( 'new-content' ); // ?
	$bar->remove_node( 'comments' ); // Comments bubble
}

 

Advanced Custom Fields Google Maps with Info Marker

Must have CSS:

.acf-map {
	width: 100%;
	height: 400px;
	border: #ccc solid 1px;
	margin: 20px 0;
}

Must Have Javascript:

(function($) {

/*
*  render_map
*
*  This function will render a Google Map onto the selected jQuery element
*
*  @type	function
*  @date	8/11/2013
*  @since	4.3.0
*
*  @param	$el (jQuery element)
*  @return	n/a
*/

function render_map( $el ) {

	// var
	var $markers = $el.find('.marker');

	// vars
	var args = {
		zoom		: 16,
		center		: new google.maps.LatLng(0, 0),
		mapTypeId	: google.maps.MapTypeId.ROADMAP
	};

	// create map	        	
	var map = new google.maps.Map( $el[0], args);

	// add a markers reference
	map.markers = [];

	// add markers
	$markers.each(function(){

    	add_marker( $(this), map );

	});

	// center map
	center_map( map );

}

/*
*  add_marker
*
*  This function will add a marker to the selected Google Map
*
*  @type	function
*  @date	8/11/2013
*  @since	4.3.0
*
*  @param	$marker (jQuery element)
*  @param	map (Google Map object)
*  @return	n/a
*/

function add_marker( $marker, map ) {

	// var
	var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

	// create marker
	var marker = new google.maps.Marker({
		position	: latlng,
		map			: map
	});

	// add to array
	map.markers.push( marker );

	// if marker contains HTML, add it to an infoWindow
	if( $marker.html() )
	{
		// create info window
		var infowindow = new google.maps.InfoWindow({
			content		: $marker.html()
		});

		// show info window when marker is clicked
		google.maps.event.addListener(marker, 'click', function() {

			infowindow.open( map, marker );

		});
	}

}

/*
*  center_map
*
*  This function will center the map, showing all markers attached to this map
*
*  @type	function
*  @date	8/11/2013
*  @since	4.3.0
*
*  @param	map (Google Map object)
*  @return	n/a
*/

function center_map( map ) {

	// vars
	var bounds = new google.maps.LatLngBounds();

	// loop through all markers and create bounds
	$.each( map.markers, function( i, marker ){

		var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );

		bounds.extend( latlng );

	});

	// only 1 marker?
	if( map.markers.length == 1 )
	{
		// set center of map
	    map.setCenter( bounds.getCenter() );
	    map.setZoom( 16 );
	}
	else
	{
		// fit to bounds
		map.fitBounds( bounds );
	}

}

/*
*  document ready
*
*  This function will render each map when the document is ready (page has loaded)
*
*  @type	function
*  @date	8/11/2013
*  @since	5.0.0
*
*  @param	n/a
*  @return	n/a
*/

$(document).ready(function(){

	$('.acf-map').each(function(){

		render_map( $(this) );

	});

});

})(jQuery);

Must have link to Google Maps API:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

Create your Advanced Custom Field, “locations”

It will return an array with, “lat” “lng” and “address”

You can create an example with a info window when you click the marker like this: (single marker type)

 <div class="googlemap">
   <?php 
	$location = get_field('locations');
		if( !empty($location) ): ?>
			<div class="acf-map">
				<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
                <div class="markerinfo">
                <strong><?php echo $location['address']; ?></strong><br>
                	<a href="https://www.google.com/maps/search/<?php echo $location['address']; ?>">Get Directions</a>
                </div><!-- narker info -->
                </div><!-- marker -->
			</div><!-- acf map -->
		<?php endif; ?>
  </div><!-- google map -->

 

Custom post Type Query order by Date picker ACF

Query Custom Post Type with an Advanced Custom Fields Date Picker and order by that date.

<?php
	// current date
    $thedate = date("Ymd"); 
	
	// Start query
	$wp_query = new WP_Query();
    $wp_query->query(array(
    'post_type'=>'book_signing',
    'posts_per_page' => 10,
    'paged' => $paged,
	'meta_key' => 'date_of_signing',
    'meta_value' => $thedate,
    'meta_compare' => '>',
    'orderby' => 'meta_value',
    'order' => 'ASC'
));
    if ($wp_query->have_posts()) : ?>
    <?php while ($wp_query->have_posts()) : ?>
        
    <?php $wp_query->the_post(); ?>
    
    <?php 
		// Set some variables to set how to show the dates.
	$eventdate = DateTime::createFromFormat('Ymd', get_field('date_of_signing'));

 	?>
					
			<div class="booksigning">
             <h2><?php the_title(); ?></h2>
             <div class="eventdate"><?php echo $eventdate->format('M d'); ?></div>
            
            </div><!-- book signing -->		
                    
    <?php endwhile; ?>
        
        <div class="clear"></div>
    		<?php pagi_posts_nav(); ?>
        
    <?php endif; ?>

 

Sortable Admin Columns for Custom Post Types in WordPress

/*-------------------------------------------------------------------------------
	Sortable Columns
-------------------------------------------------------------------------------*/

// add filter to output your heading titles = the names at the top where you sort
add_filter( 'manage_edit-customposttype_columns', 'my_edit_booksigning_columns' ) ;

function my_edit_booksigning_columns( $columns ) {

	$columns = array(
		'cb' => '<input type="checkbox" />',
		'title' => __( 'Book Signing Location' ),
		'startdate' => __( 'Book Signing Date' ),
		//'courselocation' => __( 'Book Signing Location' ),
		//'date' => __( 'Date' )
	);

	return $columns;
}


// action to pull in Advaced Custom Fields so you can sort by them
add_action( 'manage_book_customposttype_custom_column', 'my_manage_booksigning_columns', 10, 2 );

function my_manage_booksigning_columns( $column ) {
	global $post;

	
	if($column == 'startdate')
	{
		// Set some variables to set how to show the dates.
		$startdate = DateTime::createFromFormat('Ymd', get_field('date_of_signing'));
		
		echo $startdate->format('n - d') . " &raquo; " . $startdate->format('M d');
		
	}
	elseif($column == 'courselocation')
	{
		$location = get_field('location');
		echo $location;
	}
}


/*-------------------------------------------------------------------------------
	Sortable 
-------------------------------------------------------------------------------*/

function my_column_register_sortable( $columns )
{
	$columns['startdate'] = 'startdate';
	return $columns;
}

add_filter("manage_edit-customposttype_sortable_columns", "my_column_register_sortable" );


/* Only run our customization on the 'edit.php' page in the admin. */
add_action( 'load-edit.php', 'my_edit_booksigning_load' );

function my_edit_booksigning_load() {
	add_filter( 'request', 'my_sort_booksigning' );
}

/* Sorts the book_signings. */
function my_sort_booksigning( $vars ) {

	/* Check if we're viewing the 'book_signings' post type. */
	if ( isset( $vars['post_type'] ) && 'book_signings' == $vars['post_type'] ) {

		/* Check if 'orderby' is set to 'duration'. */
		if ( isset( $vars['orderby'] ) && 'startdate' == $vars['orderby'] ) {

			/* Merge the query vars with our custom variables. */
			$vars = array_merge(
				$vars,
				array(
					'meta_key' => 'date_of_signing',
					'orderby' => 'meta_value_num'
				)
			);
		}
	}

	return $vars;
}

 

FAQ Toggle Sample jquery

Sample code to get those nice FAQ page toggles. Answer is revealed on click.

Here is the CSS

.faqrow {
	width: auto;
	overflow: hidden;
	margin: 0 0 20px 0;	
	background-color: rgba(255,255,255,.05);
	border-radius: 10px;
}
.question {
	width: auto;
	float:left;
	margin: 10px;
	font-family: 'Muli', sans-serif;
	font-weight: bold;
	font-size: 20px;
	cursor:pointer;	
	line-height: 1.3;
	position: relative;
	padding: 0 0 0 20px ;
}
.question-image {
	width: 15px;
	height: 15px;
	position: absolute;
	left: 0px;
	top: 3px;
	background-image: url(images/faq.png);
	background-repeat: no-repeat;
	background-position: 0 0;	
}
.question-image.close {
	background-position: -15px 0;	
}
.answer {
	width: auto;
	float:left;
	margin: 10px;
	font-family: 'Muli', sans-serif;
	font-size: 14px;
	line-height: 1.3;
	padding: 0 0 0 40px;
	display:none;
}

This is an Advanced Custom Field Repeater field for FAQ’s on the FAQ page.  ( ACF Repeater for FAQ’s )

<?php /*
     ------------------------------------
        FAQ's
   ------------------------------------*/ ?>
 <?php if( have_rows('faqs') ): ?>
     <?php while ( have_rows('faqs') ) : ?>
         <?php the_row(); ?>
                    
            <div class="faqrow">
               <div class="question"><div class="question-image"></div><?php the_sub_field('question'); ?></div>
               <div class="answer"><?php the_sub_field('answer'); ?></div>
            </div><!-- faqrow -->
<?php endwhile; endif; // end faq's ?>

Finally, the jQuery that makes it toggle.

/*
		FAQ dropdowns
__________________________________________
*/
$('.question').click(function() {

    $(this).next('.answer').slideToggle(500);
    $(this).toggleClass('close');

});