Skip to content

Instantly share code, notes, and snippets.

@agustibr
Created October 22, 2011 00:48
Show Gist options
  • Save agustibr/1305368 to your computer and use it in GitHub Desktop.
Save agustibr/1305368 to your computer and use it in GitHub Desktop.
WP Collection of Code for functions.php [2]
<?php
// Change URL Slug from Author to Sellers
function new_author_base() {
global $wp_rewrite;
$author_slug = 'sellers';
$wp_rewrite->author_base = $author_slug;
}
add_action('init', 'new_author_base');
//only run once
<?php
// Changing "Posts" menu name in admin to whatever you wish (e.g. "Articles")
// hook the translation filters
add_filter('gettext','change_post_to_article');
add_filter('ngettext','change_post_to_article');
function change_post_to_article( $translated ) {
$translated = str_ireplace('Post','Article',$translated );// ireplace is PHP5 only
return $translated;
}
<?php
//Arguments are the same as the ones from query_posts. The content wrapped between the query tag is the template.
add_shortcode('query', 'shortcode_query');
function shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values
'posts_per_page' => '10',
'caller_get_posts' => 1,
'post__not_in' => get_option('sticky_posts'),
), $atts));
global $post;
$posts = new WP_Query($atts);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
// these arguments will be available from inside $content
$parameters = array(
'PERMALINK' => get_permalink(),
'TITLE' => get_the_title(),
'CONTENT' => get_the_content(),
'COMMENT_COUNT' => $post->comment_count,
'CATEGORIES' => get_the_category_list(', '),
// add here more...
);
$finds = $replaces = array();
foreach($parameters as $find => $replace):
$finds[] = '{'.$find.'}';
$replaces[] = $replace;
endforeach;
$output .= str_replace($finds, $replaces, $content);
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($output);
}
/***
Usage:
[query post_type=page posts_per_page=5]
Listing some pages:
<h5>{TITLE}</h5>
<div>{CONTENT}</div>
<p><a href="{PERMALINK}">{COMMENT_COUNT} comments</a></p>
[/query]
***/
<?php
/****
Display Posts from Different Custom Post Types on homepage
By dropping the following code on the bottom of this post into your functions.php file you can get wordpress to automatically display posts from different custom post types you have created. Currently by default wordpress only display posts which belong to the default "posts" post type.
In the example provided below you will need to change the section which calls:
***/
$query->set( 'post_type', array('post', 'page', 'services', 'attachment'));
/***
With your own custom post types which you would like to include in the homepage post list results. In this case we are asking wordpress to return to us all posts belonging to the default "post" and "page" post_type then asking wordpress to also include the custom post type we created for "services" and finally the default wordpress post type of "attachment" which just means that anytime something is added to the media library it will automatically get displayed on the homepage as a separate entry.
***/
// CUSTOM HOMEPAGE POST LIST INCLUDING DIFFERENT POST_TYPES
// make sure to edit the post types you wanted included in the list below
add_filter( 'pre_get_posts', 'my_homepage_post_list' );
function my_homepage_post_list ( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'post_type', array('post', 'page', 'services', 'attachment'));
return $query;
}
/**
You can also utilize this custom query in different locations, such as in a custom feed through something like this
**/
if (( is_home() && false == $query->query_vars['suppress_filters'] ) || is_feed())
<?php
// MOVE THE AUTHOR METABOX INTO THE PUBLISH METABOX
add_action( 'admin_menu', 'remove_author_metabox' );
add_action( 'post_submitbox_misc_actions', 'move_author_to_publish_metabox' );
function remove_author_metabox() {
remove_meta_box( 'authordiv', 'post', 'normal' );
}
function move_author_to_publish_metabox() {
global $post_ID;
$post = get_post( $post_ID );
echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
post_author_meta_box( $post );
echo '</div>';
}
<?php
// Display DB Queries, Time Spent and Memory Consumption
function performance( $visible = false ) {
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "<!-- {$stat} -->" ;
}
//(make sure your theme is calling wp_footer):
add_action( 'wp_footer', 'performance', 20 );
<?php
// REMOVE META BOXES FROM DEFAULT POSTS SCREEN
function remove_default_post_screen_metaboxes() {
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_post_screen_metaboxes');
// REMOVE META BOXES FROM DEFAULT PAGES SCREEN
function remove_default_page_screen_metaboxes() {
global $post_type;
remove_meta_box( 'postcustom','page','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','page','normal' ); // Excerpt Metabox
remove_meta_box( 'commentstatusdiv','page','normal' ); // Comments Metabox
remove_meta_box('commentsdiv','page','normal'); // Comments
remove_meta_box( 'trackbacksdiv','page','normal' ); // Talkback Metabox
remove_meta_box( 'slugdiv','page','normal' ); // Slug Metabox
remove_meta_box( 'authordiv','page','normal' ); // Author Metabox
}
add_action('admin_menu','remove_default_page_screen_metaboxes');
<?php
// Remove Private and Protected Prefix. This function removes the "Privite:" prefix from posts and pages marked private.
function the_title_trim($title) {
$title = attribute_escape($title);
$findthese = array(
'#Protected:#',
'#Private:#'
);
$replacewith = array(
'', // What to replace "Protected:" with
'' // What to replace "Private:" with
);
$title = preg_replace($findthese, $replacewith, $title);
return $title;
}
add_filter('the_title', 'the_title_trim');
<?php
/**
New Roles and Capabilities - Only run once!
I keep these handy, this is the right way to do them without a plugin. They set a single field (prefix_user_roles) in the options database, and you don't need a plugin to set them. Refer to the Codex page for a list of what capabilities are available and descriptions for what they do. You only need to uncomment one of these blocks, load any page and then comment them again! Here I'm creating a role that's got the capabilities I need:
*/
/* Capabilities */
// To add the new role, using 'international' as the short name and
// 'International Blogger' as the displayed name in the User list and edit page:
/*
add_role('international', 'International Blogger', array(
'read' => true, // True allows that capability, False specifically removes it.
'edit_posts' => true,
'delete_posts' => true,
'edit_published_posts' => true,
'publish_posts' => true,
'edit_files' => true,
'import' => true,
'upload_files' => true //last in array needs no comma!
));
*/
// To remove one outright or remove one of the defaults:
/*
remove_role('international');
*/
/**
It's sometimes handy to add/remove from an existing role rather than removing and re-adding one. Again, you only need to uncomment it, reload a page and then comment it again. This will store the role/capability properly in the options table. (This allows you, the developer to control them and removes the overhead of the bulky plugins that do the same thing.) Here I'm changing the author role to delete their published posts (the default), but allowing them the capability to edit their published posts (which isn't possible for this role by default)-- using *add_cap* or *remove_cap*.
**/
/*
$edit_role = get_role('author');
$edit_role->add_cap('edit_published_posts');
$edit_role->remove_cap('delete_published_posts');
*/
<?php
// 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');
unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
}
add_action('widgets_init', 'unregister_default_wp_widgets', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment