doubianyan9749 2013-10-14 06:00
浏览 109

在wordpress中的常规设置TAB中添加自定义字段

I want to add a custom field in General setting TAB in wordpress. This are the present fields that wordpress has it by default.

  1. Site Title
  2. Tag line
  3. Wordpress Address URL ...etc

I want to add a custom field like, I want to have an image upload field.

For that I had to edit options-general.php , options.php, general-template.php, I had to insert an entry in wp-options table in my database

Now when I tested it with a simple input type as text it worked well. But when I set the input type as file for my logo upload it doesn't work this is my code below. options-general.php

<tr valign="top">
<th scope="row"><label for="logo"><?php _e('Logo') ?></label></th>
<td><input name="file" type="file"/></td>
</tr>

As you can see I have placed my image field right under my blog description field, and the action of this form takes me to options.php. this is my option.php

if ( is_multisite() && !is_super_admin() && 'update' != $action )
    wp_die(__('Cheatin&#8217; uh?'));
/* image upload function goes here */

if($_POST['submit']){

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("images/logo/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"images/logo/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
    /* image upload function ends here */

Above I have added the simple image upload script, some how it doesn't work, nor the file is uploaded to the directory.

Does core PHP code works in WP environment Or I'm missing on something, Kindly suggest.

  • 写回答

2条回答 默认 最新

  • douchongbang6011 2013-10-14 06:44
    关注

    Stop the machines!

    You're _doing_it_wrong(), although this function does not detect what you're doing :)
    We don't touch core file, we do plugins. Please, restore your WP to a fresh state.


    Answering to the Question title

    Adding custom field in General setting TAB in wordpress

    Follows an example from WordPress Answers. The key function is add_settings_field, right now it adds a full WP Editor, but it can be adjusted to show any kind of field. A nice article that explains the Settings API.

    <?php
    /**
     * Plugin Name: My Custom General Setting
     */
    
    add_action( 'admin_init', 'wpse_57647_register_settings' );
    
    /* 
     * Register settings 
     */
    function wpse_57647_register_settings() 
    {
        register_setting( 
            'general', 
            'html_guidelines_message',
            'esc_html' // <--- Customize this if there are multiple fields
        );
        add_settings_section( 
            'site-guide', 
            'Publishing Guidelines', 
            '__return_false', 
            'general' 
        );
        add_settings_field( 
            'html_guidelines_message', 
            'Enter custom message', 
            'wpse_57647_print_text_editor', 
            'general', 
            'site-guide' 
        );
    }    
    
    /* 
     * Print settings field content 
     */
    function wpse_57647_print_text_editor() 
    {
        $the_guides = html_entity_decode( get_option( 'html_guidelines_message' ) );
        echo wp_editor( 
            $the_guides, 
            'sitepublishingguidelines', 
            array( 'textarea_name' => 'html_guidelines_message' ) 
        );
    }
    

    resulting settings

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大