Skip to content

Instantly share code, notes, and snippets.

View antoniofrignani's full-sized avatar
👨‍💻

Antonio Frignani antoniofrignani

👨‍💻
View GitHub Profile
@antoniofrignani
antoniofrignani / gist:2867521
Created June 4, 2012 09:44
[WP] - Add youtube thumbnail posts with custom metabox
// http://wpsnipp.com/index.php/functions-php/add-youtube-thumbnail-posts-with-custom-metabox/
add_action("admin_init", "youtube_init");
add_action('save_post', 'save_youtube_link');
function youtube_init(){
add_meta_box("youtube", "Youtube thumbnail code", "youtube_link", "post", "normal", "high");
}
function youtube_link(){
global $post;
$custom = get_post_custom($post->ID);
@antoniofrignani
antoniofrignani / gist:2867522
Created June 4, 2012 09:45
[WP] - Search category with dropdown
<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>">
<div>
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
in <?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
@antoniofrignani
antoniofrignani / gist:2867523
Created June 4, 2012 09:47
[WP] - Attach images with simple thumbnail selection metabox
// http://wpsnipp.com/index.php/functions-php/attach-images-with-simple-thumbnail-selection-metabox/
add_action("admin_init", "images_init");
add_action('save_post', 'save_images_link');
function images_init(){
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
add_meta_box("my-images", "Pictures", "images_link", $post_type, "normal", "low");
}
}
@antoniofrignani
antoniofrignani / gist:2867527
Created June 4, 2012 09:48
[WP] - Search a specific post type
// http://wpsnipp.com/index.php/posts/search-a-specific-post-type/
function SearchFilter($query) {
if ($query->is_search) {
// Insert the specific post type you want to search
$query->set('post_type', 'feeds');
}
return $query;
}
// This filter will jump into the loop and arrange our results before they're returned
@antoniofrignani
antoniofrignani / gist:2867532
Created June 4, 2012 09:49
[WP] - Filter search results by post type
// http://wpsnipp.com/index.php/functions-php/filter-search-results-by-post-type/
function search_posts_filter( $query ){
if ($query->is_search){
$query->set('post_type',array('post','custom_post_type1', 'custom_post_type2'));
}
return $query;
}
add_filter('pre_get_posts','search_posts_filter');
@antoniofrignani
antoniofrignani / gist:2867534
Created June 4, 2012 09:49
[WP] - Unregister default widgets
// http://wpsnipp.com/index.php/calendar/unregister-default-widgets/
// unregister all default WP Widgets
function unregister_default_wp_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
@antoniofrignani
antoniofrignani / gist:2867537
Created June 4, 2012 09:50
[WP] - Add select menu to filter by custom field in admin
// http://wpsnipp.com/index.php/functions-php/add-select-menu-to-filter-by-custom-field-in-admin/
add_filter( 'parse_query', 'ba_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );
function ba_admin_posts_filter( $query )
{
global $pagenow;
if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') {
$query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
@antoniofrignani
antoniofrignani / gist:2867542
Created June 4, 2012 09:51
[WP] - Rewrite the search results slug to /search/term
// http://wpsnipp.com/index.php/functions-php/rewrite-the-search-results-slug-search-term/
function search_url_rewrite_rule() {
if ( is_search() && !empty($_GET['s'])) {
wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
exit();
}
}
add_action('template_redirect', 'search_url_rewrite_rule');
@antoniofrignani
antoniofrignani / gist:2867546
Created June 4, 2012 09:54
[WP] - Track post views without a plugin using post meta
// http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/
// functions.php
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
@antoniofrignani
antoniofrignani / gist:2867556
Created June 4, 2012 09:57
[WP] - Create multiple search templates for custom post types
// http://wpsnipp.com/index.php/template/create-multiple-search-templates-for-custom-post-types/
/*
Create a new file called search.php and add the following search template.
Change the $search_refer= CUSTOM_POST_TYPE to the names of your post types.
You will also need to change the template path to the corresponding template you wish to display results.
*/
<?
/* Template Name: Search Results */