3

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.

6
  • That doesn't look like valid PHP to me. You have }option.php in a couple of places, and this }images/logo/" . $_FILES["file"]["name"])) looks weird too. The SO syntax highlighter is showing code as strings and vice versa - you're missing a " somewhere. Commented Oct 14, 2013 at 6:11
  • @Hobo I have edited my code above kindly check. Commented Oct 14, 2013 at 6:12
  • What output are you getting? Anything in your PHP logs? Commented Oct 14, 2013 at 6:18
  • @Hobo I'm not getting anything. When I click on save after I set the image in my upload field, it says "Settings Saved.". But doesn't upload the image nor the database field that I have set in wp-options table Commented Oct 14, 2013 at 6:21
  • Given you have an echo in each branch of your code, you should see some output if the code is running. Which seems to imply it's not running. $_POST['submit'] definitely set? You've uploaded the right file to the right place? And there's nothing in the PHP logs? Core PHP does work in the WP environment, to answer that part of your question. Is $_FILES an array? Maybe you should loop over it? Commented Oct 14, 2013 at 6:26

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for the good point, and good example. But it doesn't explain why the original code isn't working
@Hobo Well, then the OP has to justify why he's hacking WP core. Otherwise it's an XY Problem.
@brasofilo I really like the way you have set the UPload / Insert in General setting TAB. But this is something extra as there is a editor and gets confusing on the part, suppose if add image and enter some text under it, will that display along with the Image ? I wanted something simple as a image field. Thanks yo I really appreciate it.
@Mckenzi, answer updated. It is only a demonstration of what can be done, you'll have to dig on from here :)
@brasofilo I have updated my Code in the question kindly check.
1

If you need to add an option to a site, but it doesn’t really need to be on its own page. You can probably add it to one of the existing settings pages. Here’s how to add an option to the General Settings page.

In this case, I’m adding a ‘favorite color’ field, probably not the best example, so go ahead and change that. 🙂

  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();

The third argument of the register_setting() function is the name of the function that should be used to validate the input data, so customize that as needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.