• Ok, been using this forum for a few months now. Just getting my hands dirty with PHP. I prefer to dig through Google (as to not bother anyone when I’m stuck), but, I’ve been working at this for 3 days and can’t get it.

    I’m trying to create a plugin that will allow me to upload an image (ad) and then insert it into any post I choose.

    I’m using preg_replace() with <!–ad–> as my insert into the individual posts. That works fine.

    And, I’ve managed to get pics uploaded with the wordpress uploader.

    PROBLEM: $_FILES returns NULL when I finish the upload so I don’t know how to access the file name to use for the preg_replace.

    From research I’m guessing I need to access the array in the saved WordPress options somewhere, but I have no idea how to do that.

    Here’s the plugin code I’ve got…

    ps. my ini file is fine as other uploads have worked and returned $_FILES array.

    ////////////////////////////////////////////
    //Admin interface
    ////////////////////////////////////////////
    
    // add the admin page and such
    add_action('admin_init', 'ud_admin_init');
    function ud_admin_init() {
        register_setting( 'ud_options', 'ud_options', 'ud_options_validate' );
        add_settings_section('ud_main', 'Main Section', 'ud_section_text', 'ud');
        add_settings_field('ud_filename', 'File:', 'ud_setting_filename', 'ud', 'ud_main');
    }
    
    // add the admin options page
    add_action('admin_menu', 'ud_admin_add_page');
    function ud_admin_add_page() {
        $mypage = add_options_page('Upload Demo', 'Upload Demo', 'manage_options', 'ud', 'ud_options_page');
    }
    
    // display the admin options page
    function ud_options_page() {
    ?>
        <div class="wrap">
        <h2>Upload Demo</h2>
        <p>You can upload a file. It'll go in the uploads directory.</p>
        <form method="post" enctype="multipart/form-data" action="options.php">
        <?php settings_fields('ud_options'); ?>
        <?php do_settings_sections('ud'); ?>
        <p class="submit">
        <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
        </p>
        </form>
    
        </div>
    
    <?php
    
    }
    
    function ud_section_text() {
        $options = get_option('ud_options');
        echo '<p>Upload your file here:</p>';
        if ($file = $options['file']) {
            // var_dump($file);
            echo "<img src='{$file['url']}' />";
    
        }
    }
    
    function ud_setting_filename() {
        echo '<input type="file" name="ud_filename" size="40" />';
    
    }
    
    function ud_options_validate($input) {
        $newinput = array();
        if ($_FILES['ud_filename']) {
            $overrides = array('test_form' => false);
            $file = wp_handle_upload($_FILES['ud_filename'], $overrides);
            $newinput['file'] = $file;
        }
        return $newinput; //<-- How do I get to what's in here!?!?!?
    }
    
    //////////////////////////////////////////////
    // Ad Insert Function
    //////////////////////////////////////////////
    
    add_filter('the_content', 'ad_insert_tag', 0);
    
    function ad_insert_tag($content) {
    	if (!is_single()) {
    		return $content;
    	}
    
    	$ad_name = //nothing here yet;
    	$adContent = "?><img src=\"https://localhost/site1/wp-content/plugins/in-post-ad/images/$ad_name\"><?php";
    	$adInsert = '<!--ad-->';
    
    	if (preg_match($adInsert, $content)) {
    		$content = preg_replace($adInsert, '$1' . $adContent . '$2', $content);
    
    	} 
    
    	ob_start();
    	eval("?>$content<?");
    	$result = ob_get_contents();
    	ob_end_clean();
    
    	return $result;
    }

    I’m a newbie so I apologize if this is an idiot question. Any assistance would be much appreciated.

  • The topic ‘Will Give Karma for Help – wp_handle_upload and options.php’ is closed to new replies.