Use a PHP file as action for a form in a WordPress plugin, what's the correct way?How to include PHP files in plugins the correct wayForm doesn't submit on second submit callcan't access some WordPress function from my pluginwp_mail is undefined“Error: Options Page Not Found” on Settings Page Submission for an OOP PluginCustom form handling in WP.. the correct way?How do I make a child theme I made POST through a 3rd party plugin?Adapt PHP form action for WordPress?How to call WordPress functions from a form processing scriptform action wordpress and phpwordpress plugin php file processing form

Find the identical rows in a matrix

Creating a chemical industry from a medieval tech level without petroleum

Von Neumann Extractor - Which bit is retained?

How long after the last departure shall the airport stay open for an emergency return?

Negative Resistance

Drawing a german abacus as in the books of Adam Ries

Will I lose my paid in full property

Can someone publish a story that happened to you?

How to not starve gigantic beasts

How can I practically buy stocks?

My bank got bought out, am I now going to have to start filing tax returns in a different state?

How to pronounce 'c++' in Spanish

"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"

How to have a sharp product image?

Prove that the countable union of countable sets is also countable

How can I get rid of an unhelpful parallel branch when unpivoting a single row?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?

How much of a wave function must reside inside event horizon for it to be consumed by the black hole?

Work requires me to come in early to start computer but wont let me clock in to get paid for it

Why do real positive eigenvalues result in an unstable system? What about eigenvalues between 0 and 1? or 1?

A strange hotel

Apply a different color ramp to subset of categorized symbols in QGIS?

A faster way to compute the largest prime factor



Use a PHP file as action for a form in a WordPress plugin, what's the correct way?


How to include PHP files in plugins the correct wayForm doesn't submit on second submit callcan't access some WordPress function from my pluginwp_mail is undefined“Error: Options Page Not Found” on Settings Page Submission for an OOP PluginCustom form handling in WP.. the correct way?How do I make a child theme I made POST through a 3rd party plugin?Adapt PHP form action for WordPress?How to call WordPress functions from a form processing scriptform action wordpress and phpwordpress plugin php file processing form






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm developing my first "serious" WordPress plugin using Devin Vinson's plugin boiler plate generated with this generator. Now I need to use a PHP file not present in default boilerplate as action attribute value for a form. When the form is submitted and the PHP file executed I get many fatal errors of call to undefined function for every WordPress function that I call in that file...
I already read THIS and I obviously required the PHP file with require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ccwdpo-submit-question.php'; but I didn't solve my problem...



What's my mistake?










share|improve this question

















  • 2





    Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

    – nmr
    Apr 13 at 10:16

















1















I'm developing my first "serious" WordPress plugin using Devin Vinson's plugin boiler plate generated with this generator. Now I need to use a PHP file not present in default boilerplate as action attribute value for a form. When the form is submitted and the PHP file executed I get many fatal errors of call to undefined function for every WordPress function that I call in that file...
I already read THIS and I obviously required the PHP file with require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ccwdpo-submit-question.php'; but I didn't solve my problem...



What's my mistake?










share|improve this question

















  • 2





    Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

    – nmr
    Apr 13 at 10:16













1












1








1








I'm developing my first "serious" WordPress plugin using Devin Vinson's plugin boiler plate generated with this generator. Now I need to use a PHP file not present in default boilerplate as action attribute value for a form. When the form is submitted and the PHP file executed I get many fatal errors of call to undefined function for every WordPress function that I call in that file...
I already read THIS and I obviously required the PHP file with require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ccwdpo-submit-question.php'; but I didn't solve my problem...



What's my mistake?










share|improve this question














I'm developing my first "serious" WordPress plugin using Devin Vinson's plugin boiler plate generated with this generator. Now I need to use a PHP file not present in default boilerplate as action attribute value for a form. When the form is submitted and the PHP file executed I get many fatal errors of call to undefined function for every WordPress function that I call in that file...
I already read THIS and I obviously required the PHP file with require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ccwdpo-submit-question.php'; but I didn't solve my problem...



What's my mistake?







plugin-development forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 13 at 9:37









icolumbroicolumbro

62




