0

I'm creating a custom WP plugin. Inside my main plugin file (functions.php) I've includes the following line of code so a second file (module_2.php) with additional code will be loaded:

define( 'MY_MODULES_PATH', MY_PLUGIN_PATH . 'modules/' );

require_once MY_MODULES_PATH . 'module_2.php';

In module_2.php I've added the following code:

// CODE 1
function my_stylesheet() {
    wp_enqueue_style( 'custom-login', plugin_dir_url( __FILE__ ) . '/assets/css/login.css' );
}
add_action( 'login_enqueue_scripts', 'my_stylesheet' );

//CODE 2
add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
function remove_wp_logo( $wp_admin_bar ) {
     $wp_admin_bar->remove_node( 'wp-logo' );

I've also created an custom admin page with a checkbox option and it's working perfectly with the WordPress API. The settings are saved successfully in the database!

However I would like to have the possible to enable and disable the module_2.php file completely from the WP dashboard and currently I'm quite stuck...

I've tried searching for a solution, but I'm not getting any results. Would anybody direct my in the right direction preferable by a tutorial or the WP codex?

Thanks in advance!

7
  • Can you elaborate on this please: However I would like to enable/disable the require_once option within my custom admin page and currently I'm quite stuck... what do you mean by enabling it? and what do you mean by stuck?
    – Paul G.
    Jul 5 '21 at 11:59
  • Of course! In my plugin I'm using two functions.php files. A main file (functions.php) and a second (and third enz) in modules/module_1.php that's being called from functions.php. I would like to enable/disable the functions in the second file by creating an admin page and making use of the WP API. The settings page itself appears to be working perfectly, the settings are correctly saved in de database in the wp_options table. Jul 5 '21 at 12:03
  • I'm sorry, it's no clearer to me what you're looking to do. I don't know what the sentence means: I would like to enable/disable the functions in the second file by creating an admin page and making use of the WP API. Rather than say what way you want to do something, explain, in non-technology terms what you're trying to achieve. "disabling a function" doesn't have a meaning in PHP in this context. It's either there and available to be executed, or it isn't there. And if you can, edit the original question to include your clarifications...
    – Paul G.
    Jul 5 '21 at 12:06
  • I agree with Paul, I've read your question and the comment several times and don't understand what you're trying to achieve, that sentence does not make sense. require_once is something you either do, or you don't do, it's a part of PHP itself, not WordPress, there is no enabling/disabling. This sounds like an X Y problem. Try to describe what you originally wanted to do, not the solution you've devised but don't know how to implement. Are you trying to do something only if the checkbox is checked? If so, ask about that and update your question using the edit link under the tags
    – Tom J Nowell
    Jul 5 '21 at 12:09
  • 1
    @SallyCJ Thank you! That's exactly what I've wanted! I'm not using the stylesheet function directly in functions because there are more line's in module_2.php. I wanted to enable the module completely and the possibility to enable the various functions in module_2.php Jul 6 '21 at 11:18
0

I achieved my goal by doing an option check like so, which conditionally loads the various functions in module_2.php, in my main plugin file:

// If the checkbox is checked, load module_2.php.
if ( get_option( 'my_checkbox_option' ) ) {
    require_once MY_MODULES_PATH . 'module_2.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.