在wordpress的常规设置选项卡中添加自定义字段

问题描述 投票:0回答:2

我想在 WordPress 的常规设置选项卡中添加自定义字段。 这是 WordPress 默认情况下存在的字段。

  1. 网站标题
  2. 标语
  3. Wordpress 地址 URL ...等等

我想添加一个自定义字段,例如,我想要一个图像上传字段。

为此我必须编辑

options-general.php
options.php
,
general-template.php
, 我必须在数据库中的
wp-options
表中插入一个条目

现在,当我用一个简单的

input type as text
测试它时,效果很好。 但是,当我为徽标上传设置
input type as file
时,它不起作用,这是我的代码如下。
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>

如您所见,我已将图像字段放在博客描述字段的正下方,此表单的操作将我带到

options.php
。 这是我的
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 */

上面我添加了简单的图像上传脚本,有些它不起作用,文件也没有上传到目录。

核心 PHP 代码在 WP 环境中工作吗?或者我遗漏了一些东西,请提出建议。

php mysql wordpress image-uploading
2个回答
6
投票

停止机器!

你是

_doing_it_wrong()
,尽管此功能无法检测到你在做什么:)
我们不触及核心文件,我们做插件。请将您的 WP 恢复到新状态。


回答问题标题

在wordpress的常规设置选项卡中添加自定义字段

遵循 WordPress Answers 中的示例。关键功能是

add_settings_field
,现在它添加了一个完整的WP编辑器,但可以调整它以显示任何类型的字段。一篇好文章解释了设置 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' ) 
    );
}


1
投票

如果您需要向网站添加选项,但实际上并不需要在自己的页面上。您也许可以将其添加到现有设置页面之一。以下是如何向“常规设置”页面添加选项。

在本例中,我添加了“最喜欢的颜色”字段,可能不是最好的示例,所以请继续更改它。 🙂

  class new_general_setting {
        function init( ) {
            add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
        }
        function register_fields() {
            register_setting( 'general', 'favorite_color', 'esc_attr' );
            add_settings_field('fav_color', '<label for="favorite_color">'.__('Favorite Color?' , 'favorite_color' ).'</label>' , array(&$this, 'fields_html') , 'general' );
        }
        function fields_html() {
            $value = get_option( 'favorite_color', '' );
            echo '<input type="text" id="favorite_color" name="favorite_color" value="' . $value . '" />';
        }
    }

(new new_general_setting())->init();

register_setting() 函数的第三个参数是用于验证输入数据的函数名称,因此请根据需要进行自定义。

© www.soinside.com 2019 - 2024. All rights reserved.