62







  • 2





    Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

    – nmr
    Apr 13 at 10:16












  • 2





    Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

    – nmr
    Apr 13 at 10:16







2




2





Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

– nmr
Apr 13 at 10:16





Move the logic from the PHP file to the function and invoke it after sending the form. admin_post_action hook is what you need. Here you will find usage examples. If something is unclear, ask.

– nmr
Apr 13 at 10:16










1 Answer
1






active

oldest

votes


















2














To be honest, you should never use a PHP file as action attribute for a form in WordPress. WordPress already has API for this and you should use this instead. Why? Because it's always better if your app/site has only one entry point (or as few as possible).



And it's always a bad idea to direct any PHP requests directly to wp-content directory - such requests are very often blocked for security reasons.



So how to do this properly?



Use admin-post instead.



So in your form change this:



<form action="<SOME FILE>" ...


to this:



<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" ...
<input type="hidden" name="action" value="myform" />


And later in your plugin, you have to register your actions callbacks:



add_action( 'admin_post_myform', 'prefix_admin_myform_callback' );
add_action( 'admin_post_nopriv_myform', 'prefix_admin_myform_callback' );


function prefix_admin_myform_callback()
status_header(200);
die("Server received '$_REQUEST['data']' from your browser.");
//request handlers should die() when they complete their task






share|improve this answer

























  • Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

    – icolumbro
    Apr 13 at 10:26











  • It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

    – Krzysiek Dróżdż
    Apr 13 at 10:28











  • Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

    – icolumbro
    Apr 13 at 10:58











  • @icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

    – Krzysiek Dróżdż
    Apr 13 at 11:00






  • 1





    I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

    – icolumbro
    Apr 13 at 11:10











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "110"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f334284%2fuse-a-php-file-as-action-for-a-form-in-a-wordpress-plugin-whats-the-correct-wa%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














To be honest, you should never use a PHP file as action attribute for a form in WordPress. WordPress already has API for this and you should use this instead. Why? Because it's always better if your app/site has only one entry point (or as few as possible).



And it's always a bad idea to direct any PHP requests directly to wp-content directory - such requests are very often blocked for security reasons.



So how to do this properly?



Use admin-post instead.



So in your form change this:



<form action="<SOME FILE>" ...


to this:



<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" ...
<input type="hidden" name="action" value="myform" />


And later in your plugin, you have to register your actions callbacks:



add_action( 'admin_post_myform', 'prefix_admin_myform_callback' );
add_action( 'admin_post_nopriv_myform', 'prefix_admin_myform_callback' );


function prefix_admin_myform_callback()
status_header(200);
die("Server received '$_REQUEST['data']' from your browser.");
//request handlers should die() when they complete their task






share|improve this answer

























  • Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

    – icolumbro
    Apr 13 at 10:26











  • It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

    – Krzysiek Dróżdż
    Apr 13 at 10:28











  • Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

    – icolumbro
    Apr 13 at 10:58











  • @icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

    – Krzysiek Dróżdż
    Apr 13 at 11:00






  • 1





    I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

    – icolumbro
    Apr 13 at 11:10















2














To be honest, you should never use a PHP file as action attribute for a form in WordPress. WordPress already has API for this and you should use this instead. Why? Because it's always better if your app/site has only one entry point (or as few as possible).



And it's always a bad idea to direct any PHP requests directly to wp-content directory - such requests are very often blocked for security reasons.



So how to do this properly?



Use admin-post instead.



So in your form change this:



<form action="<SOME FILE>" ...


to this:



<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" ...
<input type="hidden" name="action" value="myform" />


And later in your plugin, you have to register your actions callbacks:



add_action( 'admin_post_myform', 'prefix_admin_myform_callback' );
add_action( 'admin_post_nopriv_myform', 'prefix_admin_myform_callback' );


function prefix_admin_myform_callback()
status_header(200);
die("Server received '$_REQUEST['data']' from your browser.");
//request handlers should die() when they complete their task






share|improve this answer

























  • Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

    – icolumbro
    Apr 13 at 10:26











  • It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

    – Krzysiek Dróżdż
    Apr 13 at 10:28











  • Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

    – icolumbro
    Apr 13 at 10:58











  • @icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

    – Krzysiek Dróżdż
    Apr 13 at 11:00






  • 1





    I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

    – icolumbro
    Apr 13 at 11:10













