Wordpress meta box values display to custom fields - php

I've added meta boxes to my posts using the action "add_meta_boxes" to add/change custom settings like background-color, etc.
When I enable the custom fields in my screen options, all values of my meta boxes are displayd in these custom fields!
There are also showing up in the selectbox to add a new "custom field".

If you want to hide your post meta data from custom fields metabox, you should start your meta keys with underscore. Example _background-color
Added:
Also you can use is_protected_meta filter, which return boolean value (true - hide, false - show).
Filter parameters: $protected, $meta_key. See wp-includes/meta.php file. function is_protected_meta()

Normally WP hides meta keys that start with an underscore/_ from the Custom Fields (default/core) MetaBox.
Now imagine that you do not want to give the user of your plugin the possibility to alter Post Meta Data through the ugly and user unfriendly Custom Fields meta box. And therefore you build a custom meta box and prefix your meta key with an underscore/_. Then the user changes his mind and deactivates or uninstalls your plugin. What happens now is that the user has exactly no access to any UI to alter the (still present) meta data. This is a really, really bad situation for the user.
So we need a switch to turn off the Custom Fields MetaBox access as long as your plugin is activated. Therefore WP Core got the is_protected_meta() function. It basically consists out of two lines of code:
$protected = ( '_' == $meta_key[0] );
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
As it's not nice to only offer a filter to handle that, WordPress today has a simple function that you can use:
register_meta( $meta_type, $key, $sanitize_callback, $auth_callback );
And the last argument, the $auth_callback does the following inside this function:
if ( empty( $auth_callback ) ) {
if ( is_protected_meta( $meta_key, $meta_type ) )
$auth_callback = '__return_false';
else
$auth_callback = '__return_true';
}
if ( is_callable( $auth_callback ) )
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
As you can see, you want to just add '__return_false' as $auth_callback to deactivate Custom Fields MetaBox access as long as your plugin is active. When the user removes or deactivates your plugin, he instantly has access to the meta field via the standard Custom Fields MetaBox.
Notes: WP core at v4.0.1 while writing this question. Make use of the $sanitize_callback! Thanks to Trepmal for posting about the is_protected_meta filter on her blog. Else I would have never stumbled upon the use case for that.

Related

WordPress Custom Fields w/ Shortcodes to use in Posts and Pages

I am new to WordPress but learning very fast. I started my first plugin that creates a custom admin menu. One of the submenus is named Business Profile. Here is what I want to do:
1). Add custom fields to the Business Profile to collection business info (ex: Business Name)
2). Each custom field needs a related shortcode
3). All shortcodes can be placed within content on Posts and Pages and replaced with the custom field value.
I'm looking for a tutorial or some direction on how to do the above without the use of a plugin. Can anyone point me in the right direction?
I believe that you want to store and retrieve only one business information.
function sample_register_my_custom_menu_page(){
add_menu_page(
__( 'Business Profile', 'business-profile' ),
'Business Profile',
'manage_options',
'business-profile-settings',
'business_profile_settings_page',
80
);
}
add_action( 'admin_menu', 'sample_register_my_custom_menu_page' );
function business_profile_settings_page(){
if ($_POST) {
// Form Submission Processing
$business_profile = array();
$business_profile['business_name'] = sanitize_text_field($_POST['business_name']);
update_option('business_profile_details', $business_profile);
}
// Form View
// Add your html form here
$business_profile = get_option( 'business_profile_details' );
// Get your saved data and pre fill your form.
}
You can further use add_shortcode to get the data from the options and display it the post.
Note: Use wp_options to store this data only if this is a single data set. If you want to save data of multiple business you may need to use a custom post for it.

WooCommerce Hiding a specific custom attribute

