Wordpress meta box in specific template file not saving - php

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?

Related

Custom Woocommerce Tracking Form

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

save checkbox meta box and use it in wordpress

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
}

How to use Metabox checkbox to add page ids in array?

What I'm trying to do is for a dual-language website. This particular language, Papiamento, isn't supported by Wordpress. Therefore, the client had to create two separate pages, in English and Pap. What I did was to code it like that to display English or Pap menu for each page.
Like this, in header.php:
<?php
if( is_page( array('salon-and-spa-pap', 'tocante-nos', 'testimonio', 'tuma-contacto-cu-nos', 'galeria', 'tratamentonan-di-masahe', 'tratamentonan-spa-di-curpa', 'servicionan-di-boda', 'tratamentonan-spa-di-cara', 'wowo-lip-nek', 'cuido-di-man', 'tratamento-di-huna', 'tratamento-di-huna-di-pia', 'cuido-di-pia', 'salon-p', 'spa-etiquette-pap', 'wax-p', 'reserva-un-tratamento')) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
However, the problem is that the client will have to keep going back to header.php to add another page slug every time she create a new page. Therefore, I created a Metabox plugin for that. I made a Metabox checkbox so that everytime a page is intended for Papiamento language, the client can just check the box and either the page id or slug will be added to the code above.
I found another question (https://wordpress.stackexchange.com/questions/71043/listing-pages-with-checkboxes-in-a-metabox-and-saving-them) that might be similar to what I was looking for but it didn't work for me.
Here's my metabox function based on this article (http://themefoundation.com/wordpress-meta-boxes-guide/).
<?php
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Papiamento Page Box', 'prfx-textdomain' ), 'prfx_meta_callback', 'page', 'normal', 'low' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="checkfield[]" id="page_<?php echo $page->ID; ?>" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?>
<?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?>
</label>
</div>
</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
// Checks for input and saves
if( isset( $_POST[ 'checkfield' ] ) ) {
update_post_meta($post_id, "checkfield", $_POST['checkfield'] );
}
}
add_action( 'save_post', 'prfx_meta_save' );
and calling the page id in header.php like this:
<?php
if( in_array($page->ID, (array) $checkfield) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
But it didn't work. Please help!
***************** UPDATE *****************
I've been trying to edit the function prfx_meta_callback( $post ) but with no success. Here's the latest code I'm trying to modify...
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
$page = get_pages();
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="page_<?php echo $page->ID; ?>">
<input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( isset($checkfield [ ?>'<?php echo $page->ID; ?>'<?php ] ) ) checked( $checkfield[ ?>'<?php echo $page->ID; ?>'<?php ][0], '<?php echo $page->ID; ?>'); ?>/> <?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?></label> <br>
</div>
</p>
<?php
I've been trying to make it like that, if Pap, then checked, but if not Pap, unchecked in every page.
If the metabox is saving the checkbox settings properly, you can use the following code to make the check in header.php:
if ( in_array( get_the_ID(), get_post_meta( get_the_ID(), 'checkfield', true ) ) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}

Wordpress Custom Meta Box Returns Empty?

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');

cannot save data to db using wordpress metaboxes

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/

Categories