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.
Related
I'm working on my own Avada Theme, only my WordPress homepage search working. other pages search not working I have attached all codes Any idea how to solve the issue? I have attached my search code.
Searchform.php
<?php
/**
* The search-form template.
*
* #package Avada
* #subpackage Templates
*/
// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct script access denied.' );
}
?>
<form role="search" class="searchform" method="get" action="<?php echo home_url();?>"
<div class="search-table">
<div class="search-field">
<input type="text" value="" name="s" class="s" placeholder="<?php esc_html_e( 'Search ...', 'Avada' ); ?>" required aria-required="true" aria-label="<?php esc_html_e( 'Search ...', 'Avada' ); ?>"/>
</div>
<div class="search-button">
<input type="submit" class="searchsubmit" value="" alt="<?php esc_attr_e( 'Search', 'Avada' ); ?>" />
</div>
</div>
</form>
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
I'd like to give another html element the function of 'add to cart'. Is this possible? I have added some html code into single-product.php and want one of these elements to function as 'add to cart' when you click on them :)
I hope that I've been able to explain myself :P
The following will work as an add to cart button within a product loop, where $product is the post object -
<?php if( $product->is_purchasable() ):?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit">Add to Cart</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php endif; ?>
How I search my WordPress site pages with search box I placed in my site?
Currently I am using this code.
<form id="searchform" method="get" action="<?php bloginfo('siteurl')?>/" style="margin-left: -43px;">
<input type="text" name="" id="s" class="textbox" placeholder="Search" required/>
<input id="btnSearch" type="submit" name="submit" value="<?php _e('Search'); ?>" />
</form>
I used name="s" but it not do search properly. I want that if user right page1 name search box take it to siteurl/page1 and so for page2 and remaining page.
Thanks in advance
Try this :
function redirect_search() {
if (is_search() && !empty($_GET['s'])) {
wp_redirect(home_url("/").urlencode($_GET['s']));
exit();
}
}
add_action('template_redirect', 'redirect_search' );
Add above code in your functions.php file.
So now when you type something, it will redirect it to that page.
NOTE:If you type something related to the published post, it will redirect to that POST instead of what have you entered.
For example : If I type hello in search box, then it will redirect you to hello world(which is default post that comes during installation of wordpress).
This is what have for you.
<div id="content" class="textbox <?php echo of_get_option('blog_sidebar_pos') ?>">
<?php _e('Search for:',''); ?> "<?php the_search_query(); ?>"
<?php
if (have_posts()) : while (have_posts()) : the_post();
$format = get_post_format();
get_template_part( 'include/'.$format );
if($format == '')
get_template_part( 'include/standard' );
endwhile; else:
?>
<?php echo '<p><strong>' . __('There has been an error.', '') . '</strong></p>'; ?>
<p> <?php _e('return to home page', ''); ?> <?php _e('use the search .', ''); ?></p>
<?php get_search_form(); ?>
</div><!--no-results-->
<?php endif; ?>
<?php get_template_part('include/post'); ?>
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;
}
}