10 Essential WordPress Snippets You Need to Know

Remove or Hide Admin Menu Links for Certain or Specific Users or Roles

The following snippet will let you remove as well as hide admin menu links for certain users or role.

if (current_user_can('hide_jobs')) {

add_action( 'admin_menu', 'my_remove_menu_jobs' );

function my_remove_menu_jobs() {

remove_menu_page('edit.php?post_type=job');

remove_menu_page('edit.php?post_type=solution');

remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); // right now

}
}

Get all media attached to a post

The following snippet will allow you to get all media attached to a post

$args = array(

'post_parent' => $post->ID,

'post_type' => 'attachment',

'post_mime_type' => 'image',

'posts_per_page' => -1,

'orderby' => 'menu_order',

'order' => 'ASC',

);

$attachments = get_children( $args )

Filter Category from Blog Page

The following snippet will filter category from the blog page

function exclude_cat($query) {
if ( $query->is_home) {
$query-> set('cat','-611');
}
return $query;
}
add_filter('pre_get_posts','exclude_cat');

Custom Admin Login Logo & Alt Text

The snippet will allow you to have custom admin login logo and alt text

function change_wp_login_title()
{
echo get_option('blogname'); // OR ECHO YOUR OWN ALT TEXT
 
}add_filter('login_headertitle', 'change_wp_login_title');

Prev/Next wordpress loop links

Add the following snippet to previous/next loop links

<div class="navigation"><p><?php posts_nav_link(); ?></p></div>

Admin Footer Modification

function remove_footer_admin ()
{
echo '<span id="footer-thankyou">Developed by <a href="http://www.designerswebsite.com" target="_blank">Your Name</a></span>';
}
add_filter('admin_footer_text', 'remove_footer_admin');

Query a custom taxonomy

The following snippet will allow you to query a custom taxonomy, using the taxquery parameter

<?php
// Example query to select all posts belonging to a custom taxonomy.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'TAXONOMY_NAME',
'field' => 'slug',
'terms' => 'TAXONOMY_SLUG'
)
)
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

Remove Personal Options from Profile Menu

The following snippet will allow you to remove personal options from personal menu

function hide_personal_options(){
echo "\n" . '<script type="text/javascript">jQuery(document).ready(function($) { $(\'form#your-profile > h3:first\').hide(); $(\'form#your-profile > table:first\').hide(); $(\'form#your-profile\').show(); });</script>' . "\n";
}
add_action('admin_head','hide_personal_options');

Exclude Category from Feed

The following snippet will allow you to exclude category from feed

function customFeedquery($query) {
if(is_feed()) {
$query->set('cat','-8'); // exclude category 8
return $query;
}
}
add_filter('pre_get_posts', 'customFeedquery');

Load jQuery from CDN, fallback to local

Add the following snippet to your theme’s functions.php file

/**
* Load jQuery from Google CDN, fallback to local
*/
// http://wp.tutsplus.com/tutorials/load-jquery-from-google-cdn-with-local-fallback-for-wordpress/
if( !is_admin()){ // Don't do this for admin area, since Google's jQuery isn't in noConflict mode and will interfere with WP's admin area.
$url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'; // the URL to check against
$test_url = @fopen($url,'r'); // test parameters
if($test_url !== false) { // test if the URL exists
function load_external_jQuery() { // load external file
wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'); // register the external file
wp_enqueue_script('jquery'); // enqueue the external file
}
add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function
} else {
function load_local_jQuery() {
wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
wp_register_script('jquery', get_bloginfo('template_url').'/js/jquery-1.6.2.min.js', __FILE__, false, '1.6.2', true); // register the local file
wp_enqueue_script('jquery'); // enqueue the local file
}
add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function
}
}

Leave a Comment

× How can I help you?