/wp-content/plugins/zwinit/zwinit.php

https://github.com/zhanghedong/zw · PHP · 188 lines · 99 code · 33 blank · 56 comment · 17 complexity · 08a0e05642e3489ab83653879e4e8a48 MD5 · raw file

  1. <?php
  2. /*
  3. Plugin Name: zwinit
  4. Plugin URI: http://z-n-w.com
  5. Description: product field init
  6. Version: 1.0
  7. Author: 1625.me
  8. Author URI:
  9. */
  10. //////////////添加自定义字段
  11. $new_meta_boxes = array(
  12. //"description" => array(
  13. //"name" => "description",
  14. //"std" => "",
  15. //"title" => "Description:"),
  16. "item" => array(
  17. "name" => "unit",
  18. "std" => "",
  19. "title" => "unit:"),
  20. "unit" => array(
  21. "name" => "unit",
  22. "std" => "",
  23. "title" => "Unit:"),
  24. "item" => array(
  25. "name" => "item",
  26. "std" => "",
  27. "title" => "Item:"),
  28. "size" => array(
  29. "name" => "size",
  30. "std" => "",
  31. "type" => "textarea",
  32. "title" => "Size:"),
  33. "pack" => array(
  34. "name" => "pack",
  35. "std" => "",
  36. "title" => "PacK:"),
  37. "cbm" => array(
  38. "name" => "cbm",
  39. "std" => "",
  40. "title" => "CBM:"),
  41. "qty" => array(
  42. "name" => "qty",
  43. "std" => "",
  44. "title" => "qty:"),
  45. "manufacturers" => array(
  46. "name" => "manufacturers",
  47. "std" => "",
  48. "type" => "selectM",
  49. "title" => "manufacturers:")
  50. );
  51. function new_meta_boxes() {
  52. global $post, $new_meta_boxes;
  53. foreach($new_meta_boxes as $meta_box) {
  54. $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
  55. if($meta_box_value == "")
  56. $meta_box_value = $meta_box['std'];
  57. echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
  58. // 自定义字段标题
  59. echo'<h4>'.$meta_box['title'].'</h4>';
  60. if( $meta_box['type'] == 'selectM' ){
  61. // 自定义字段输入框
  62. echo '<select class="regular-select" name="'.$meta_box['name'].'_value" ><option value="富鑫">富鑫</option><option value="阳松">阳松</option></select><br />';
  63. }else if( $meta_box['type'] == 'textarea' ){
  64. // 自定义字段输入框
  65. echo '<textarea class="regular-textarea" name="'.$meta_box['name'].'_value" >' . $meta_box_value . '</textarea><br />';
  66. }else{
  67. // 自定义字段输入框
  68. echo '<input class="regular-text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" /><br />';
  69. }
  70. }
  71. }
  72. function create_meta_box() {
  73. global $theme_name;
  74. if ( function_exists('add_meta_box') ) {
  75. add_meta_box( 'new-meta-boxes', 'Detail', 'new_meta_boxes', 'post', 'normal', 'high' );
  76. }
  77. }
  78. function save_postdata( $post_id ) {
  79. global $post, $new_meta_boxes;
  80. foreach($new_meta_boxes as $meta_box) {
  81. if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  82. return $post_id;
  83. }
  84. if ( 'page' == $_POST['post_type'] ) {
  85. if ( !current_user_can( 'edit_page', $post_id ))
  86. return $post_id;
  87. }
  88. else {
  89. if ( !current_user_can( 'edit_post', $post_id ))
  90. return $post_id;
  91. }
  92. $data = $_POST[$meta_box['name'].'_value'];
  93. if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
  94. add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
  95. elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
  96. update_post_meta($post_id, $meta_box['name'].'_value', $data);
  97. elseif($data == "")
  98. delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
  99. }
  100. }
  101. add_action('admin_menu', 'create_meta_box');
  102. add_action('save_post', 'save_postdata');
  103. function remove_post_custom_fields() {
  104. remove_meta_box( 'postcustom' , 'post' , 'normal' );
  105. }
  106. add_action( 'admin_menu' , 'remove_post_custom_fields' );
  107. ///评论邮件通知
  108. function comment_mail($comment_id,$comment){
  109. /////保存当前评论ID
  110. add_option('comment_id_current',$comment_id);
  111. /*暂时关闭邮件通知功能
  112. $email_to = stripslashes(get_option('admin_email'));
  113. $tel= stripslashes($_POST['tel']);
  114. $email= stripslashes($_POST['email']);
  115. $author= stripslashes($_POST['author']);
  116. $subject= stripslashes($_POST['subject']);
  117. $comment= stripslashes($_POST['comment']);
  118. $productname= stripslashes($_POST['productname']);
  119. $content .="Dear:webmaster \n\n ";
  120. $content .= $comment ;
  121. $content .=" \n\n author:";
  122. $content .= $author ;
  123. $content .="\n\n email:";
  124. $content .= $email ;
  125. $content .=" \n\n tel:\n\n";
  126. $content .= $tel;
  127. $content .=" \n\n Product list: ";
  128. if($productname != ''){
  129. $content .= $productname;
  130. }
  131. $content .=" \n\n";
  132. wp_mail($email_to,$subject,$content);
  133. //global $phpmailer;
  134. //if ( $phpmailer->ErrorInfo != "" ) {
  135. //echo 'abc';
  136. //} else {
  137. //echo 'cde';
  138. //}
  139. */
  140. }
  141. /*同时邮件通知*/
  142. add_action ( 'wp_insert_comment', comment_mail, 10, 2 );
  143. function zwinit_js_css()
  144. {
  145. echo '<link type="text/css" rel="stylesheet" href="'
  146. . get_bloginfo('wpurl') . '/wp-content/plugins/zwinit/zwinit.css">';
  147. }
  148. add_action( 'admin_print_styles', 'zwinit_js_css', 1 );