0

I am making a custom plugin with various functions that do different things. I have created a settings page using the Settings API to save data to the options db. That works fine. Now I'm trying to "turn on/off" functions from the settings page, with the below example currently being set to "true." If the form value on the settings page equals true, then the function should be called, otherwise it should not be called.

What would be the best approach for this? I'm thinking adding each function as a different php file, and then checking if the option is true and calling the file if necessary. I have added the function to a file called add_id_user_admin.php, and currently attempting to call it like so:

add_action( 'init', 'switch_functions' ); // Tested and works
function switch_functions() { // Tested and works
    if (get_option('new_option_name') == 'true') { // Tested and works
        include '/wp-content/plugins/eri-webtools-plugin/includes/theme/functions/add_id_user_admin.php'; // Not working
    }
}

Perhaps the file path is the problem, or I should be using a different method of calling the php file? Can't seem to get it to fire the function even though I know the get_option() equals true at the moment.

0

Nevermind, I figured it out. Here's what I did in case anybody else wants to know. In my main php file in the plugin directory, I just added the following:

if (get_option('new_option_name') == 'true') {
    require_once plugin_dir_path(__FILE__) . 'includes/theme/functions/add_id_user_admin.php';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.