Display custom meta box to the admin sidebar not working - wordpress

I created a custom meta box to display some content inside the post display in the admin area and want it to show in the sidebar instead of below the wysiwyg editor. I added "side" to the context but nothing happens! I've been playing with this for many hours and haven't had any luck.
This is my code:
function add_custom_meta_box() {
add_meta_box (
'custom_meta_box',
'Custom Meta Box Title',
'show_custom_meta_box',
'post',
'side',
'high'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function show_custom_meta_box() {
// here i have all the code
}

Adapted from this Q&A, the following will force the custom meta box into the second position in the side column.
Check the comments and note the caveat for admin_init.
It only works if the user hasn't rearranged the positions himself. When registering a new user, the position is set for him, as the hooks admin_init and user_register are attached to the same callback function.
// This fires at **every** page load, a better hook must be found
add_action( 'admin_init', 'set_user_metaboxes_so_14183498' );
// This fires when a new user is created
add_action( 'user_register', 'set_user_metaboxes_so_14183498' );
function set_user_metaboxes_so_14183498( $user_id = null )
{
// This is the metakey we will need to update
$meta_key = 'meta-box-order_post';
// So this can be used without hooking into user_register
if( !$user_id )
$user_id = get_current_user_id();
// Set the default order if it has not been set yet by the user.
// These are WP handles, PLUS our custom meta box handle
if ( ! get_user_meta( $user_id, $meta_key, true ) )
{
$meta_value = array(
'side' => 'submitdiv,custom_meta_box,formatdiv,postimagediv',
'normal' => 'postcustom,commentsdiv,slugdiv,revisionsdiv',
'advanced' => '',
);
update_user_meta( $user_id, $meta_key, $meta_value );
}
}

Related

How to update Order meta data, from a WooCommerce Action

I'm trying to add some custom meta_data to a WooCommerce Order, by running a Order action.
Here is my code:
function custom_add_order_actions( $actions ){
global $theorder;
$actions['my_custom_action'] = 'My custom action';
return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_add_order_actions' );
function custom_add_single_action( $order ){
// Non of these change anything on the order
$order->set_billing_first_name( 'A new test name' );
$order->update_post_meta( 'a_test_field', 'Test field value' );
update_post_meta( $order->get_id(), 'a_test_field', 'Some other value' );
// $order->save(); // I even tried adding this as well, but it doesn't change anything.
}
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
How do I change the order (or specifically, post_meta fields for an order) from inside an action?
A example
Imagine that I add a post_meta field, with the field name (key): a_test_field.
It's currently an ACF-field, but it's the same for regular WordPress custom fields.
If I change the value of the field and press 'Update', then the value changes:
So far so good. Now the value of the field is 'Foobar'.
What's wierd is that even if I do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
update_post_meta( $order->get_id(), 'a_test_field', 'A new value' );
die(); // This die is vital, to make the change in the database.
}
Then I can see the value change in the database to 'A new value'.
But if I just do this:
add_action( 'woocommerce_order_action_my_custom_action', 'custom_add_single_action' );
function custom_add_single_action( $order ){
$order->update_post_meta( 'a_test_field', 'A new value' );
// No die(); here...
}
Then the value remains 'Foobar' in the database.
Sorry but the following lightly revisited code works (Selecting the action and click on the button arrow):
add_action( 'woocommerce_order_actions', 'add_custom_order_action' );
function add_custom_order_action( $actions ){
$actions['my_custom_action'] = __('My custom action', 'WooCommerce');
return $actions;
}
add_action( 'woocommerce_order_action_my_custom_action', 'triggered_custom_order_action' );
function triggered_custom_order_action( $order ){
$order->update_meta_data( '_test_1_custom_field', 'AAFFBB9977' );
$order->save();
update_post_meta( $order->get_id(), '_test_2_custom_field', 'Some other value' );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: Order actions are mostly used for some other things than what you are trying to do.
Now when using a meta box with an input field (as you are showing), on submit, you should save that field value using the action hook save_post_shop_order like in those related threads:
Metabox with multi checkbox in WooCommerce admin single orders
Dynamic custom order numbers based on payment method
Metabox with multiple custom fields for WooCommerce admin order pages

Woocommerce remove admin order item item meta

Im adding a custom item meta to every item with woocommerce_add_order_item_meta action.
I dont need to show this custom meta in the Order Detail, because it's an arry stringy that im using to print a pdf.
How can i remove this meta custom item? Is there some action to do it?
Thanks
I understand its a bit old question but I am answering for some other users who will have same issue in future.
If you want your order item meta to not display in admin order details page than you should append underscore (_) at the start of your meta name.
Example:
_custom_order_meta
The underscore trick no longer works. In Woo 3.x there is a hidden meta array:
add_filter('woocommerce_hidden_order_itemmeta',
array($this, 'hidden_order_itemmeta'), 50);
function hidden_order_itemmeta($args) {
$args[] = 'my_hidden_meta';
return $args;
}
It sounds like you need to keep it in order to print the PDF. If you override the order-details.php template you can possibly change:
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
to
$array = $item['item_meta'];
if( isset( $array['your_pdf_array_key'] ) ){ unset( $array['your_pdf_array_key'] ); }
$item_meta = new WC_Order_Item_Meta( $array, $_product );
EDIT
The wc_add_order_item_meta() function has 4 parameters as seen in the code:
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'order_item', $item_id, $meta_key, $meta_value, $unique );
}
If you choose a $meta_key with a preceding underscore, the meta will be automatically hidden from view on the checkout/order-received page, the My Order's list of the My account area, as well as in the admin's order overview page.
Therefore, I would suggest making your woocommerce_add_order_item_meta callback function look something like the following:
add_action( 'woocommerce_add_order_item_meta', '25979024_add_order_item_meta', 10, 3 );
function 25979024_add_order_item_meta( $order_item_id, $cart_item, $cart_item_key ) {
wc_add_order_item_meta( $order_item_id, '_pdf_something', 'hide this stuff' );
}

Get WooCommerce products on sale with WC_Query

I know there are plenty of solutions on the internet about how to get WooCommerce products on sale, by doing a WP_Query. However, WooCommerce doesn't seem to work fully if it's WC_Query object is not populated. For example: filter or sorting
Both these templates call:
woocommerce_products_will_display()
Which check's to see if the page is a taxonomy page (obvious false if you're using your own custom template):
if ( ! is_product_taxonomy() ) return false;
This is an example of a simple solution if you just want the products: WooCommerce: Display ONLY on-sale products in Shop
So, I there seems to be a couple of issues I need to solve here:
1) How to tell WC that my "Sale" page is a taxonomy page? Is there some sort of trick I need to do to force it into a taxonomy?
2) How do I get get WC_Query filled with the sales query (rather than just the WP_Query)
I have plugins that depend on:
$woocommerce->query->layered_nav_product_ids
being populated.
Any help is appreciated!
Thanks!!!
Well woocommerce_products_will_display() is pluggable, meaning you can define it in your own functions.php (or site-specific plugin) and alter it, having it return true for your specific template/page.
I think it could stand for some tweaking and a filter.
EDIT
I played around with this a bit more. Typically changing the posts that you want to retrieve is done in the pre_get_posts hook. I took a look at what WooCommerce is doing. They are adding something to the pre_get_posts hook and calling their special query stuff from there.
But their special query stuff dies if you aren't on a WooCommerce page. So, it made me thing that maybe we could just call it ourselves from our own function. I put this together and coupled with a special page template for a page called "on-sale" (basically just a copy of the shop template), seems to show just the for sale items with proper sorting and pagination.
Your mileage may vary, but I hope it helps.
function kia_pre_get_posts( $q ){
// We only want to affect the main query
if ( ! $q->is_main_query() ) {
return;
}
// Fix for verbose page rules
if ( is_page('on-sale') ) {
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
$meta_query = array( array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
) );
$q->set( 'meta_query', $meta_query );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
$wc_query = WC()->query;
$wc_query->product_query( $q );
if ( is_search() ) {
add_filter( 'posts_where', array( $wc_query, 'search_post_excerpt' ) );
add_filter( 'wp', array( $wc_query, 'remove_posts_where' ) );
}
add_filter( 'posts_where', array( $wc_query, 'exclude_protected_products' ) );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( $wc_query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
$wc_query->remove_product_query();
}
add_action( 'pre_get_posts', 'kia_pre_get_posts' );

How to Display Custom meta field value insted of title on custon post list table?

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.
function remove_box(){
remove_post_type_support('cinfo', 'title');
remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");
It looks something like this.
Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.
I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.
add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
$defaults['title'] = 'Email';
return $defaults;
}
add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
if ($column_name == 'title') {
echo "its working";
}
}
The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.
I tried this with a fast class to test all together inside my setup:
class SO23467344
{
private $cpt = 'portfolio';
private $custom_field = 'video';
public function __construct()
{
add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
add_action( "admin_init", array( $this, "remove_box" ) );
}
function column_register( $columns )
{
$columns['my-col'] = 'My column';
# Remove default columns
unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );
return $columns;
}
function column_display( $column_name, $post_id )
{
if ( 'my-col' != $column_name )
return;
if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
}
function remove_box(){
remove_post_type_support( $this->cpt, 'title' );
remove_post_type_support( $this->cpt, 'editor' );
}
}
new SO23467344;

WordPress Hide Page Editor Based on ID?

Is there anyway to hide the post editor wysiwyg on a page based on the page ID?
I have a few custom meta boxes and dont need it on this specific page..
I've tried the following but it doesnt do anything, the page has the id of 75 but its still showing up?
function remove_pages_editor(){
if(get_the_ID() == 75) {
remove_post_type_support( 'post', 'editor' );
} // end if
} // end remove_pages_editor
add_action( 'add_meta_boxes', 'remove_pages_editor' );
Any ideas..?
This can be done with an earlier hook, do_action('load-' . $page_hook);.
add_action( 'load-post.php', 'hide_specific_editor_so_15154969' );
function hide_specific_editor_so_15154969()
{
if( '75' == $_GET['post'] && 'edit' == $_GET['action'] )
remove_post_type_support( 'post', 'editor' );
}

Resources