为每一个WP开发者量身定做的10个WordPress代码段

jopen 9年前

  1. Add custom terms in admin column

If you want to add custom terms, to be visible within the admin section of your WordPress site/blog, then all you need to do is simply add the below code snippet to your theme’s functions.php file.

// for pages

add_filter(‘manage_pages_columns’, ‘term_columns_id’, 2);

add_action(‘manage_pages_custom_column’, ‘term_custom_id_columns’, 2, 1);

// for posts

add_filter(‘manage_posts_columns’, ‘term_columns_id’, 2);

add_action(‘manage_posts_custom_column’, ‘term_custom_id_columns’, 2, 1);

function term_columns_id($defaults){

$defaults[‘wps_post_type’] = __(‘Term’);

return $defaults;

}

function term_custom_id_columns($column_name, $id){

if($column_name === ‘wps_post_type’){

echo the_terms( $id, ‘TERM_NAME’, ”, ‘, ‘ );

}

}

In the above code snippet, make sure to replace TERM_NAME with the name of term that you want to be displayed within your WP admin area.

  1. Show or hide widgets on specific WordPress pages

There are times when you want to display a specific widget on your WP pages and situations when you want to hide the same for spceific pages. Mentioned below is a code snippet that will allow you to do just that. Contributed by Joel Worsham, this code snippet first expects you to fetch the id name of the widget that you intend to define. Once you’ve fetched this id, you can choose to implement the code snippet as explained below:

add_filter( ‘widget_display_callback’, ‘hide_widget_pages’, 5, 1 );

function hide_widget_pages( $instance, $widget, $args ) {

if ( $widget->id_base == ‘pages’ ) { // change ‘pages’ to widget name

if ( !is_page( ‘about’ ) ) { // change page name

return false;

}

}

}

  1. Exclude a set of pages from WordPress search results

WordPress will display results from pages included within your site. However, if you aren’t inclined on displaying specific pages to all users, then there’s a possible solution to it. For this, all you need to do is simply add the below code snippet to your functions.php file:

function filter_search($query) {

if ($query->is_search) {

$query->set(‘post_type’, ‘page’);

}

return $query;

}

add_filter(‘pre_get_posts’, ‘filter_search’);

  1. Increase WordPress memory limit

It is the php.ini file which controls the default memory limit in WordPress, which is 32MB. Since the installation of some specific plugins might result in error message i.e. out of memory, it becomes vital for you to modify the amount of memory WordPress uses. For this, you simply need to change the value of WP_MEMORY_LIMIT to 24MB, 128MB, 256MB etc. within the WP configuration file as shown in below code snippet:

define(‘WP_MEMORY_LIMIT’, ‘256M’);

  1. Remove URL Field from the Comment Form

As a viable means of preventing manual comment spam, you can opt for removing the URL field from the comment forms available for your posts. For this, all you need to do is simply add the below function to your WP theme’s functions.php file:

function remove_comment_fields($fields) {

unset($fields[‘url’]);

return $fields;

}

add_filter(‘comment_form_default_fields’,’remove_comment_fields’);

  1. Crop all updated images(with sizes as standard, medium and large)

If you want to crop all the images uploaded to your WP site/blog, instead of scaling them, then you simply need to add the below code snippet to your theme’s functions.php file:

// Standard Size Thumbnail

if(false == get_option(“thumbnail_crop”)) {

add_option(“thumbnail_crop”, “1”); }

else {

update_option(“thumbnail_crop”, “1”);

}

 

// Medium Size Thumbnail

if(false === get_option(“medium_crop”)) {

add_option(“medium_crop”, “1”); }

else {

update_option(“medium_crop”, “1”);

}

 

// Large Size Thumbnail

if(false === get_option(“large_crop”)) {

add_option(“large_crop”, “1”); }

else {

update_option(“large_crop”, “1”);

}

  1. Clean up the wp_head() hook without using a WP plugin

The wp_head() hook available within the WP theme allows you to do some brilliant stuff with your site/blog. But, there might be situations when this stuff might interfere with your portal’s overall performance. Here, it is advised to clean up the wp_head(). For this, all you need to do is paste the below code snippet in the functions.php file:

remove_action( ‘wp_head’, ‘rsd_link’ );

remove_action( ‘wp_head’, ‘wlwmanifest_link’ );

remove_action( ‘wp_head’, ‘wp_generator’ );

remove_action( ‘wp_head’, ‘start_post_rel_link’ );

remove_action( ‘wp_head’, ‘index_rel_link’ );

remove_action( ‘wp_head’, ‘adjacent_posts_rel_link’ );

remove_action( ‘wp_head’, ‘wp_shortlink_wp_head’ );

  1. Block admin for all users but administrator

If you want to black all users excluding the site administrators from accessing wp-admin, then you simply need to add the below snippet to your theme’s functions.php file:

add_action( ‘init’, ‘blockusers_wps_init’ );

function blockusers_wps_init() {

if ( is_admin() && ! current_user_can( ‘administrator’ ) ) {

wp_redirect( home_url() );

exit;

}

}

  1. Add content to end of each RSS post in WordPress

At times, you might feel the need for adding a specific piece of content towards the bottom of every post within your RSS Feed. It is here that adding the below snippet to the functions,php file serves as a handy option:

function feedFilter($query) {

if ($query->is_feed) {

add_filter(‘the_content’,’feedContentFilter’);

}

return $query;

}

add_filter(‘pre_get_posts’,’feedFilter’);

 

function feedContentFilter($content) {

$content .= ‘<span>Extra RSS content goes here… </span>';

 

return $content;

}

  1. Add custom text to WordPress Login Page

If you’re inclined on displaying a custom message on your WordPress login page, then simply paste the below mentioned code in your functions.php file:

function wps_login_message( $message ) {

if ( empty($message) ){

return “<p class=’message’>Login to browse the website</p>”;

} else {

return $message;

}

}

add_filter( ‘login_message’, ‘wps_login_message’ );

That’s it!