How to save in wp_options table as WordPress standards? - wordpress

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

Related

Gravity Form on WordPress

I am trying to pull dynamic form id to create a dropdown for the end-user. This I have done here.
Now, I'd like to pass that id so that a different form shows up based on selection as shown below:
<form class="ui form">
<select name="select" class="ui fluid search selection dropdown">
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option name="select" value="<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo gravity_form($form, true, true, false, '', true, 1); ?>
Here's what I might do to make a shortcode for gravity forms that pulls the ID from the URL parameter.
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'.$form_id.'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
You'd add the shortcode [form-url] to the page where you'd like the form to display.
So now the URL with ?form_id=6 would load the gravity form with id 6. I didn't test the whole javascript aspect but assuming you can change the URL and force a reload things should work out ok.
Here is what I ended up doing if it helps someone else out there:
<form class="ui form">
<select name="select" id="select" class="ui fluid search selection dropdown" onchange="location = this.options[this.selectedIndex].value;">
<?php
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'. $form_id .'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
?>
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option value="/prevention/sessions/?form_id=<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo do_shortcode('[form-url]') ?>

can't get wordPress's admin-post.php to work

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

Create WordPress theme:

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.

Wordpress Gallery, how to dynamically update a custom select box on attachment upload

I've added a select box audio option to the default gallery settings. Here's what I added to the functions.php file:
<?php
function add_custom_gallery_settings() {
global $post;
$attached_audio = get_attached_media( 'audio', $post->ID );
?>
<script type="text/html" id="tmpl-custom-gallery-settings">
<label class="setting">
<span><?php _e('Audio'); ?></span>
<select data-setting="audio">
<option value="">No Audio</option>
<?php foreach($attached_audio as $a): ?>
<option value="<?php esc_attr_e($a->ID); ?>">
<?php echo $a->post_name; ?>
</option>
<?php endforeach; ?>
</select>
</label>
</script>
<script>
jQuery(document).ready(function(){
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
render: function() {
wp.media.view.Settings.prototype.render.apply(this, arguments);
this.$el.append(wp.media.template('custom-gallery-settings'));
wp.media.gallery.defaults.audio = null;
this.update.apply( this, ['audio'] );
return this;
}
});
});
</script>
<?php }
add_action('print_media_templates', 'add_custom_gallery_settings');
?>
As you see, select box populates with audio attachments uploaded to the post. Because I generate those templates on print_media_templates hook, any audio files uploaded to the post, won't show up until I update/save the post and/or reload the page. Question is, what would be a good way to dynamically update those options when an audio attachment is uploaded?

How to have multiple sections in the same theme options page

For my theme, I have created a Settings Page which is shown in the Appearance menu of Wordpress admin.
Now, I have been following this tutorial.
Exactly like the tutorial, I have created multiple sections and I face the issue where only the fields of the last section are saved in the DB while the fields of other sections are not saved.
I do not want to go for the Tabbed Navigation option - I have too many sections.
How do I solve the problem of not being able to save fields of other sections due to the nonce issue?
EDIT: I am using Wordpress 3.3.1
Try to separate section forms...
Just like this:--
<?php if( $active_tab == 'display_options' ) { ?>
<form method="post" action="options.php">
<?php
settings_fields( 'sandbox_theme_display_options' );
do_settings_sections( 'sandbox_theme_display_options' );
submit_button();
?>
</form>
<?php } else { ?>
<form method="post" action="options.php">
<?php
settings_fields( 'sandbox_theme_social_options' );
do_settings_sections( 'sandbox_theme_social_options' );
submit_button();
?>
</form>
<?php } ?>
May be this will help you...
In tutorial document, Form is like following...
<form method="post" action="options.php">
<?php
if( $active_tab == 'display_options' ) {
settings_fields( 'sandbox_theme_display_options' );
do_settings_sections( 'sandbox_theme_display_options' );
} else {
settings_fields( 'sandbox_theme_social_options' );
do_settings_sections( 'sandbox_theme_social_options' );
} // end if/else
submit_button();
?>
</form>
This is also separate form content.

Resources