WooCommerce Orders metabox: Run php code on custom submit action - php

In Woocommerce, I have been able to add a custom submit button on order edit page in a custom metabox.
Here is my code (added in function.php Wordpress theme):
add_action( 'add_meta_boxes', 'MY_order_meta_boxes' );
function MY_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'order_meta_box_content',
'shop_order',
'side',
'default'
);
}
function order_meta_box_content( $order_id ) {
global $woocommerce, $table_prefix, $wpdb;
$order = new WC_Order( $order_id );
$customeremail = $order->get_billing_email();
?>
<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($submit)) {$order->add_order_note(sprintf("test2"));}
?>
<?php
return $order_id;
}
But I don't know why, the code doesn't run when button is clicked (submitted).
How can I run some custom code, when submit button is clicked on this custom metabox?

To make this work as you expect you need some more things. I have also removed unnecessary code and some errors. Also <imput> "submit" ID is too generic and can make unexpected errors.
You will be able to do any action (or saves) in a custom function hooked in save_post action hook:
// Add a custom metabox
add_action( 'add_meta_boxes', 'trusted_list_order_meta_boxes' );
function trusted_list_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'trusted_list_order_meta_box_content',
'shop_order',
'side',
'default'
);
}
// Custom metabox content
function trusted_list_order_meta_box_content( $post ){
$customeremail = get_post_meta( $post->ID, '_billing_email', true);
$button_text = __( 'Add Note action', 'woocommerce' );
echo '<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit_trusted_list" value="' . $button_text . '"/>
<input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '">
</form>';
}
// Saving or doing an action when submitting
add_action( 'save_post', 'trusted_list_save_meta_box_data' );
function trusted_list_save_meta_box_data( $post_id ){
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Check if our nonce is set (and our cutom field)
if ( ! isset( $_POST[ 'trusted_list_nonce' ] ) && isset( $_POST['submit_trusted_list'] ) )
return $post_id;
$nonce = $_POST[ 'trusted_list_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
return $post_id;
// Action to make or (saving data)
if( isset( $_POST['submit_trusted_list'] ) ) {
$order = wc_get_order( $post_id );
// $customeremail = $order->get_billing_email();
$order->add_order_note(sprintf("test2"));
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested on Woocommerce 3+ and works. You will get:
In your custom Metabox content function you will not be able to get any data using $_POST submitted data… So for example $_POST['submit'] will be always empty.

...I think you should use $_POST['submit'] other than $submit

Related

Add personalized content after the "Add to Cart" button? Woocommerce

Woocommerce: How to add some html codes below add to cart in single product page? and i want add different codes for each single product!
I used a similar one for metabox, but I can not find a way to adapt it after "add to cart"
---- 1. Backend ----
// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_meta_box',
__( 'Additional Product text <em>(optional)</em>', 'woocommerce' ),
'add_custom_product_content_meta_box',
'product',
'normal',
'default'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){
function add_custom_product_content_meta_box( $post ){
$text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : '';
$args['textarea_rows'] = 6;
echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>';
wp_editor( $text_area, 'custom_text', $args );
echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 );
if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){
function save_custom_product_content_meta_box( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
// Sanitize user input and update the meta field in the database.
if ( isset( $_POST[ 'custom_text' ] ) )
update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) );
}
}
---- 2. Frontend ----
// Add custom text under single product meta
add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 );
function add_custom_product_text() {
global $product;
$custom_text = get_post_meta( $product->get_id(), '_custom_text', true );
if( empty($custom_text) ) return;
echo '<div class="product-extra-text" style="margin-top:30px;">';
echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $custom_text );
echo '</div>';
}
I put the code in parts, but it is united in functions of the theme.
Backend IMAGE:
enter image description here
In your theme's function.php add the code,
add_action('woocommerce_after_add_to_cart_button','show_custom_text');
function show_custom_text() {
call the custom text inside this function
}

Meta Box data not saving when updating the post

