• Resolved enriquerene

    (@enriquerene)


    Hello,

    I’m developing my first plugin. It creates directories and save the path, level (if it is a subdirectory of other created directory) and name in a custom table, created at the activation.

    The plugin has an option page where users can manager the files in each directory or create more directories and upload more files. In this option page I had to create a form with action atribute as "<?php echo EASYDM_PLUGIN_URL; ?>generate-dir.php".

    The problem is when I try to use the constants like WP_CONTENT_URL, ABSPATH wordpress tells me they are undefined constants… just in this page of action atribute. Why?

    I tried //add_action( 'admin_init', 'easydm_register_settings' ); with register_setting( 'pluginPage', 'easydm_settings' ); and so on, but when I click in submit button the page redirects me to the ‘file.php’ in action but as wp-admin/file.php. So, neither the first nor the second way are working for me. How can I do that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    When you submit a form to a non-WP PHP page, all of the WP constants and functions are not available to your PHP page. Your code on that page does not even know that WP exists, you can’t do anything related to WP on it.

    Normally, options forms use the Settings API and submit to option.php. If your options page is not saving settings to the options table, this is of no help at all. To do other things besides saving options, you have 3 choices on where to submit your form if you want to have access to WP functions and constants. You can use AJAX, you can submit to a WP page based on a custom template containing your PHP code, or you can submit to wp-admin/admin-post.php which makes a call to do_action() based on a form field named ‘action’ – your code is part of a callback to that action.

    Thread Starter enriquerene

    (@enriquerene)

    I see. This made me work a lot, and I found an easy way to do my form in option page:

    https://wpshout.com/make-wordpress-admin-options-page-without-using-settings-api/

    Just <form method="post"> without action atribute. So the the form send the information to the same page, and I treat these easier.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Losing constants and variables wordpress core’ is closed to new replies.