Skip to content

Instantly share code, notes, and snippets.

View kylephillips's full-sized avatar

Kyle Phillips kylephillips

  • Atlanta, Georgia
View GitHub Profile
@kylephillips
kylephillips / favorites-display.php
Last active October 14, 2022 19:33
Using the Favorites get_user_favorites() function to display favorited post data in a custom format
<?php
// Method 1: simple foreach loop
$favorites = get_user_favorites();
if ( isset($favorites) && !empty($favorites) ) :
foreach ( $favorites as $favorite ) :
// You'll have access to the post ID in this foreach loop, so you can use WP functions like get_the_title($favorite);
endforeach;
endif;
@kylephillips
kylephillips / location-categories.php
Last active August 29, 2015 14:23
Enable Categories for the Location Post Type (Simple Locator)
<?php
/**
* Enable Categories for the Location Post Type
*/
function enable_categories_in_locations()
{
register_taxonomy_for_object_type('category', 'location');
}
add_action( 'init', 'enable_categories_in_locations' );
@kylephillips
kylephillips / most-favorited.php
Last active July 27, 2020 03:11
Favorites for WordPress - Display the most favorited posts
<?php
/**
* Display a list of the 10 most favorited posts
* @see https://wordpress.org/plugins/favorites/
*/
$favorites_query = new WP_Query(array(
'post_type' => array('post'),
'posts_per_page' => 10,
'meta_key' => 'simplefavorites_count',
'orderby' => 'meta_value_num',
@kylephillips
kylephillips / favorites-user-list-filter.php
Created August 15, 2015 03:31
Example of using the User List filter in the Favorites WordPress plugin
add_filter('simplefavorites_user_list', 'filter_favorites_user_list', 10, 3);
function filter_favorites_user_list($output, $users, $anonymous_count)
{
$output = '<ul>';
foreach($users as $user){
$output .= '<li><a href="' . site_url() . '/members/' . $user->user_login . '">' . $user->display_name . '</a></li>';
}
$output .= '</ul>';
$output .= '(+' . $anonymous_count . ' Anonymous Users)';
return $output;
@kylephillips
kylephillips / favorites-logged-in.php
Created August 17, 2015 18:05
Show Favorites Button only for logged users
<?php
if ( is_user_logged_in() ) :
the_favorites_button();
else :
// Place any logged-out messaging/JS here
?>
<p>Please log in to save favorites</p>
<?php endif; ?>
@kylephillips
kylephillips / favorites-post-table.php
Created September 2, 2015 19:11
Add favorite counts to the WP admin post tables
<?php
// Place this code in your theme's functions.php file
/**
* First, add the column to the view
* Change the post type by substituting 'post' with the post type
* Ex: a post type of recipe would be manage_recipe_posts_columns
*/
add_filter('manage_post_posts_columns', 'add_favorite_count_column');
@kylephillips
kylephillips / nested-pages-query-filter.php
Created September 15, 2015 21:40
Apply a filter on the Nested Pages Query
<?php
/**
* Remove Posts from Being Displayed in the Nested Pages List
* @param $args - array of WP_Query arguments
* @see wp-nested-pages/app/Entities/Listing/Listing: Line 274
*/
add_filter('nestedpages_page_listing', 'remove_from_nested_pages');
function remove_from_nested_pages($args)
{
$remove_ids = array(); // Array of Post IDs to exclude
@kylephillips
kylephillips / favorites-button-styling.php
Created September 19, 2015 03:03
Add custom styling to the favorites button
<?php
/**
* Styling should ideally be placed in your theme's style.css file.
* If you are not comfortable editing this file, or with CSS, the following
* may be placed in your theme's functions.php
**/
add_action('wp_head', 'style_favorites_button');
function style_favorites_button()
{
echo '
@kylephillips
kylephillips / favorites-gravityform-notification.php
Last active June 18, 2020 16:04
Email a list of favorites using Gravity Forms
/**
* Allow a user to email a list of favorites
* Place in functions.php and update to suit your specific form and needs
* @link https://www.gravityhelp.com/documentation/article/gform_notification/
*/
add_filter( 'gform_notification_1', 'email_favorites_list', 10, 3 );
function email_favorites_list($notification, $form, $entry)
{
// Get the user's favorites and create a list
$favorites = get_user_favorites();
add_filter('simple_locator_result', 'location_result', 10, 3);
/**
* Filter the individual results in the list
* @param string $output – HTML to return
* @param object $result - SQL result. If custom fields have been added to the query using the JOIN filter, they will be available
* @param int $count - current result count
*/
function location_result($output, $result, $count)
{
$out = '<li>';