I am trying to add a meta box, following a tutorial. I have triple checked the code that it matches that of the tutorial (in which case, the meta box data IS saving), but on mine, it is not saving. This is my code.
<?php
/*
Plugin Name: Custom Post Meta Box
Plugin URI: http://acetronaut.com
Description: Demonstrates how to implement a custom posts meta box into
wordpress
Version: 1.0.0
Author: Acetronaut
Author URI: http://acetronaut.com
License: GPL2
*/
function acetrnt_add_admin_styles() {
wp_enqueue_style( 'acetrnt-admin', plugins_url( 'custom-post-meta-box/css/admin.css' ) );
}
add_action( 'admin_enqueue_scripts', 'acetrnt_add_admin_styles' );
function acetrnt_add_meta_box() {
add_meta_box(
'acetrnt_audio', // The ID for the meta box
'Add MP3', //The title of the meta box
'acetrnt_display_meta_box', //The function for rendering the markup
'post', // We'll only be displaying this on post pages
'side', //Where the meta box should appear
'core' // The priority of where the meta box whould be displayed
);
}
add_action( 'add_meta_boxes', 'acetrnt_add_meta_box' );
function acetrnt_display_meta_box( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'acetrnt_nonce_field' ); ?>
<label id="mp3-title" for="mp3-title">Title of MP3</label>
<input type="text" id="mp3-title" name="mp3-title" value="<?php echo esc_attr( get_post_meta( $post->ID, true ) ); ?>" placeholder="Your Song By Elton John" />
<label id="mp3-file" for="mp3-file">MP3 File</label>
<input type="file" id="mp3-file" name="mp3-file" value="" />
<?php
}
function acetrnt_save_meta_box_data( $post_id ) {
if ( acetrnt_user_can_save( $post_id, 'acetrnt-nonce-field' ) ) {
if ( isset( $_POST['mp3-title'] ) && 0 < count( strlen( trim( $_POST['mp3-title'] ) ) ) ) {
$mp3_title = $_POST['mp3-title'];
update_post_meta( $post_id, 'mp3-title', $mp3_title );
}
}
}
add_action( 'save_post', 'acetrnt_save_meta_box_data' );
function acetrnt_user_can_save( $post_id, $nonce ) {
// Is this an autosave?
$is_autosave = wp_is_post_autosave( $post_id );
// Is this a revision?
$is_revision = wp_is_post_revision( $post_id );
// Is the nonce valid?
$is_valid_nonce = ( isset( $_POST[ $nonce] ) && wp_verify_nonce( $_POST[ $nonce ], plugin_basename( __FILE__ ) ) );
// Return true if the user is able to save the file
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
?>
I am banging my head against the wall as to the culprit. Is there anything in my code that is wrong? I have also tried dropping out of php and using regular html code, but that does not work.

Wordpress Custom Post Type Meta Box not saving to database

I've created a Custom Post Type 'media-page-items' that I'm attempting to add meta boxes to. Currently the meta boxes are showing but not saving to the database. I've tried a few different approaches on it and none of them seem to save to database.
Debug is turned on but no errors are being thrown currently that I can see.
Any help is much appreciated!
//add article link to media page item
add_action( 'admin_menu', 'gruman_article_link_create' );
add_action( 'save_post', 'gruman_article_link_save', 10, 2 );
function gruman_article_link_create() {
add_meta_box( 'gruman-article-link', 'Article Link', 'gruman_article_link', 'media-page-items', 'advanced', 'high' );
}
function gruman_article_link( $post ) {
// retrieve the _gruman_article_title current value
$current_article_link = get_post_meta( $post->ID, '_gruman_article_link', true );
?>
<p>
<label>Article Link</label>
<br />
<input name="gruman-article-link" id="article-link" style="width: 97%;"><?php $current_article_link; ?>/>
<input type="hidden" name="gruman_article_link_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
function gruman_article_link_save( $post_id ) {
// verify taxonomies meta box nonce
if ( !isset( $_POST['gruman_article_link_nonce'] ) || !wp_verify_nonce( $_POST['gruman_article_link_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return $post_id;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ){
return $post_id;
}
// store article title value
if ( isset( $_REQUEST['gruman-article-link'] ) ) {
update_post_meta( $post_id, '_gruman_article_link', sanitize_text_field( $_POST['gruman-article-link'] ) );
}
}
In wp_create_nonce you are using plugin_basename( __FILE__ ).
And when you verifying nonce you use basename( __FILE__ ) as action name.
Those values are not the same. First one will return something like my-plugin/my-plugin.php and the second will be my-plugin.php
That is why I believe wp_verify_nonce returns False and your data is not saved.

How to make the custom meta boxes support for the visual composer in WordPress?

I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.

Issues displaying custom meta box value on post edit page

I am trying to add some custom meta boxes to my wordpress post edit page. I have the box added (Video URL) but when I attempt to call the info from the post into the field as the set value nothing ever shows. I believe I am saving it correctly, but I can't get it to show after being saved, please help. Here is the code for adding and saving the meta box info:
<!-- adding the video url meta box prototype data -->
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'post-video', 'Video URL', 'video_url_meta_box', 'post', 'side', 'high' );
}
?>
<!-- rendering the video url meta box on the post edit page -->
<?php
function video_url_meta_box( $post )
{
$value = get_post_meta( $post->ID, 'video_url_text', true );
wp_nonce_field( 'video_url_nonce', 'meta_box_nonce' );
?>
<p>
<label for="video_url_text">Youtube or Vimeo URL</label>
<input type="text" name="video_url_text" id="video_url_text" value="<?php echo $value; ?>" />
</p>
<?php
}
?>
<!-- Saving the video url meta box data -->
<?php
add_action( 'save_post', 'video_url_save' );
function video_url_save( $post_id )
{
//return if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
//return if we can't verify nonce or it isn't there
if( !isset( $_POST['video_url_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'video_url_nonce' ) ) return;
//return if the current user can't edit the post
if( !current_user_can( 'edit_page' ) ) return;
// save data once all checks are passed
// make sure the url is set
if( isset( $_POST['video_url_text'] ) )
update_post_meta( $post_id, 'video_url_text' );
}
?>
I ended up moving all of the code into a custom plugin and got it working as shown below:
/**
* Adds the meta boxes to the post editing screen
*/
function weshine_meta_boxes() {
add_meta_box( 'video_url_meta', __( 'Video URL', 'video-url-text' ), 'video_url_meta_callback', 'post', 'side', 'high' );
add_meta_box( 'caption_text_meta', __( 'Caption Text', 'thumb-caption-text' ), 'caption_text_meta_callback', 'post', 'side', 'high');
}
add_action( 'add_meta_boxes', 'weshine_meta_boxes' );
/**
* Outputs the content of the meta box
*/
function video_url_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$video_url_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="video-url" class="prfx-row-title"><?php _e( 'Enter Youtube or Vimeo URL', 'video-url-text' )?></label>
<input type="text" name="video-url" id="video-url" value="<?php if ( isset ( $video_url_stored_meta['video-url'] ) ) echo $video_url_stored_meta['video-url'][0]; ?>"$
</p>
<?php
}
/**
* Saves the custom meta input
*/
function video_url_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[ 'video-url' ] ) ) {
update_post_meta( $post_id, 'video-url', sanitize_text_field( $_POST[ 'video-url' ] ) );
}
}
add_action( 'save_post', 'video_url_meta_save' );

Categories