Jump to content

Callback functions not working with include file


Tsalagi

Recommended Posts

I have a file I'm trying to split up to separate some HTML from PHP code. Everything works fine when all of the code is together but when I split it into two files the two call back functions in the html form will not work. No errors and when I view the page source my file is included with the form. Any help would be appreciated.

Here is the file that has the include statement.

add_action('admin_menu', 'sampleoptions_add_page_fn');
// Add sub page to the Settings Menu
function sampleoptions_add_page_fn() {
    add_options_page('Options Example Page', 'Options Example', 'administrator', __FILE__, 'options_page_fn');
}
function options_page_fn() {
?>
<?php include('options_form.php'); ?>
<?php
}

Here is the options_form.php file code.

<div class="wrap">
        <div class="icon32" id="icon-options-general"><br></div>
        <h2>My Example Options Page</h2>
        Some optional text here explaining the overall purpose of the options and what they relate to etc.
        <form action="options.php" method="post">
        <?php settings_fields('plugin_options'); ?>
        <?php do_settings_sections(__FILE__); ?>
        <p class="submit">
            <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
        </p>
        </form>
    </div>

I'm guessing that it may have to do with this line of code, due to the page parameter but I don't know that much about that area.

<?php do_settings_sections(__FILE__); ?>

Thanks in advance

you are correct that __FILE__ seems to be what's causing the problem:

 

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

 

you need another way to pass the file's absolute path to the function containing the include.

 

like:

1)

<?php
function options_page_fn($file_path){
include 'options_form.php';
}
?>

 

2) In your included file, change __FILE__ to $file_path.

 

3) call your function like this:

echo options_page_fn(__FILE__);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.