博华网络科技,点击查看详情

wordpress教程:纯代码实现外链转内链go跳转短链接功能

打开当前wordpress主题的functions.php文件,添加以下代码:

  1. // 代码实现短网址功能
  2. class SimpleURLs {
  3. // Constructor
  4. function __construct() {
  5. //register_activation_hook( __FILE__, ‘flush_rewrite_rules’ );
  6. add_action( ‘init’, array( &$this, ‘register_post_type’ ) );
  7. add_action( ‘manage_posts_custom_column’, array( &$this, ‘columns_data’ ) );
  8. add_filter( ‘manage_edit-surl_columns’, array( &$this, ‘columns_filter’ ) );
  9. add_action( ‘admin_menu’, array( &$this, ‘add_meta_box’ ) );
  10. add_action( ‘save_post’, array( &$this, ‘meta_box_save’ ), 1, 2 );
  11. add_action( ‘template_redirect’, array( &$this, ‘count_and_redirect’ ) );
  12. }
  13. // PHP4 Constructor
  14. function SimpleURLs() {
  15. $this->__construct();
  16. }
  17. function register_post_type() {
  18. register_post_type( ‘surl’,
  19. array(
  20. ‘labels’ => array(
  21. ‘name’ => __( ‘Simple URLs’ ),
  22. ‘singular_name’ => __( ‘URL’ ),
  23. ‘add_new’ => __( ‘Add URL’ ),
  24. ‘add_new_item’ => __( ‘新建URL’ ),
  25. ‘edit’ => __( ‘Edit’ ),
  26. ‘edit_item’ => __( ‘Edit URL’ ),
  27. ‘new_item’ => __( ‘New URL’ ),
  28. ‘view’ => __( ‘View URL’ ),
  29. ‘view_item’ => __( ‘View URL’ ),
  30. ‘search_items’ => __( ‘Search URL’ ),
  31. ‘not_found’ => __( ‘No URLs found’ ),
  32. ‘not_found_in_trash’ => __( )
  33. ),
  34. ‘public’ => true,
  35. ‘query_var’ => true,
  36. ‘menu_position’ => 20,
  37. ‘supports’ => array( ‘title’ ),
  38. ‘rewrite’ => array( ‘slug’ => ‘go’, ‘with_front’ => false )
  39. )
  40. );
  41. }
  42. function columns_filter( $columns ) {
  43. $columns = array(
  44. ‘cb’ => ‘<input type=”checkbox” />’,
  45. ‘title’ => __(‘标题’),
  46. ‘url’ => __(‘原地址’),
  47. ‘permalink’ => __(‘短地址’),
  48. ‘clicks’ => __(‘点击量’)
  49. );
  50. return $columns;
  51. }
  52. function columns_data( $column ) {
  53. global $post;
  54. $url = get_post_meta($post->ID, ‘_surl_redirect’, true);
  55. $count = get_post_meta($post->ID, ‘_surl_count’, true);
  56. if ( $column == ‘url’ ) {
  57. echo make_clickable( esc_url( $url ? $url : ) );
  58. }
  59. elseif ( $column == ‘permalink’ ) {
  60. echo make_clickable( get_permalink() );
  61. }
  62. elseif ( $column == ‘clicks’ ) {
  63. echo esc_html( $count ? $count : 0 );
  64. }
  65. }
  66. function add_meta_box() {
  67. add_meta_box(‘surl’, __(‘URL信息’, ‘surl’), array( &$this, ‘meta_box’ ), ‘surl’, ‘normal’, ‘high’);
  68. }
  69. function meta_box() {
  70. global $post;
  71. printf( ‘<input type=”hidden” name=”_surl_nonce” value=”%s” />’, wp_create_nonce( plugin_basename(__FILE__) ) );
  72. printf( ‘<p><label for=”%s”>%s</label></p>’, ‘_surl_redirect’, __(‘URL原链接地址:’, ‘surl’) );
  73. 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 ) ) );
  74. $count = isset( $post->ID ) ? get_post_meta($post->ID, ‘_surl_count’, true) : 0;
  75. printf( ‘<p>此链接已经被点击 <b>%d</b> 次.’, esc_attr( $count ) );
  76. }
  77. function meta_box_save( $post_id, $post ) {
  78. $key = ‘_surl_redirect’;
  79. // verify the nonce
  80. if ( !isset($_POST[‘_surl_nonce’]) || !wp_verify_nonce( $_POST[‘_surl_nonce’], plugin_basename(__FILE__) ) )
  81. return;
  82. // don’t try to save the data under autosave, ajax, or future post.
  83. if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) return;
  84. if ( defined(‘DOING_AJAX’) && DOING_AJAX ) return;
  85. if ( defined(‘DOING_CRON’) && DOING_CRON ) return;
  86. // is the user allowed to edit the URL?
  87. if ( ! current_user_can( ‘edit_posts’ ) || $post->post_type != ‘surl’ )
  88. return;
  89. $value = isset( $_POST[$key] ) ? $_POST[$key] : ;
  90. if ( $value ) {
  91. // save/update
  92. update_post_meta($post->ID, $key, $value);
  93. } else {
  94. // delete if blank
  95. delete_post_meta($post->ID, $key);
  96. }
  97. }
  98. function count_and_redirect() {
  99. if ( !is_singular(‘surl’) )
  100. return;
  101. global $wp_query;
  102. // Update the count
  103. $count = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, ‘_surl_count’, true) : 0;
  104. update_post_meta( $wp_query->post->ID, ‘_surl_count’, $count + 1 );
  105. // Handle the redirect
  106. $redirect = isset( $wp_query->post->ID ) ? get_post_meta($wp_query->post->ID, ‘_surl_redirect’, true) : ;
  107. if ( !emptyempty( $redirect ) ) {
  108. wp_redirect( esc_url_raw( $redirect ), 301);
  109. exit;
  110. }
  111. else {
  112. wp_redirect( home_url(), 302 );
  113. exit;
  114. }
  115. }
  116. }
  117. $SimpleURLs = new SimpleURLs;
  118. // 代码实现短网址功能

人已赞赏
wordpress教程

WordPress教程:完美过滤垃圾评论

2020-5-26 12:40:17

wordpress教程

WordPress教程:伪静态规则大全

2020-5-26 12:40:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索