奥多码,点击查看详情 APP分发吧,点击查看详情

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

       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03
       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03

打开当前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&gt;%d</b&gt; 次.’, 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’) &&amp; 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-&gt;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. // 代码实现短网址功能
本文由【好易之】整理自网络!
原创文章,作者:【好易之】如转载请注明出处:https://www.zhengjiaxi.com/zxwd/jswd/69762.html
如有侵权,请邮件联系 aoduoye@qq.com 删除。
本站发布的文章及附件仅限用于学习和研究目的;不得将上述内容用于商业或非法用途,否则后果请用户自负。
本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
好易之的头像好易之站长
上一篇 2020-05-27 00:34
下一篇 2020-05-27 00:34

相关推荐

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询:点击这里给我发消息

 

工作时间:周一至周五,9:30-18:30,节假日休息

关注公众号
请查看头部文章来源地址!本站所有内容均为互联网收集整理和网友上传。仅限于学习研究,切勿用于商业用途。否则由此引发的法律纠纷及连带责任本站概不承担。
阿里云