I've created this code, it is supposed to use wordpress metabox functionality to save additional data of posts in DB.
//Price list meta boxes
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add() {add_meta_box( 'price-list-id', 'Price List', 'cd_meta_box_cb', 'post', 'normal', 'high' );}
function cd_meta_box_cb()
{
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
//
$plheading = isset( $values['price_list_heading'] ) ? esc_attr( $values['price_list_heading'][0] ) : '';
$plcategory1 = isset( $values['price_list_category1'] ) ? esc_attr( $values['price_list_category1'][0] ) : '';
$plcategoryitems1 = isset( $values['price_list_items_category1'] ) ? esc_attr( $values['price_list_items_category1'][0] ) : '';
$plcategory2 = isset( $values['price_list_category2'] ) ? esc_attr( $values['price_list_category2'][0] ) : '';
$plcategoryitems2 = isset( $values['price_list_items_category2'] ) ? esc_attr( $values['price_list_items_category2'][0] ) : '';
$plcategory3 = isset( $values['price_list_category3'] ) ? esc_attr( $values['price_list_category3'][0] ) : '';
$plcategoryitems3 = isset( $values['price_list_items_category3'] ) ? esc_attr( $values['price_list_items_category3'][0] ) : '';
// We'll use this nonce field later on when saving.
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="price_list_heading">Price list heading:</label>
<input type="text" name="price_list_heading" id="price_list_heading" value="<?php echo $plheading; ?>" />
</p>
<p>
<label for="price_list_category1">Category1 Title:</label>
<input type="text" name="price_list_category1" id="price_list_category1" value="<?php echo $plcategory1; ?>" />
<textarea style="width: 100%;" rows="3" name="price_list_items_category1" id="price_list_items_category1"><?php echo $plcategoryitems1; ?></textarea>
<span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 €</span>
</p>
<p>
<label for="price_list_category2">Category2 Title:</label>
<input type="text" name="price_list_category2" id="price_list_category2" value="<?php echo $plcategory2; ?>" />
<textarea style="width: 100%;" rows="3" name="price_list_items_category2" id="price_list_items_category2"><?php echo $plcategoryitems2; ?></textarea>
<span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 €</span>
</p>
<p>
<label for="price_list_category3">Category3 Title:</label>
<input type="text" name="price_list_category3" id="price_list_category3" value="<?php echo $plcategory3; ?>" />
<textarea style="width: 100%;" rows="3" name="price_list_items_category3" id="price_list_items_category3"><?php echo $plcategoryitems3; ?></textarea>
<span style="display: block; font-weight: bold; text-align: right;">Example: Rhine Riesling1|0,75 l|9,50 €</span>
</p>
<?php
}
//Saving price list
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Make sure your data is set before trying to save it
if( isset( $_POST['price_list_heading'] ) )
update_post_meta( $post_id, 'price_list_heading', esc_attr( $_POST['price_list_heading'] ) );
//
if( isset( $_POST['price_list_category1'] ) )
update_post_meta( $post_id, 'price_list_category1', esc_attr( $_POST['price_list_category1'] ) );
if( isset( $_POST['price_list_items_category1'] ) )
update_post_meta( $post_id, 'price_list_items_category1', esc_attr( $_POST['price_list_items_category1'] ) );
//
if( isset( $_POST['price_list_category2'] ) )
update_post_meta( $post_id, 'price_list_category2', esc_attr( $_POST['price_list_category2'] ) );
if( isset( $_POST['price_list_items_category2'] ) )
update_post_meta( $post_id, 'price_list_items_category2', esc_attr( $_POST['price_list_items_category2'] ) );
//
if( isset( $_POST['price_list_category3'] ) )
update_post_meta( $post_id, 'price_list_category3', esc_attr( $_POST['price_list_category3'] ) );
if( isset( $_POST['price_list_items_category3'] ) )
update_post_meta( $post_id, 'price_list_items_category3', esc_attr( $_POST['price_list_items_category3'] ) );
}
Instead of saving what I write in textboxes and inputs in DB I find "Array" on all fields! What is going on? What did I miss?
figured it out $plheading = isset( $values['price_list_heading'] ) ? esc_attr( $values['price_list_heading'][0] ) : '';
I was missing [0]
btw, in that tutorial in some examples [0] is missing :)
Please have a look at the steps and see if you missed anything, here is the link http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/
Related
I tried modifying this plug-in for WordPress:
https://easydigitaldownloads.com/downloads/custom-prices/?utm_campaign=documentation&utm_source=helpscout&utm_medium=doc&utm_term=1975-custom-prices-overview
What I'm trying to achieve is moving the $ sign next to the custom price field or alternatively removing it completely.
/*
* Hook into add to cart post
*/
function edd_cp_purchase_link_top( $download_id ) {
global $edd_options;
$default_price = get_post_meta( $download_id, 'edd_cp_default_price', true );
$min_price = edd_format_amount ( get_post_meta( $download_id, 'edd_cp_min', true ) );
$custom_price = isset( $_GET ['cp_price'] ) ? edd_format_amount( $_GET ['cp_price'] ) : '';
$button_text = get_post_meta( $download_id, 'cp_button_text', true );
if ( empty( $custom_price ) && ! empty( $default_price ) ) {
$custom_price = edd_format_amount( $default_price );
}
if ( edd_cp_has_custom_pricing( $download_id ) && ! edd_single_price_option_mode( $download_id ) ) {
$wrapper_display = '';
$download = new EDD_Download( $download_id );
if ( edd_item_in_cart( $download_id, array() ) && ( ! $download->has_variable_prices() || ! $download->is_single_price_mode() ) ) {
$wrapper_display = 'style="display:none;"';
} ?>
<p class="edd-cp-container" <?php echo $wrapper_display; ?>>
<?php
echo __( 'Name your price', 'edd_cp' );
if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<?php echo edd_currency_filter( '' ); ?> <input type="text" name="edd_cp_price" class="edd_cp_price" value="<?php echo esc_attr( $custom_price ); ?>" size="30" data-min="<?php echo $min_price; ?>" style="width: 40px;" data-default-text="<?php echo esc_attr( $button_text ); ?>" />
<?php else : ?>
<input type="text" name="edd_cp_price" class="edd_cp_price" value="<?php echo esc_attr( $custom_price ); ?>" size="30" data-min="<?php echo $min_price; ?>" style="width: 80px;" data-default-text="<?php echo esc_attr( $button_text ); ?>" /><?php echo edd_currency_filter( '' );
endif;
$min_no_format = floatval( $min_price );
if( !empty( $min_no_format ) ) {
echo ' <small>(min '.edd_currency_filter( ( $min_price ) ).')</small>';
} ?>
</p>
<?php
}
}
add_filter( 'edd_purchase_link_top', 'edd_cp_purchase_link_top' );
/*
* Add additional list item if variable pricing is enabled
*/
function edd_cp_after_price_options_list( $key, $price, $download_id ) {
if( ! edd_cp_has_custom_pricing( $download_id ) ) {
return;
}
if ( ! edd_single_price_option_mode( $download_id ) ) {
return;
}
global $edd_options;
$default_price_option = edd_get_default_variable_price( $download_id );
$display = 'none';
if ( $key === $default_price_option ) {
$display = 'block';
} ?>
<div class="edd-cp-price-option-wrapper" style="display: <?php echo esc_attr( $display ); ?>;">
<?php
echo __( 'Name your price', 'edd_cp' );
if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<?php echo edd_currency_filter( '' ); ?> <input type="text" name="edd_cp_price[<?php echo esc_attr( $key ); ?>]" class="edd_cp_price" value="<?php echo esc_attr( $price['amount'] ); ?>" size="30" data-min="" />
<?php else : ?>
<input type="text" name="edd_cp_price[<?php echo esc_attr( $key ); ?>]" class="edd_cp_price" value="<?php echo esc_attr( $price['amount'] ); ?>" size="30" data-min="" data-default-text="<?php echo esc_attr( $button_text ); ?>" /><?php echo edd_currency_filter( '' );
endif; ?>
</div>
<?php }
add_action( 'edd_after_price_option', 'edd_cp_after_price_options_list', 10, 3 );
See the attached image. This is how it currently looks.
I really appreciate your help!
Woocommerce has a default tracking form that lets customers see their order status.
But I'm looking to replace the Email request with Billing Phone Number (which value should be billing_phone)
Any idea how to change? It seems it's not that easy to change order_email with billing_phone.
Here is the part in woocommerce/order/tracking-form.php
<p class="form-row form-row-first">
<label for="orderid">
<?php esc_html_e( 'Order ID', 'woocommerce' ); ?>
</label>
<input class="input-text" type="text" name="orderid" id="orderid" value="<?php echo isset( $_REQUEST['orderid'] ) ? esc_attr( wp_unslash( $_REQUEST['orderid'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Found in your order confirmation email.', 'woocommerce' ); ?>" />
</p>
<?php // #codingStandardsIgnoreLine ?>
<p class="form-row form-row-last">
<label for="order_email">
<?php esc_html_e( 'Billing email', 'woocommerce' ); ?>
</label>
<input class="input-text" type="text" name="order_email" id="order_email" value="<?php echo isset( $_REQUEST['order_email'] ) ? esc_attr( wp_unslash( $_REQUEST['order_email'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Email you used during checkout.', 'woocommerce' ); ?>" />
</p>
<?php // #codingStandardsIgnoreLine ?>
<div class="clear"></div>
Thank you all.
There is no simple solution for this. Since the core WC process is to get the email input and to compare it against the order billing email.
Unfortunately, I could not find an easy way to override it.
But you can consider adjusting the copy of the template file in your theme folder to get a response from your code rather than WC.
to do this, you will need:
Change the nonce field in the form, so the action will be customized (then you will be able to "catch" and handle it from the server-side:
wp_nonce_field( 'woocommerce-order_tracking_custom', 'woocommerce-order-tracking-custom-nonce' );
Copy (to your functions.php) and adjust the method output from class-wc-shortcode-order-tracking.php
public static function output( $atts ) {
// Check cart class is loaded or abort.
if ( is_null( WC()->cart ) ) {
return;
}
...
...
wc_get_template( 'order/form-tracking.php' );
}
To something like this:
add_action('init', 'handle_tracking_request');
function handle_tracking_request( ) {
// Check cart class is loaded or abort.
if ( is_null( WC()->cart ) ) {
return;
}
$nonce_value = wc_get_var( $_REQUEST['woocommerce-order-tracking-custom-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );
if ( isset( $_REQUEST['orderid'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-order_tracking_custom' ) ) {
$order_id = empty( $_REQUEST['orderid'] ) ? 0 : ltrim( wc_clean( wp_unslash( $_REQUEST['orderid'] ) ), '#' ); // WPCS: input var ok.
$order_phone = empty( $_REQUEST['order_phone'] ) ? '' : sanitize_email( wp_unslash( $_REQUEST['order_phone'] ) );
if ( ! $order_id ) {
wc_print_notice( __( 'Please enter a valid order ID', 'woocommerce' ), 'error' );
} elseif ( ! $order_phone ) {
wc_print_notice( __( 'Please enter a valid phone', 'woocommerce' ), 'error' );
} else {
$order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
if ( $order && $order->get_id() && ( $order->get_billing_phone() ) === strtolower( $order_phone ) ) {
do_action( 'woocommerce_track_order', $order->get_id() );
wc_get_template(
'order/tracking.php', array(
'order' => $order,
)
);
return;
} else {
wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error' );
}
}
}
wc_get_template( 'order/form-tracking.php' );
}
I didn't tested it, and hope it will work. You may need to adjust it further. let me know if it helped you
I made a checkbox meta box in my functions.php file but I have two problems. I can't save if checkbox is checked or not for the next times. and I want to show something in div if it was checked.
<?php
function filmview_ext_info_meta_box() {
add_meta_box(
'filmview_ext_info_meta_box',
__( 'Extra info', 'filmview_ext_info_meta' ),
'filmview_ext_info_meta_html',
'filmview',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'filmview_ext_info_meta_box' );
function filmview_ext_info_meta_html( $post) {
wp_nonce_field( '_filmview_ext_info_meta_nonce', 'filmview_ext_info_meta_nonce' ); ?>
<p>
<input type="checkbox" id="filmview_ext_info_meta_sub" name="filmview_ext_info_meta_sub" <?php checked( $check, 'on' ); ?> />
<label for="filmview_ext_info_meta_sub">Does it have subtitle?</label>
</p>
<?php
}
function filmview_info_meta_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['filmview_info_meta_nonce'] ) || ! wp_verify_nonce( $_POST['filmview_info_meta_nonce'], '_filmview_info_meta_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
$chk = isset( $_POST['filmview_ext_info_meta_sub'] ) ? 'on' : 'off';
update_post_meta( $post_id, 'filmview_ext_info_meta_sub', $chk );
}
add_action( 'save_post', 'filmview_info_meta_save' );
?>
What I can see is that:
1) You init $chk in the function: filmview_info_meta_save
2) You are using (with your provided code, an undefined variable ) $check
Try to read it from the database instead of of $_POST which is only present in the page after you submit the form, this should Do:
function filmview_ext_info_meta_html( $post) {
wp_nonce_field( '_filmview_ext_info_meta_nonce', 'filmview_ext_info_meta_nonce' );
$chk = get_post_meta($post->ID, 'filmview_ext_info_meta_sub', true);
?>
<p>
<input type="checkbox" id="filmview_ext_info_meta_sub" name="filmview_ext_info_meta_sub" <?php checked( $chk[0], 'on' ); ?> />
<label for="filmview_ext_info_meta_sub">Does it have subtitle?</label>
</p>
<?php
}
I created a custom meta box (author) for a custom post type (books). The box shows up in the admin fine, but I can't figure out how to display the author in my theme.
This is the code used to create the box:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Book Author', 'prfx-textdomain' ), 'prfx_meta_callback', 'rabe_books', 'side' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
/**
* Adds the meta box stylesheet when appropriate
*/
function prfx_admin_styles(){
global $typenow;
if( $typenow == 'post' ) {
wp_enqueue_style( 'prfx_meta_box_styles', plugin_dir_url( __FILE__ ) . 'meta-box-styles.css' );
}
}
add_action( 'admin_print_styles', 'prfx_admin_styles' ); ?>
And this is what I placed in my template:
<div class="book-author">by:
<?php $book_author = get_post_meta( get_the_ID(), 'meta-text', true );
if (!empty($book_author)) {
echo $book_author;
} elseif (empty($book_author)) {
echo "Why doesnt it work?";
} ?>
</div>
Instead of displaying the author, it displays the text "Why doesnt it work?" which I guess means that the value is empty. But it shouldn't be. What am I doing wrong?
As mevius mentioned, you're issue is here:
<?php _e( 'Example Text Input', 'prfx-textdomain' )?>
http://codex.wordpress.org/Function_Reference/_e
You'll need to assign the box to a data point.
This is a snippet from one of our functions.php file that has this:
function css_metadata_box($object, $box) { ?>
<p>
<label for="custom-css-data">Add custom CSS for this page</label>
<textarea name="custom-css-data" id="custom-css-data" cols="60" rows="25" tabindex="30" style="width: 97%;"><?php
echo esc_html( get_post_meta( $object->ID, 'custom-css', true ), 1 );
?></textarea>
<input type="hidden" name="css_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
add_meta_box('css-data-box', 'Page CSS', 'css_metadata_box', 'page', 'normal', 'high');
I seem to have a problem with my meta box saving, not totally sure what I've done wrong. Any help would be greatly appreciated!
Heres my functions file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'golf.php')
{
add_meta_box (
'golf-times',
'Golf Opening Times & Prices',
'cd_meta_box_cb',
'page',
'normal',
'high'
);
}
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$times = isset( $values['golf_meta_box_times'] ) ? esc_attr( $values['golf_meta_box_times'][0] ) : '';
$prices = isset( $values['golf_meta_box_prices'] ) ? esc_attr( $values['golf_meta_box_prices'][0] ) : '';
wp_nonce_field( 'golf_meta_box_nonce', 'meta_box_nonce' );
?>
<div style="overflow: hidden;">
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_times">Opening Times</label></p>
<p><textarea type="text" name="golf_meta_box_times" id="golf_meta_box_times" rows="5" style="width: 90%;" value="<?php echo $times; ?>"> </textarea></p>
</div>
<div style="width: 45%; float: left;">
<p><label for="golf_meta_box_prices">Prices</label></p>
<p><textarea type="text" name="golf_meta_box_prices" id="golf_meta_box_prices" rows="5" style="width: 90%;" value="<?php echo $prices; ?>"> </textarea></p>
</div>
</div>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'golf_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array(
'href' => array()
)
);
if( isset( $_POST['golf_meta_box_times'] ) )
update_post_meta( $post_id, 'golf_meta_box_times', wp_kses( $_POST['golf_meta_box_times'], $allowed ) );
if( isset( $_POST['golf_meta_box_prices'] ) )
update_post_meta( $post_id, 'golf_meta_box_prices', wp_kses( $_POST['golf_meta_box_prices'], $allowed ) );
}
?>
If anyone has a clue how to make this save it would be a great help!
I am also having issues making the data display - but I can only assume thats where it isnt actually saving haha!
Cheers
functions.php cannot output HTML as far as i know.
wrap your html in a string and return it, your file might be returning errors.
Your add_meta_box code seems to be correct, the condition might not be met, are you sure you are sending data over get?