I've reviewed several online docs on this as well as reviewed the post's on this site but still can't seem to get my admin-post requests to work. In my admin menu constructor I add the following actions:
add_action('admin_post_modify_release_request', 'handle_release_posting');
with the following function for the handler:
function handle_release_posting(){
print_r($_POST);
echo "<br/>incoming:" . var_export($_POST, TRUE) . "<br/>";
?> <div class="wrap">
<?php screen_icon(); ?>
<div class="release_response">Posting response goes here.</div>
</div>
<?php
}
My html form element is this:
" method="post">
with other inputs plus...
<input type="hidden" id='action' name="action" value="modify_release_request">
<input type="submit" value="Modify Release"></div>
</form>
The behavior on submission of the request is to call http://joomla.tenasys.lan/wp-admin/admin-post.php
with a completely empty page:
<html><head></head><body></body></html>
What Have I missed? What am I doing wrong?
Somehow I have not managed to use hidden input to pass action parameter, like this:
The alternative solution was add action parameter in URL as this:
<!-- language: lang-html -->
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>?action=add_foobar" method="post">
Here is whole solution:
<!-- language: lang-php -->
function sandbox_show_plugin(){
global $post;
?>
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>?action=add_foobar" method="post">
<input name="name" type="text" placeholder="Enter email">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<?php
}
//Hooks
add_shortcode( 'sandbox_plugin_shortcode', 'sandbox_show_plugin' );
function prefix_admin_add_foobar(){
status_header(200);
var_dump($_POST);
// wp_redirect( 'http://any.url/',302 );
die("Server received '{$_POST['name']}' from your browser.");
}
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' ); // this is for non logged users
Related
I have a form it sends data to options.php. After that that saved value takes and imported into textarea.
<form method="post" action="options.php">
<?php
settings_fields( 'my_option_group' );
do_settings_sections( 'my_settings_admin' );
submit_button();
?>
</form>
This code works, but if I want to save in DB some default value manually, the value doesn't come into textarea's value. I'm using add_option or update_option.
Understand this flow to save the value to database,
/* code for adding menu and options in admin */
add_action('admin_menu','admin_menu_adding');
add_action('admin_init','admin_reg_settings');
function admin_menu_adding(){
add_menu_page('Mega Page','Mega Page','administrator','mega-page','settings_page_test');
}
function settings_page_test(){
echo "test";
?>
<form method="post" action="<?php echo admin_url('admin.php'); ?>">
<?php
settings_fields('local-settings');
do_settings_sections('local-settings');
?>
<?php submit_button(); ?>
<input type="hidden" name="action" value="trotbgo" />
</form>
<?php
}
add_action('admin_action_trotbgo','save_value_here');
function save_value_here(){
update_option() //update option goes here
exit;
}
function admin_reg_settings(){
register_setting('local-settings','fieldone');
register_setting('local-settings','fieldtwo');
}
You need to save via the add_option/update_option inside save_value_here() function
I created a custom checkout page.
I wanted to put the coupon code anywhere and it worked.
Unfortunately after clicking on the "apply coupon" button, instead of validating the coupon and adding it to the order, the system responds as per clicking the "order and pay" button Code used in form-checkout.php:
<?php do_action('woocommerce_checkout_coupon_form', $checkout); ?>
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( wc_get_checkout_url() ); ?>" enctype="multipart/form-data">
<div class="checkout-form-container">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<div class="col2-set" id="customer_details">
<div class="col-1">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_checkout_after_customer_details'); ?>
<p class="form-row form-row-first">
<label for="coupon_code">Coupon?</label>
<input type="text" name="coupon_code" class="custom-coupon-code-input input-text" placeholder="<?php esc_attr_e( 'Wprowadż kod', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<input type="submit"button class="button custom-apply-coupon-btn">
<?php esc_attr_e( 'Apply', 'woocommerce' ); ?>
</button>
</p>
<?php endif; ?>
</div>
Check your checkout page HTML. You are actually putting your coupon form inside checkout form. HTML forms doesn't support nested forms.
Put the form outside of checkout form. If you really need to show them there then take those values by javascript with the click of 'Apply Coupon' button and do the validation. But A form tag inside another form is not the correct way.
The submit button is submitting parent form also. That's why you are getting that.
I know where is the problem. Everything works perfectly on woo 2.6.8 but after update to min 3.0.4 its crash as I described above.
I am trying to create an option panel WordPress theme. The option page displays fine but on submit of form I get:
ERROR: options page not found.
This is my code.
<?php
/*
Plugin Name: Option Man
Description: A test plugin to manage site
Author: Pravin Mishra
Version: 0.1
*/
add_action('admin_menu', 'test_plugin_setup_menu');
function test_plugin_setup_menu(){
add_menu_page( 'Option-Man-Plugin', 'Option Man', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init(){
add_action( 'admin_init', 'register_my_setting' );
function register_my_setting() {
//register_setting( 'myoptions-group', 'my_option_name', 'intval' );
register_setting(‘myoptions-group’, ‘testplugin_option1′);
register_setting(‘myoptions-group’, ‘testplugin_option2′);
}
echo "Hello world!";
?>
<div class="wrap">
<u><h2>BS3 Custom Options</h2></u>
<form method="post" action="http://192.168.2.139/web/wp-admin/options.php">
<?php //settings_fields( 'myoptions-group' );
//do_settings_sections( 'myoption-group' );
settings_fields( 'myoptions-group' );
?>
<input type="text" name="testplugin-option1" value="<?php echo get_option('testplugin_option1'); ?>"/>
<input type="text" name="testplugin-option2" value="<?php echo get_option('testplugin_option2'); ?>"/>
<?php
submit_button();
?>
</form>
</div>
<?php
}
?>
Change <form method="post" action="http://192.168.2.139/web/wp-admin/options.php"> to <form method="post" action="">
I have correct the code now it's working fine.
add_action('admin_menu', 'test_plugin_setup_menu');
function test_plugin_setup_menu(){
add_menu_page( 'Option-Man-Plugin', 'Option Man', 'manage_options', __FILE__, 'test_init' );
add_action( 'admin_init', 'register_my_setting' );
}
function register_my_setting() {
//register_setting( 'myoptions-group', 'my_option_name', 'intval' );
register_setting('myoptions-group', 'testplugin_option1');
register_setting('myoptions-group', 'testplugin_option2');
}
function test_init(){
?>
<div class="wrap">
<u><h2>BS3 Custom Options</h2></u>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php
settings_fields('myoptions-group');
do_settings_sections('myoptions-group');
?>
<input type="text" name="testplugin_option1" value="<?php echo get_option('testplugin_option1'); ?>"/>
<input type="text" name="testplugin_option2" value="<?php echo get_option('testplugin_option2'); ?>"/>
<?php
submit_button();
?>
</form>
</div>
<?php
}
Your field name and option name is not same and also the options page path is wrong.
i am new with wordpress.
I have modify search form in wordpress.
<form class="form-inline" role="form" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<input type="search" class="form-control" placeholder="Type your search" value="" name="query" />
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
In this form i have modify name attribute of Search input box name='s' to name='query'.
But after that search is not working .
Do i need to write anything in function.php to Get query string.
Try this code, i hope this will help you to workout,
Add this in theme's fuctions.php file:-
add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $query_vars )
{
if ( isset( $_GET['query'] ) && ! empty( $_GET['query'] ) ) {
$_GET['s'] = $_GET['query'];
}
return $query_vars;
}
I am new to wordpress plug in development. I have designed a search form however I have no idea where to handle and print the submitted form data.
it is a wedget based plug in and the plugin form section code is here:
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$message = apply_filters( 'widget_content', $instance['content'] );
echo $before_widget;
//if ( $title )
// echo $before_title . $title . $after_title;
echo '<div class="shk_location_form_holder">
<span class="shk_loc_title">'.$title.'
<form mthod="post">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form></div>';
echo $after_widget;
if(isset($_REQUEST['shk_inp_search_locations'])){
add_filter('the_content','handle_content');
}
}
In WP plugins you usually have an empty action="" in a form, and handle it in the same function (by the way, as wordpress procedural code becomes very messy, it's better to write plugins using OOP), because, anyway plugins are loaded before any content is outputted in WP (this is the reason why writing ajax plugins is so easy in wp). So you can have everything structured like this:
function draw_form() {
handle_submit();
?>
<div class="shk_location_form_holder">
<span class="shk_loc_title"><?php echo $title; ?></span>
<form mthod="post" action="">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form>
</div>
<?
}
function handle_submit() {
if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') {
echo 'you may want to end your program here, especially if it\'s ajax!';
exit;
}
}