2












2








2







To be honest, you should never use a PHP file as action attribute for a form in WordPress. WordPress already has API for this and you should use this instead. Why? Because it's always better if your app/site has only one entry point (or as few as possible).



And it's always a bad idea to direct any PHP requests directly to wp-content directory - such requests are very often blocked for security reasons.



So how to do this properly?



Use admin-post instead.



So in your form change this:



<form action="<SOME FILE>" ...


to this:



<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" ...
<input type="hidden" name="action" value="myform" />


And later in your plugin, you have to register your actions callbacks:



add_action( 'admin_post_myform', 'prefix_admin_myform_callback' );
add_action( 'admin_post_nopriv_myform', 'prefix_admin_myform_callback' );


function prefix_admin_myform_callback()
status_header(200);
die("Server received '$_REQUEST['data']' from your browser.");
//request handlers should die() when they complete their task






share|improve this answer















To be honest, you should never use a PHP file as action attribute for a form in WordPress. WordPress already has API for this and you should use this instead. Why? Because it's always better if your app/site has only one entry point (or as few as possible).



And it's always a bad idea to direct any PHP requests directly to wp-content directory - such requests are very often blocked for security reasons.



So how to do this properly?



Use admin-post instead.



So in your form change this:



<form action="<SOME FILE>" ...


to this:



<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" ...
<input type="hidden" name="action" value="myform" />


And later in your plugin, you have to register your actions callbacks:



add_action( 'admin_post_myform', 'prefix_admin_myform_callback' );
add_action( 'admin_post_nopriv_myform', 'prefix_admin_myform_callback' );


function prefix_admin_myform_callback()
status_header(200);
die("Server received '$_REQUEST['data']' from your browser.");
//request handlers should die() when they complete their task







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 13 at 10:59

























answered Apr 13 at 10:15









Krzysiek DróżdżKrzysiek Dróżdż

18.9k73350




18.9k73350












  • Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

    – icolumbro
    Apr 13 at 10:26











  • It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

    – Krzysiek Dróżdż
    Apr 13 at 10:28











  • Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

    – icolumbro
    Apr 13 at 10:58











  • @icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

    – Krzysiek Dróżdż
    Apr 13 at 11:00






  • 1





    I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

    – icolumbro
    Apr 13 at 11:10

















  • Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

    – icolumbro
    Apr 13 at 10:26











  • It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

    – Krzysiek Dróżdż
    Apr 13 at 10:28











  • Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

    – icolumbro
    Apr 13 at 10:58











  • @icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

    – Krzysiek Dróżdż
    Apr 13 at 11:00






  • 1





    I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

    – icolumbro
    Apr 13 at 11:10
















Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

– icolumbro
Apr 13 at 10:26





Thank you, I'll try soon your solution but I have a doubt: what means "action"="myform" in hidden input field?

– icolumbro
Apr 13 at 10:26













It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

– Krzysiek Dróżdż
Apr 13 at 10:28





It tells WP, which action should it run to process this request. The "myform" part is then used to register your callbacks - admin_post_action

– Krzysiek Dróżdż
Apr 13 at 10:28













Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

– icolumbro
Apr 13 at 10:58





Yeah, I understand but is it correct to use "action" with double quotes as a name for a "not-standard" attribute?

– icolumbro
Apr 13 at 10:58













@icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

– Krzysiek Dróżdż
Apr 13 at 11:00





@icolumbro Aaaa... Sorry for that, it's just a typo - this happens when you write answer on mobile. I've already fixed this - sorry again :)

– Krzysiek Dróżdż
Apr 13 at 11:00




1




1





I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

– icolumbro
Apr 13 at 11:10





I figured it is a typo even reading the codex page but I preferred to ask you to be sure, thank you, now I try!

– icolumbro
Apr 13 at 11:10

















draft saved

draft discarded
















































Thanks for contributing an answer to WordPress Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f334284%2fuse-a-php-file-as-action-for-a-form-in-a-wordpress-plugin-whats-the-correct-wa%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?