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' ); ?>
Related
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.
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.
Sorry if my terminology is confusing but I am unsure of what call this exactly.
In Wordpress when editing a post or page type, on the right side of the editor there is the sections Publish, Attributes, Featured Image, etc. I'm wondering if there is a way where I can add my own section to the right side like the other? Is it custom post type or something more complicated?
I'm trying to stay within the possiblity of using hooks.
Those are 'meta boxes'
http://codex.wordpress.org/Function_Reference/add_meta_box
function my_custom_meta_boxes()
{
add_meta_box(
'custom1', // id
'Custom Metabox', // title
'display_custom_meta_box', // callback for display
'post', // post type
'normal', // position
'high' // priority
);
}
add_action('add_meta_boxes', 'my_custom_meta_boxes');
function display_custom_meta_box()
{
echo 'Your meta box here';
}
There are plugins available to create meta boxes quickly e.g.
http://wordpress.org/extend/plugins/meta-box/
http://wordpress.org/extend/plugins/types/
http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
Read up about it here^
Wasn't that hard to google! :)
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.
I'm building a plugin which will pull custom meta post automatically.
For example if I have a post with content
"A post content"
when display content in the theme used the_content() tag, I want to display as :
A post content <br> <div id="custom-data">Some custom data</div>
In that way, I can build a plugin to pull custom meta post automatically and re-style the custom data.
Anyone know it? Thanks!
You can use add_filter() to add text to the content. Here's an example:
function functionname($text){
$text .= 'Some extra text, added below the content';
return $text;
}
add_filter('the_content', 'functionname');
There are many filters you can hook on, here's a list: http://codex.wordpress.org/Plugin_API/Filter_Reference