我们的wordpress网站在发展过程中,会添加很多外部链接,比如我们自己的广告外链,网站预览外链,如果时间长了太多的跳出外链可能会导致网站分权,百度权重降低。我们可以用go跳转方法强制将外链转换为我们的内链,在此给大家推荐一款插件:simple-urls
当然我比较喜欢用代码来实现,不喜欢装太多插件.下面来分享下代码
打开主题的functions.php
文件,插入以下代码即可
// 代码实现短网址功能-vlwx.com class SimpleURLs { // Constructor function __construct() { //register_activation_hook( __FILE__, 'flush_rewrite_rules' ); add_action( 'init', array( &$this, 'register_post_type' ) ); add_action( 'manage_posts_custom_column', array( &$this, 'columns_data' ) ); add_filter( 'manage_edit-surl_columns', array( &$this, 'columns_filter' ) ); add_action( 'admin_menu', array( &$this, 'add_meta_box' ) ); add_action( 'save_post', array( &$this, 'meta_box_save' ), 1, 2 ); add_action( 'template_redirect', array( &$this, 'count_and_redirect' ) ); } // PHP4 Constructor function SimpleURLs() { $this->__construct(); } function register_post_type() { register_post_type( 'surl', array( 'labels' => array( 'name' => __( 'Simple URLs' ), 'singular_name' => __( 'URL' ), 'add_new' => __( 'Add URL' ), 'add_new_item' => __( '新建URL' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit URL' ), 'new_item' => __( 'New URL' ), 'view' => __( 'View URL' ), 'view_item' => __( 'View URL' ), 'search_items' => __( 'Search URL' ), 'not_found' => __( 'No URLs found' ), 'not_found_in_trash' => __( '' ) ), 'public' => true, 'query_var' => true, 'menu_position' => 20, 'supports' => array( 'title' ), 'rewrite' => array( 'slug' => 'go', 'with_front' => false ) ) ); } function columns_filter( $columns ) { $columns = array( 'cb' => '<input type="checkbox" />', 'title' => __('标题'), 'url' => __('原地址'), 'permalink' => __('短地址'), 'clicks' => __('点击量') ); return $columns; } function columns_data( $column ) { global $post; $url = get_post_meta($post->ID, '_surl_redirect', true); $count = get_post_meta($post->ID, '_surl_count', true); if ( $column == 'url' ) { echo make_clickable( esc_url( $url ? $url : '' ) ); } elseif ( $column == 'permalink' ) { echo make_clickable( get_permalink() ); } elseif ( $column == 'clicks' ) { echo esc_html( $count ? $count : 0 ); } } function add_meta_box() { add_meta_box('surl', __('URL信息', 'surl'), array( &$this, 'meta_box' ), 'surl', 'normal', 'high'); } function meta_box() { global $post; printf( '<input type="hidden" name="_surl_nonce" value="%s" />', wp_create_nonce( plugin_basename(__FILE__) ) ); printf( '<p><label for="%s">%s</label></p>', '_surl_redirect', __('URL原链接地址:', 'surl') ); printf( '<p><input style="%s" type="text" name="%s" id="%s" value="%s" /></p>', 'width: 99%;', '_surl_redirect', '_surl_redirect', esc_attr( get_post_meta( $post->ID, '_surl_redirect', true ) ) ); $count = isset( $post->ID ) ? get_post_meta($post->ID, '_surl_count', true) : 0; printf( '<p>此链接已经被点击 <b>%d</b> 次.', esc_attr( $count ) ); } function meta_box_save( $post_id, $post ) { $key = '_surl_redirect'; // verify the nonce if ( !isset($_POST['_surl_nonce']) || !wp_verify_nonce( $_POST['_surl_nonce'], plugin_basename(__FILE__) ) ) return; // don't try to save the data under autosave, ajax, or future post. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; if ( defined('DOING_AJAX') && DOING_AJAX ) return; if ( defined('DOING_CRON') && DOING_CRON ) return; // is the user allowed to edit the URL? if ( ! current_user_can( 'edit_posts' ) || $post->post_type != 'surl' ) return; $value = isset( $_POST[$key] ) ? $_POST[$key] : ''; if ( $value ) { // save/update update_post_meta($post->ID, $key, $value); } else { // delete if blank delete_post_meta($post->ID, $key); } } function count_and_redirect() { if ( !is_singular('surl') ) return; global $wp_query; // Update the count $count = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, '_surl_count', true) : 0; update_post_meta( $wp_query->post->ID, '_surl_count', $count + 1 ); // Handle the redirect $redirect = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, '_surl_redirect', true) : ''; if ( !empty( $redirect ) ) { wp_redirect( esc_url_raw( $redirect ), 301); exit; } else { wp_redirect( home_url(), 302 ); exit; } } } $SimpleURLs = new SimpleURLs; // 代码实现短网址功能
然后打开后台,是不是发现多了一个sinmple Urls的栏目