I working on a theme options page for my template and want to integrate a checkbox. The problem is, the checkbox won't save the value. Where is my mistake? Well, I'm clueless...
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'wordpress_options', 'wordpress_theme_options');
}
function theme_options_add_page() {
add_theme_page('Options', 'Options', 'edit_theme_options', 'theme-options', 'wordpress_theme_options_page' );
}
function wordpress_theme_options_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false; ?>
[...]
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade">
<p><strong>Settings saved!</strong></p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'wordpress_options' ); ?>
<?php $options = get_option( 'wordpress_theme_options' ); ?>
<div id="admininterface">
[...]
<p>
<p class="before-form">Show RSS Icon:</p>
<?php $options = get_option( 'icon-rss-show' ); ?>
<input type="checkbox" name="$options[icon-rss-show]" value="1"<?php checked( 1 == $options['icon-rss-show'] ); ?> />
</p>
[...]
</div><!-- #admininterface -->
</form>
</div>
<?php } ?>
Adding another hidden input field will actually make the form save the zero value in the database like this:
<form>
<input type='hidden' value='0' name='selfdestruct'>
<input type='checkbox' value='1' name='selfdestruct'>
</form>
Make sure to place the hidden input above your regular one, with the value '0' and the same name as in the regular input field.
That answer is from here: https://stackoverflow.com/a/1992745/4688612
Please send all credit to that poster.
Related
I am trying to create a button "Approved" to change the post category from it's current one to the "approved" category. I don't mind if it reloads the page or not. I would also like to redirect the page afterward to the next post in the original category.
I have found some questions on this already but am ultimately lost on how to get this all together and working.
<?php add_shortcode('approved_button', 'brist_approved_button_function');
function brist_approved_button_function() {
ob_start(); ?>
<form method="post" action="approved.php">
<input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->
</form>
<?php
wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );
$output = ob_get_clean();
return $output;
}?>
I figured it out. Though it was a head-scratcher for me. If anyone else if having the same issue, note the code below. You have to create the select and then have the 'selected' option on the category you want the post to change to. Then, in the CSS, hide the select input, only leaving the button.
<?php add_shortcode('approved_button', 'approved_button_function');?>
<?php function approved_button_function() { ob_start();?>
<div class="approval">
<form action="" id="update-post" method="post">
<?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>
<input class="approval-button" type="submit" name="submit" value="Approve" />
</form>
</div>
<?php if ( array_key_exists('cat', $_POST )) {
global $post;
$post_id = $post->ID;
wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );
} ?>
I read different articles, but they describe more other things. I do not want to make the wrong code with too much space. In the module there will be one single value that can be changed in the admin panel and other extraneous functions. How correctly to create a database for value of this window in a plugin?
I found the documentation, but I do not have enough experience to cut the excess, if it is.
My code /plugins/custom-counter/custom-counter.php
/*
Plugin Name: Custom Counter
Plugin URI: https://example.com
Description: This plugin adds counter.
Author: Kuznetsova Alexandra
Author URI: https://example.com
*/
// Hook for adding admin menus
add_action('admin_menu', 'custom_counter_menu');
// action function for above hook
function custom_counter_menu() {
// Add a submenu to Woocommerce menu:
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// custom_counter_page() displays the page content
function custom_counter_page() {
?>
<div class="wrap">
<h2>Custom Counter</h2>
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Total Counter</th>
<td><input type="text" name="custom-counter" value="..." /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
Updated - As this is a unique value that will be updated multiple times, you can use the wp_options table with the following WordPress dedicated functions:
get_option($option); where $option is the name of option to retrieve
update_option($option, $new_value); where $option is the name of option and $new_value the new value to be updated.
The fact that options are cached is not a problem if you use some tricks as you will see below, to avoid this data to be cached.
So try the following code:
// Add a custom admin submenu to Woocommerce
add_action('admin_menu', 'custom_counter_menu');
function custom_counter_menu() {
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// Content for the custom Woocommerce admin submenu
function custom_counter_page() {
$option_name = 'wc-custom-counter' ;
if( isset($_POST[$option_name]) ){
$new_value = sanitize_text_field( $_POST[$option_name] );
if ( get_option( $option_name ) !== false ) {
update_option($option_name, $new_value );
} else {
add_option( $option_name, $new_value, null, 'no' );
}
}
$default = ''; // Set the default value
$value = get_option( $option_name ) ? get_option( $option_name ) : $default;
?>
<div class="wrap">
<h2><?php _e('Custom Counter'); ?></h2>
<form method="post" action="">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Total Counter'); ?></th>
<td><input type="text" name="<?php echo $option_name; ?>" value="<?php echo $value; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
Code goes in function.php file of the active child theme (or active theme). tested and work.
If the option already exist, to avoid caching issue, you should need to delete it using:
delete_option('wc-custom-counter');
And adding it in your function.php file. Then browse any page of your web site and remove it.
If you only want to store one thing, like a setting, then you can use wp_options. You can then use WordPress functions to interact with the database instead of writing your own SQL.
I added a checkbox to the setting page like this:
function abc_render_admin(){
global $abc_options;
ob_start(); ?>
<form id="abc-form" action="" method="POST">
<div>
<?php $options = get_option('abc_options'); ?>
<p>
<input id="abc_settings[update_date]" name= "abc_settings[update_date]" type="checkbox" value="1" <?php checked('1', $abc_options['update_date']); ?>/>
<label class="description" for="abc_settings[update_date]">
<?php _e('Option 1', 'abc'); ?>
</label>
</p>
<button id="abc_submit" name="abc-submit" class="button-primary"> <?php _e('Submit', 'abc'); ?> </button>
</div>
</form>
<div id="abc_results"></div>
<?php echo ob_get_clean();
}
The checkbox display nicely on the admin page. However, I couldn't figure out the correct way to check if the checkbox is checked. I've tried this (not working):
function abc_process_ajax(){
if($abc_options['update_date'] == true){
echo 'Checked';
}
}
add_action('wp_ajax_abc_show_results', 'abc_process_ajax');
I've also tried doing print_r($abc_options); but got nothing.
What is the correct way of checking a checkbox? I'm not very familiar with WordPress, any suggestions would be much appreciated.
By the way, the Ajax is working fine. Here's the script:
function abc_load_scripts($hook){
global $abc_settings;
if($hook != $abc_settings)
return;
wp_enqueue_script('abc-ajax', plugin_dir_url(__FILE__) .'js/abc-ajax.js', array('jquery'));
wp_localize_script('abc-ajax', 'abc_vars', array(
'abc_nonce' => wp_create_nonce('abc-nonce')
)
);
}
add_action('admin_enqueue_scripts', 'abc_load_scripts');
abc-ajax.js
jQuery(document).ready(function($){
$('#abc-form').submit(function(){
data = {
action: 'abc_show_results',
abc_nonce: abc_vars.abc_nonce
};
$.post(ajaxurl, data, function(response){
$('#abc_results').html(response);
});
return false;
});
});
I've created some custom fields on the user profile page in WordPress.
I've managed to build a front end editor for the user to change some e-mail preferences:
<?php if( isset($_POST['preferences']) ) :
update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');
update_field('event-suggestions', $_POST['event-suggestions'], 'user_'.$user_id.'');
update_field('woa-updates', $_POST['woa-updates'], 'user_'.$user_id.'');
echo '<p class="success">E-Mail Prefereneces Updated<p>';
endif; ?>
<form action="<?php the_permalink(); ?>" method="POST" class="user-email-settings">
<!-- planner reminders -->
<?php
$field = get_field('planner-reminders', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="planner-reminders" name="planner-reminders" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="planner-reminders">I don't want to revieve reminders about events I've added to my planner.</label>
<?php $checked = false; ?>
<!-- event suggestions from WOA -->
<?php
$field = get_field('event-suggestions', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="event-suggestions" name="event-suggestions" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="event-suggestions">I don't want to recieve suggestions about events I may be interested in.</label>
<?php $checked = false; ?>
<!-- updates from WOA -->
<?php
$field = get_field('woa-updates', 'user_'.$user_id.'');
if( $field == true ) {
$field = '1';
$checked = true;
}
?>
<input type="checkbox" id="woa-updates" name="woa-updates" class="pref"
value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
<label for="woa-updates">I don't want to recieve e-mail updates from What's On Advisor.</label>
<?php $checked = false; ?>
<input type="submit" value="Save Preferences" name="preferences" id="preferences" />
</form>
Now, this actually seems to work. If I update a checkbox it shows and checked/unchecked as it should, and it shows correctly in the front end and the back end.
But, when I try to query this setting with a wp_query to actually send the e-mails to those who haven't opted out, it bug out a little.
If the user opts out, then opts back in, the wp_query doens't pick them up. It only picks them up when I go into the wp-admin area and update their user profile. I don't actually have to change anything, just open the user up and click update.
Here's the wp_query just incase:
<?php $args = array(
'role' => 'Subscriber',
'meta_key' => 'planner-reminders',
'meta_value' => '0',
'meta_compare' => '=='
); ?>
<?php $user_query = new WP_User_Query( $args ); ?>
<?php if ( ! empty( $user_query->results ) ) : ?>
etc. etc.
Any ideas how I can get this working properly? Is there a function to fake a click on the 'Update User' button in wp-admin?
Thanks.
When the user opts out, the $field value will be empty, and therefore the value-attribute of the checkboxes will be empty. I haven't fully tested it, but this can yield unexpected behaviour in your setup. When a form with an unchecked checkbox is submitted through a POST request, the checkbox name will not be set in the $_POST array, which is why you should, in this case, set the value-attribute of the checkboxes to "1", so it is properly stored via update_field.
Could you try changing the checkbox values in the code posted above to "1" for the checkbox input elements? That would be value="1" instead of value="<?php echo $field; ?>".
To prevent PHP notices being generated for inexistent array keys, I would advise to change
update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');
to
update_field('planner-reminders', empty( $_POST['planner-reminders'] ) ? '0' : '1', 'user_'.$user_id.'');
as well.
I'm creating my first options page and i'm trying to upload images for a slider, I don't know how many images are going to be in the slider so I will need to add more with a + button, It will automatically show one text input an upload button and a + button to start off with, should I require more i'll click the + button which will then add another text input, upload button a + button and a - button.
I've got this working to a point, it still needs a little help but it's getting there http://jsfiddle.net/vs8p5/5/
So far if I upload an image and click the + button, it will give me the option to upload another, this is working great and the images upload to wordpress.
Now for the issue.
I'm using this part of code to retrieve the data from wordpress
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
echo "<script type='text/javascript'>alert('yes it has');</script>";
}
This tells me that if there is a value then echo the alert
Now, working form that code above I've
function kandibox_hero_upload_image_link_callback($args) {
$hero_options = get_option( 'hero_options' ); ?>
<div id="upload_image_sets"> <?php
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}
else { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div>
<?php } ?>
</div> <?php
}
What i'm trying to do here is echo a form with a value, if nothing exists, echo a blank form, this seems to work although I don't think it's correct.
The main issue is that it will only echo the first form, when you click the + buttons, the form's id, name, and value script increments.
So now the question.
How can I get the form to look for all the $hero_options['upload_image_link_...'] and echo out a form for each if it exists? then echo out 1 blank form if nothing exists?
I'm pretty sure i've covered everything you might need but if i've missed something, let me know and i'll add it.
I've followed about 20 tutorials old and new and come up with this.
To add the information to the wordpress database, i'm using the following code.
function register_hero_options() {
add_settings_section(
'hero_settings_section', // ID used to identify this section and with which to register options
__( 'hero Options', 'kandibox' ), // Title to be displayed on the administration page
'kandibox_hero_options_callback', // Callback used to render the description of the section
'hero_options' // Page on which to add this section of options
);
add_settings_field(
'show_hero_options', // ID used to identify the field throughout the theme
__( 'hero', 'kandibox' ), // The label to the left of the option interface element
'kandibox_toggle_hero_callback', // The name of the function responsible for rendering the option interface
'hero_options', // The page on which this option will be displayed
'hero_settings_section' // The name of the section to which this field belongs
);
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['show_hero_options'] ) && $hero_options[ 'show_hero_options' ] ) {
add_settings_field(
'hero_size',
__( 'Size', 'kandibox' ),
'kandibox_hero_size_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'hero_background',
__( 'Background', 'kandibox' ),
'kandibox_hero_background_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'upload_image_links',
__( 'Upload Image', 'kandibox' ),
'kandibox_hero_upload_image_link_callback',
'hero_options',
'hero_settings_section'
);
}
register_setting(
'hero_options',
'hero_options'
);
}
add_action( 'admin_init', 'register_hero_options' );
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
ok i juss wrote a rough code for you might this will help you. this is according to the array you have go. there might be some better ways of doing it.
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
</div> <?php
Updated the code so now it's correct.