I've seen multiple answers to how to move or remove all custom attributes and there is documentation on how to remove boilerplate attributes like dimension and SKU, but I still can't seem to find a snippet, filter or hook detailing how to go about removing a single specific "custom" attribute.
In our case, the custom attribute we have in use is useful for backend reasons, but we'd rather not show it on the frontend. I'm aware of the tick-box in product data for attributes while in admin making an attribute not visible, but I'm handling over 2,000 products...it just wouldn't be a practical approach to tick it for each one.
(EDIT)
I was finally able to source an example on how to do this.
#helgatheviking hit upon the base of coding needed to make this happen.
function my_attribute_hider ( $attributes ) {
if ( isset( $attributes['pa_attribute-name'] ) ){
unset( $attributes['pa_attribute-name'] );
}
return $attributes;
}
add_filter( 'woocommerce_get_product_attributes', 'my_attribute_hider' );
Be sure to replace attribute-name w/ your attribute's unique slug.
I almost missed the answer that I was looking for because the author has posted it on the question post itself. So I am posting it here.
function my_attribute_hider ( $attributes ) {
if ( isset( $attributes['pa_attribute-name'] ) ){
unset( $attributes['pa_attribute-name'] );
}
return $attributes;
}
add_filter( 'woocommerce_get_product_attributes', 'my_attribute_hider' );
Note: It will not remove the attribute itself, it will just hide it from the product pages. It will just ignore it.

Add hidden meta data to product Woocommerce

I've added a custom field to my product for admin only meta data, I have hidden it using CSS.
However it still shows up in emails. Is there any way I can create a custom field where the meta data only shows up in the admin orders page?
You could try to use woocommerce_email_order_meta_fields filter hook to remove this custom field from order metadata, using unset() php function this way:
add_filter( 'woocommerce_email_order_meta_fields', 'wc_email_order_meta_remove_custom_field', 10, 3 );
function wc_email_order_meta_remove_custom_field( $fields, $sent_to_admin, $order ) {
// Replace HERE 'meta_key' by your custom field meta key or slug.
unset($fields['meta_key']);
return $fields;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work, but not sure as you don't provide any information and code related to the way you have set this custom field.

Custom field not showing in admin Wordpress

As I created custom post type with taxonomy, when I add a new custom field, it is saving in the database but not showing in custom fields. I don't understand how to show the custom field in admin panel.
There is a very good plugin called Advanced Custom Fields for Wordpress. It's very easy to use and features conditional logic for page layouts, post type and much more.
We can easily create a meta box without using any plugin and customize it as per our needs:
here is the Wordpress documentation to create a meta box and here is the example to easily implement it with custom post type. Here is the example:
<?php
add_action('add_meta_boxes', 'meta_box_add_function');;
function meta_box_add_function()
{
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
'post' // Post type ['post', 'custom_post_type']
);
// You can add multiple boxes like above
}
function wporg_custom_box_html($post){
echo 'What you put here, show\'s up in the meta box';
}
?>
And here you can save the post data using below hook:
<?php add_action( 'save_post', 'meta_box_save_function' ); ?>

How do you add Text Color Button to Advanced Custom Fields TinyMCE Basic toolbar?

By default ACF has two choices for the WYSIWYG custom field regarding toolbars, Full and Basic. These are both great but I just need one more button( maybe more in another project) in the Basic toolbar.
I am trying to add the text-color picker to the Basic toolbar.
Based on this documentation here, ACF Documentation, I came up with this:
add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars' );
function my_toolbars( $toolbars ) {
array_unshift( $toolbars['Basic' ] , 'forecolor' );
return $toolbars;
}
I also looked at this past question,"How to add a button to ACF tiny MCE editor", but didn't find the links provided very clear in producing a result(maybe partially because comments were in French).
I am guessing it has something to do with what was said in that questions comments about missing the plugin for that button. But I am unsure, any solutions?
With some help from the Advanced Custom Fields Support Forum, I found out that my code was prepending to the outer array and I needed to target the nested array to add the button.
This is the code that works to add the Font Color button to the Basic Toolbar:
add_filter( 'acf/fields/wysiwyg/toolbars' , 'my_toolbars' );
function my_toolbars( $toolbars ) {
array_unshift( $toolbars['Basic' ][1], 'forecolor' );
return $toolbars;
}

Categories