I want to enable and disable custom meta-box on click of a button in my plugin. This button will be on the settings page of the plugin. This is what I've done so far:
function wpplugin_settings_page_markup()
{
if(!current_user_can('manage_options'))
{
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e(get_admin_page_title()); ?></h1>
<?php
?>
<form method="post" action="">
<input type="checkbox" id="postcb" name="postcheck" value="Post">
<label id='postcbid' name="labelpostcheck" for='postcb'>
Post
</label>
<input type="submit" value="submit" name="submit_btn">
</form>
</div>
<?php
function cd_meta_box_add()
{
$multi_posts=array('post', 'page');
foreach ($multi_posts as $multi_post) {
add_meta_box(
'my-meta-box-id', //id
'Custom Meta Box', //title
'cd_meta_box_cb', //callback
$multi_post, //post type
'normal', //position
'high' //priority
);
}
}
add_action('add_meta_boxes', 'cd_meta_box_add');
function cd_meta_box_cb()
{
echo'<b> This is Custom meta box </b>';
}
}
This code does not display the meta-box but I want the meta-box to be added only when that checkbox is checked, and removed when checkbox is not checked. Can anyone please help me achieve that? Thanks!
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 );
} ?>
Adding From,To and Message fields in cart page before checkout.I have added some code in cart.php file but after adding that code the cart page is displaying blank.
/**
* Add the order_comments field to the cart
**/
add_action('woocommerce_cart_collaterals',
'order_comments_custom_cart_field');
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?>
</label>
<textarea id="customer_notes_text"></textarea>
</div>
<?php
}
/**
* Process the checkout and overwriting the normal button
*
*/
function woocommerce_button_proceed_to_checkout() {
$checkout_url = wc_get_checkout_url();
?>
<form id="checkout_form" method="POST" action="<?php echo $checkout_url;
?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<a href="#" onclick="document.getElementById('customer_notes').value=document.getElementById('customer_notes_text').value;document.getElementById('checkout_form').submit()" class="checkout-button button alt wc-forward">
<?php _e( 'Proceed to checkout', 'woocommerce' ); ?></a>
</form>
<?php
}
// getting the values in checkout again
add_action('woocommerce_checkout_before_customer_details',function(){
?>
<script>
jQuery( document ).ready(function() {
jQuery('#order_comments' ).val("<?php echo
sanitize_text_field($_POST['customer_notes']); ?>");
});
</script>
<?php
});
In cart.php i have added this code at the bottom before closing the form tag as well as after the form tag.But i am getting a blank page after adding this piece of code in cart.php.
In the same format i am trying to get those from,to and message fields.
The following code will post from a custom textarea field in cart page the imputed text value to checkout order notes field:
// Add the order_comments field to the cart
add_action( 'woocommerce_cart_collaterals', 'order_comments_custom_cart_field' );
function order_comments_custom_cart_field() {
?>
<div class="customer_notes_on_cart" style="clear:both;">
<label for="customer_notes_text"><?php _e("Order notes", "woocommerce") ?></label>
<textarea id="customer_notes_text"></textarea></div>
<?php
}
// Process the checkout and overwriting the normal button
add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 15 );
function change_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
?>
<form id="checkout_form" method="POST" action="<?php echo wc_get_checkout_url(); ?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
</form>
<?php
}
// Jquery script for cart and checkout pages
add_action('wp_footer', 'customer_notes_jquery' );
function customer_notes_jquery() {
?>
<script>
jQuery(function($) {
<?php // For cart
if( is_cart() ) : ?>
$('#customer_notes_text').on( 'blur', function(){
$('#customer_notes').val($(this).val());
});
<?php // For checkout
elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
$('#order_comments' ).val("<?php echo sanitize_text_field($_POST['customer_notes']); ?>");
<?php endif; ?>
});
</script>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
I think is better no change "proceed to checkout" form, is better to store vars in localstorage when data in field changed, and get it after, when user is in checkout form.
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?>
</label>
<textarea id="customer_notes_text"></textarea>
</div>
<script>
jQuery(document).ready(function (jQuery) {
jQuery("#customer_notes_text").on('change', function () {
localStorage.setItem(jQuery(this).attr('id'), this.val());
});
});
</script>
<?php
}
Then you can get it with
LocalStore.getItem(item);
Don't forget to destroy element after obtain it, with
LocalStorage.removeItem(item);
I would like to add a notes field in the Woocommerce cart page under Woocommerce cart coupon area. This field should be something similar to Woocommerce checkout page "Order Notes" field where the customer can add some notes.
So far I have this code that indicates my desired location:
add_action ('woocommerce_after_cart_table','add_content_below_cart_coupon');
function add_content_below_cart_coupon () {
echo 'this will show below the cart cuopon';
}
How can I add a notes field in this area so these customer notes would also appear in order details at checkout page?
Thanks!
I solved this problem but a little hacky, I suggest to put it in a plugin
/**
* Add the order_comments field to the cart
**/
add_action('woocommerce_cart_collaterals', 'order_comments_custom_cart_field');
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?></label>
<textarea id="customer_notes_text"></textarea>
</div>
<?php
}
/**
* Process the checkout and overwriting the normal button
*
*/
function woocommerce_button_proceed_to_checkout() {
$checkout_url = wc_get_checkout_url();
?>
<form id="checkout_form" method="POST" action="<?php echo $checkout_url; ?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<a href="#" onclick="document.getElementById('customer_notes').value=document.getElementById('customer_notes_text').value;document.getElementById('checkout_form').submit()" class="checkout-button button alt wc-forward">
<?php _e( 'Proceed to checkout', 'woocommerce' ); ?></a>
</form>
<?php
}
// getting the values in checkout again
add_action('woocommerce_checkout_before_customer_details',function(){
?>
<script>
jQuery( document ).ready(function() {
jQuery('#order_comments' ).val("<?php echo sanitize_text_field($_POST['customer_notes']); ?>");
});
</script>
<?php
});
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'm having what im sure is a simple issue but i can't manage to figure it out.
I'm coding a theme options page for my wordpress template and i've managed to get it where the values are saved and i can use them on the site but whenever i reload the theme options page all the form fields are blank and any previously applied settings are gone. My code is below.
<?php
//Theme Options Functionality is Below
if (get_option('pardue_theme_options')){
$theme_options = get_option('pardue_theme_options');
} else {
add_option('pardue_theme_options', array(
'sidebar2_on' => true,
'footer_text' => 'Made by William'
));
}
?>
<?php add_action('admin_menu', 'theme_page_add');
function theme_page_add() {
add_submenu_page('themes.php', 'Pardue Theme Options', 'Theme Options', 'administrator', 'themeoptions', 'theme_page_options');
}
function theme_page_options() {
global $theme_options;
$new_values = array(
'footer_text' => htmlentities($_POST['footer_text'], ENT_QUOTES),
);
update_option('pardue_theme_options', $new_values);
$theme_options = get_option('pardue_theme_options');
echo '<div class="wrap">';
echo '<h2>Theme Options</h2>';
?>
<form action="themes.php?page=themeoptions" method="post">
<label for="footer_text">Footer Text: </label><input name="footer_text" id="footer_text" value="<?php echo $theme_options['footer_text']; ?>" /> <br /> <br />
<label for="sidebar_checkbox">Sidebar 2 on: </label> <input name="sidebar_checkbox" id="sidebar_checkbox" value="on" type="checkbox" /> <br /> <br />
<input type="submit" value="Update Options" name="submit" />
</form>
<?php
echo '</div>';
}
?>
I found a good solution for coding my theme options. The plugin at the link below makes it very easy to code up theme options.
Link to plugin
Documentation is included when you activate the plugin.